简体   繁体   English

在Copy Constructor和Assignment运算符中删除私有数组

[英]Deleting Private Array in Copy Constructor and Assignment operator

I'm trying to implement a container that allocated memory to the heap, but it seems as though my base constructor and my argument constructor don't like each other. 我正在尝试实现一个为堆分配内存的容器,但似乎我的基本构造函数和我的参数构造函数不喜欢彼此。 Below, I've posted the code without anything commented out. 下面,我发布的代码没有任何注释。 As it stands, it crashes. 就目前而言,它崩溃了。

#include <iostream>
using namespace std;

class foo
{
public:
    foo() {size=1; vals = new double[1]; vals[0]=0;}
    ~foo() {delete[] vals;}

    foo(const foo& other)
    {
        size=other.getsize();
        delete[] vals;
        vals = new double[size];
        for(long unsigned i=0; i<size; i++)
            vals[i]=other[i];
    }

    foo& operator=(const foo& other)
    {
        size=other.getsize();
        delete[] vals;
        vals = new double[size];
        for(long unsigned i=0; i<size; i++)
            vals[i]=other[i];
        return *this;
    }

    foo(double* invals, long unsigned insize)
    {
        size=insize;
        delete[] vals;
        vals = new double[size];
        for(long unsigned i=0; i<size; i++)
            vals[i]=invals[i];
    }

    double operator[](long unsigned i) const {return vals[i];}

    long unsigned getsize() const {return size;}
private:
    double* vals;
    long unsigned size;
};


int main()
{
    double bar[3] = {5,2,8};
    foo B(bar, 3);

    cout<< B[0]<< " "<< B[1]<< " "<< B[2]<<endl;    //couts fine

    foo A;    //crashes here

    return 0;
}

However, when I change main to be: 但是,当我改变主要是:

int main()
{
    double bar[3] = {5,2,8};
    foo B(bar, 3);

    cout<< B[0]<< " "<< B[1]<< " "<< B[2]<<endl;    //couts fine

    foo A();    //works now

    return 0;
}

It runs fine. 它运行正常。 But then I can't assign A = B because it thinks foo is a function or something. 但后来我不能指定A = B,因为它认为foo是一个函数或其他东西。

I assume you have some really compelling reason not to use std::vector<double> here... 我假设你有一些非常令人信服的理由不在这里使用std::vector<double> ...

But anyway... in your copy constructor, you don't want to delete[] vals . 但无论如何......在你的拷贝构造函数中,你不想delete[] vals

foo(const foo& other)
{
    size=other.getsize();
    vals = new double[size];
    for(long unsigned i=0; i<size; i++)
        vals[i]=other[i];
}

When the copy constructor is called, your object hasn't been initialized yet, so vals* doesn't even point to anything valid. 调用复制构造函数时,您的对象尚未初始化,因此vals*甚至没有指向任何有效的对象。 Therefore, deleting it invokes undefined behavior (and your program crashes.) You only need to delete[] vals in your assignment operator. 因此,删除它会调用未定义的行为 (并且程序崩溃。)您只需要在赋值运算符中delete[] vals

Also, when you declare the Foo variable A , you don't want those parentheses after the variable name. 此外,当您声明Foo变量A ,您不希望在变量名之后使用这些括号。 Just say: 说啊:

foo A;

When you place those parenthesis after the variable name, you're actually writing a function declaration using syntax inherited from C, and A becomes a function pointer type. 当您将这些括号放在变量名后面时,您实际上是使用从C继承的语法编写函数声明,并且A成为函数指针类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM