简体   繁体   English

您如何在类的成员函数中调用复制构造函数?

[英]How do you call the copy constructor within a member function of a class?

Here's what I've got: 这是我得到的:

void set::operator =(const set& source)
{
    if (&source == this)
        return;

    clear();

    set(source);
}

And here's the error I get: 这是我得到的错误:

vset.cxx:33: error: declaration of 'source' shadows a parameter vset.cxx:33:错误:'源'的声明遮盖了参数

How do I properly do this? 如何正确执行此操作?

You are looking for the copy swap idiom: 您正在寻找复制交换习惯用法:

set& set::operator=(set const& source)
{
    /* You actually don't need this. But if creating a copy is expensive then feel free */
    if (&source == this)
        return *this;

    /*
     * This line is invoking the copy constructor.
     * You are copying 'source' into a temporary object not the current one.
     * But the use of the swap() immediately after the copy makes it logically
     * equivalent.
     */
    set tmp(source);
    this->swap(tmp);

    return *this;
}

void swap(set& dst) throw ()
{
    // swap member of this with members of dst
}

I believe with set(source); 我相信与set(source); you are trying to call copy ctor. 您正在尝试调用复制ctor。 You can not do that in C++ ie you can not explicitly invoke the ctor. 您不能在C ++中做到这一点,即您不能显式调用ctor。 What you can do is write a private clone method and call it in both copy ctor and assignment operator. 您可以做的是编写一个私有clone方法,并在复制ctor和赋值运算符中对其进行调用。

As you've noted, set(source); 如前所述, set(source); is the source (no pun intended) of the problem. 是问题的根源(无双关语)。 This isn't doing quite what you think it is -- it's not attempting to invoke a copy ctor. 这并没有完全按照您的想法做,也不是试图调用复制ctor。 Instead, it's basically equivalent to: set source; 相反,它基本上等效于: set source; -- ie it's attempting to define a set object named source -- the parentheses are redundant but allowed. -即它试图定义一个名为sourceset对象-括号是多余的,但允许使用。

You can invoke a copy ctor in a ctor (or just about anywhere you want to) but it's not going to to what you want anyway -- a copy ctor creates a copy, so even if you did invoke it, it would just create a temporary object, which would evaporate at the end of that statement. 您可以在ctor中(或几乎任何您想要的地方)调用复制ctor,但无论如何都不会达到您想要的内容-复制ctor会创建一个副本,因此即使您确实调用了它,它也只会创建一个副本。临时对象,该对象将在该语句的末尾蒸发。

As already mentioned, what you probably want is a private function to copy the data from one object to another, then use that from both your copy ctor and your copy assignment operator. 如前所述,您可能想要的是一个私有函数,用于将数据从一个对象复制到另一个对象,然后在复制控制器和复制赋值运算符中使用它。 Better still, define it using objects that can be handled correctly by the default copy ctor and copy assignment operators. 更好的是,使用可以由默认复制ctor和复制分配运算符正确处理的对象进行定义。

That error is usually the result of having a local variable named the same as a function argument. 该错误通常是由于具有与函数参数相同的局部变量而导致的。 Can you post more of your code? 您可以发布更多代码吗?

The error message you are seeing doesn't match the question, but I don't know that it is relevant. 您看到的错误消息与问题不符,但我不知道它是否相关。 The answer to your question is that you cannot call the copy constructor from inside your code because the object is already constructed. 问题的答案是,由于对象已被构造,因此无法从代码内部调用复制构造函数。

If you want to avoid code duplication between the copy constructor and operator=, I suggest having a private function that does the common work, and call that from both the copy constructor and operator=. 如果要避免复制构造函数和operator =之间的代码重复,建议使用一个私有函数来完成常见工作,并从复制构造函数和operator =两者中进行调用。

For reference, you could call operator= from copy constructor by doing: 作为参考,您可以通过执行以下操作从副本构造函数调用operator =:

*this = source;

However, I don't think that is a good idea especially if you have virtual functions or if the operator=() function assumes a fully constructed object (which it probably does). 但是,我认为这不是一个好主意,特别是如果您具有虚函数或operator =()函数假定一个完全构造的对象(它可能这样做)时,尤其如此。

I don't know the details of what your header file specifies, but I would try something like this (you may need to modify it for your specific application): 我不知道您的头文件指定什么的详细信息,但是我会尝试这样的操作(您可能需要针对特定​​的应用程序对其进行修改):


void set::operator =(const set& source) 无效set :: operator =(const set&source)
{ {

if (this == &source)
  {
     return;
  }

size_t i;
this->clear();
data=source.data;
for (i=0; i<source.child.size(); i++)
  {
     child.push_back(new set(*(source.child[i])));
  }  

} }


-Joel -乔尔

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

相关问题 您如何在类外部的成员函数中调用成员函数? - How do you call a member function in a member function outside of a class? 如何调用类成员的构造函数? - How do I call the constructor of a class' member? 您如何将结构作为 class 中的私有成员并为其创建参数化构造函数/设置器 function? - How do you have a struct as a private member in a class and creating a parameterized constructor/setter function for it? 如何获取指向类的复制构造函数的成员函数指针? - How can I obtain a member function pointer to the copy constructor of a class? 如何区分类成员函数和数据成员的声明 - How do you distinguish the declaration of a class member function and data member 如何从静态成员函数返回副本? - How do you return a copy from a static member function? 类成员函数内的虚拟函数调用 - virtual function call within a class member function 如何从类函数访问类构造函数变量? - How do you access a class constructor variable from a class function? 你称之为成员函数的成员函数是什么?如何编写成员函数? - What do you call a member function that follows a member function and how do I write one? 如何从另一个函数调用属于类成员的函数? - How do I call a function that is member of a class, from another function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM