简体   繁体   English

C ++类复制构造函数

[英]C++ class copy constructor

I am having erratic issue with my copy constructor. 我的复制构造函数存在不稳定的问题。

I have a class MyData as follows: 我有一个MyData类,如下所示:

class MyData
{
    private:
    std::vector<double> wavelength;
    std::vector<double> amplitude;

    public:
    MyData::MyData(void) {}

    MyData::MyData(const MyData &cSource)
      : wavelength(cSource.wavelength), amplitude(cSource.amplitude)
    {}
}

In my main program, I am inserting MyData objects into a ring buffer. 在我的主程序中,我将MyData对象插入到环形缓冲区中。 This is how I am reading it back in main: 这就是我在main中读回它的方式:

MyData data;
data = removeq(&q);

The problem is that sometimes the copied data is missing some values. 问题是有时复制的数据缺少某些值。 Etc. if the original size of wavelength is 1, the copied data shows 0. I have debugged my program and the data in the ring buffer is correct etc it shows the correct size of 1. 等等,如果波长的原始大小为1,则复制的数据显示为0。我已经调试了程序,环形缓冲区中的数据正确,以此类推,显示的正确大小为1。

Anyone have any idea if my copy constructor is wrong or do i need an assignment operator overload ?? 任何人都知道我的副本构造函数是否错误,或者我是否需要赋值运算符重载?

Thanks! 谢谢!

The code i used for insert/remove into ring buffer: 我用于插入/删除环形缓冲区的代码:

void insertq(struct queue *p, MyData v)
{
    int t;
    t = (p->rear+1)%MAX;
    if(t == p->front)
    {   }
    else
    {
            p->rear = t;        
            p->arr[p->rear] = v;
    }
}
MyData removeq(struct queue *p)
{
    MyData empty;   

    if(isempty(p))
    {               
        return empty;
    }
    else
    {       
        p->front = (p->front + 1)%MAX;
        empty = p->arr[p->front];
        return empty;
    }
 }

In order to call the copy constructor you have to declare and initialize your object (using another object) in the same line. 为了调用复制构造函数,您必须在同一行中声明和初始化您的对象(使用另一个对象)。 Your current code actually calls the overloaded = operator. 您当前的代码实际上调用了重载=运算符。

A copy constructor is called when you create a new object from an existing object. 从现有对象创建新对象时,将调用复制构造函数。 Here, you are calling the assignment operator: 在这里,您正在调用赋值运算符:

MyData data;
data = removeq(&q);

If you had used 如果你曾经用过

 Data oldDataObject;
 Data newDataObject = oldDataObject; 

the copy constructor will get invoked. 复制构造函数将被调用。

In the context of your code, you should override the '=' operator to solve this. 在您的代码上下文中,您应该重写'='运算符以解决此问题。

MyData& operator = (const MyData& data);

Thanks all for the advice. 谢谢大家的建议。 I have removed the copy constructor and assignment overload methods as suggested. 我已按照建议删除了复制构造函数和赋值重载方法。

The issue was with the circular buffer structure I used. 问题出在我使用的循环缓冲区结构上。 I changed the circular buffer code to this example here: 我在这里将循环缓冲区代码更改为此示例:

http://www.codeproject.com/Articles/43510/Lock-Free-Single-Producer-Single-Consumer-Circular http://www.codeproject.com/Articles/43510/Lock-Free-Single-Producer-Single-Consumer-Circular

and it worked. 而且有效。 There seem to be no more errors. 似乎没有更多的错误了。 I had initially thought that the problem was due to the copy or assignment operator as the error was intermittent and so I did not check if it was the circular buffer that was causing the error. 最初我以为问题是由于复制或赋值运算符引起的,因为错误是断断续续的,所以我没有检查是否是导致错误的循环缓冲区。

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

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