标签: copy-constructor

std :: containers调用的复制构造函数是否包含复制构造函数?

包含对象指针的任何std :: container(特别是std :: queue)的复制构造函数是否调用成员的复制构造函数以允许深层复制,或者它是否对指针值执行浅复制?

例:

/*******************************************************************************
 * <summary>
 *  Initializes a new instance of the EventHandler class.
 * </summary>
 *
 * <param name="handler">The handler to copy.</param>
*******************************************************************************/
EventHandler::EventHandler(const EventHandler& handler) : _eventQueue(handler._eventQueue) { }
Run Code Online (Sandbox Code Playgroud)

_eventQueue声明为:std::queue<Event*> _eventQueue;其中Event是具有复制构造函数的Base类,并且具有多个具有自己的复制构造函数的派生类.

PS:我喜欢AtomineerUtilsVisualAssistX(尤其是合并时!:D)

编辑:

鉴于下面的答案,这是一个正确的方式来创建原件的副本,使原件未经修改或复制是否与原件相反(简单的修复,但仍然是一个重要的区别)?

EventHandler::EventHandler(const EventHandler& handler) {
    for(size_t i = 0; i < handler._eventQueue.size(); ++i) {
        this->_eventQueue.push(new Event(handler._eventQueue._Get_container().at(i)));
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ containers std deep-copy copy-constructor

1
推荐指数
1
解决办法
1954
查看次数

从复制构造函数调用2种不同类型的构造函数

请考虑以下示例代码:

#include <iostream>

using namespace std;

class core
{
   public:
      core(const core& obj)
      {
         cout << "core copy ctor called\n";
      }
      core()
      {
         cout << "core default ctor called\n";
      }
};

class sample : public core
{
   public:
      sample()
      {
         cout << "sample default ctor called\n";
      }
#if 0
      sample(const sample& obj)
      {
         cout << "sample copy ctor called\n";
      }
#endif
};

int main()
{
   sample s1;
   sample s2 = s1; //Line1
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

Type1:未为类示例显式声明的复制构造函数

(Type1显示在上面的代码中.然后编译器隐式生成类sample的复制构造函数).Line1执行语句时,首先class …

c++ copy-constructor default-constructor implicit-declaration

1
推荐指数
1
解决办法
729
查看次数

复制构造函数接受const引用的行为

在以下代码中,在此行中

A(A&b)

使用此编译器时会出现错误

c110.cpp:41:错误:没有匹配函数来调用'A :: A(A)'

c110.cpp:8:注意:候选人是:A :: A(A&)

但只要我把它转换成

A(const A&b)

提前许多不止于此

没有错误.为什么会这样?

Code
class A
{
    public: 
    static int cnt;
    A(A& b)
    {
       cnt++;
       cout<<"cnt="<<cnt<<endl;
    }
    A()
    {
       cnt++;
       cout<<"cnt="<<cnt<<endl;
    }
    ~A()
    {
       cnt--;
       cout<<"cnt="<<cnt<<endl;
    }
};



  int A :: cnt=0;


  A fun(A b)
  {
  return b;
  }


 int main()
 {
     A a;
     A b=fun(a);
     return 0;
 }
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor

1
推荐指数
1
解决办法
168
查看次数

复制构造函数理解

我正在通过这个再次研究复制构造函数.

码:

#include <iostream>

using namespace std;

class Line
{
   public:
      int getLength( void );
      Line( int len );             // simple constructor
      Line( const Line &obj);  // copy constructor
      ~Line();                     // destructor

   private:
      int *ptr;
};

// Member functions definitions including constructor
Line::Line(int len)
{
    cout << "Normal constructor allocating ptr" << endl;
    // allocate memory for the pointer;
    ptr = new int;
    *ptr = len;
}

Line::Line(const Line &obj)
{
    cout << "Copy constructor allocating ptr." << …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor

1
推荐指数
1
解决办法
249
查看次数

unique_ptr,移动构造函数,以及为什么总是尝试访问私有成员

我经常遇到这个问题,我相信移动构造函数是有序的,但我认为复制构造函数是问题,隐藏它似乎不起作用.

代码:

template <class T>
class LinkedList{
public:
    //
    LinkedList() {}
    LinkedList(const T &data);
    LinkedList(const T &data, const LinkedList &node);
    LinkedList(const LinkedList &object);
    LinkedList &operator=(const LinkedList &object);

    ~LinkedList() {}

    std::shared_ptr<LinkedList> push_back(const T& data);

private:
    T data;
    std::unique_ptr<LinkedList> link;

    std::unique_ptr<LinkedList> LinkFactory(const LinkedList &node);

    std::shared_ptr<LinkedList> CreateStartNode(const T &data);
    std::shared_ptr<LinkedList> CreateNode(const T &data, const LinkedList &node);
};
Run Code Online (Sandbox Code Playgroud)

发生错误的特定行是:

LinkedList<T>::LinkedList(const LinkedList<T> &object) : data(object.data),  
link(std::move(object.link)) {}
Run Code Online (Sandbox Code Playgroud)

我试图移动而不是复制复制构造函数中的链接无济于事.如果设计移动构造函数而不是合成,那会更好吗?

c++ copy-constructor unique-ptr move-constructor c++11

1
推荐指数
1
解决办法
872
查看次数

具有动态分配数组的类的C ++复制构造函数

刚开始使用C ++(几天后),我就有C背景。

我有一个类,主要包含一个指向带有以下代码的int数组的指针:

class Array
{
private:
    int * _arr;
    int _size;
public:
    Array();
    Array(int size);
    Array(const Array& obj);  // copy constructor
    ~Array();
    void readInValues();
    void mult(int num);
    void add(int num);
    void printArray();
};
Run Code Online (Sandbox Code Playgroud)

_arr是指向int数组的指针,当使用复制构造函数创建新实例时,我会在堆上创建一个新的int数组(我认为)。在复制构造函数中:

Array::Array( const Array & obj )
{
    _arr = new int[_size];

    for(int i=0;i<_size;i++)
        *_arr[i] = *obj._arr[i];
}
Run Code Online (Sandbox Code Playgroud)

我要做的第一件事是为新数组分配内存(_size是原始类型,因此据我所知会自动复制)。我想做的下一件事是使用循环复制数组本身。该部分无法通过编译说非法间接。我不确定为什么...

c++ arrays copy-constructor dynamic-allocation

1
推荐指数
1
解决办法
2万
查看次数

复制构造函数调用方法

#include<iostream>
using namespace std ; 
class Foo{
    int a , b ;
public:
    Foo(int x, int y){
        a = x ;
        b = y  ;
    }
    Foo(Foo& obj){
        a = obj.a ;
        b = obj.b ;
    }
};

int main(){
    Foo obj(2,3) ;
    Foo obj1(obj) ;
    Foo obj2 = obj ;
}
Run Code Online (Sandbox Code Playgroud)

如果我是正确的,都Foo obj2 = obj ;Foo obj1(obj) ;调用拷贝构造函数.

使用其中一个的优点和缺点是什么?

c++ initialization copy-constructor

1
推荐指数
1
解决办法
92
查看次数

复制包含另一个对象的超类的对象

我想创建一个包含另一个对象的超类的对象的副本.在这个例子中,我想制作一个Box包含a 的副本Toy.但是所有类型的玩具都可以装在盒子里.创建复制构造函数的最佳方法是Toy什么?

class Box {
    Toy toy;
    public Box(Toy toy) {
        this.toy = toy;
    }
    public Box(Box box) {
        this.toy = new Toy(box.getToy());
    }
}
abstract class Toy {
    public Toy(String name) {
        // ...
    }
}   
class Car extends Toy {
    public Car(String name) {
        super(name);
        // ...
    }
}
class Puppet extends Toy {
    public Puppet(String name) {
        super(name);
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

我真的不知道如何解决这个问题.

java oop copy-constructor

1
推荐指数
1
解决办法
125
查看次数

检查char指针是否为null - 复制构造函数

我正在处理以下代码

class base
{
private:
    char* mycharpointer;
    std::string mystring;

public:
    base() : mycharpointer(NULL) {/*default constructor*/}

    //Copy Constructor
    base(const base& rhs){
        if(mycharpointer != NULL)  ---> Why is this condition true ?
        {
            mycharpointer = new char[ strlen(rhs.mycharpointer + 1)];
            strcpy(this->mycharpointer,rhs.mycharpointer);
        }
        mystring = rhs.mystring;
    }


    base operator=(base& b)
    {   
        if(this == &b) 
            return *this;

        base temp(b); 
        temp.swap(*this);
        return *this;
    }

    //Swap operation
    void swap(base& lhs) {
        std::swap(lhs.mycharpointer,this->mycharpointer);
        std::swap(lhs.mystring,this->mystring);
    }

    //Destructor
    virtual ~base(){
        if(mycharpointer) 
            delete[] mycharpointer;
    }
};

class der : public …
Run Code Online (Sandbox Code Playgroud)

c++ constructor initialization copy-constructor

1
推荐指数
1
解决办法
620
查看次数

转换运算符(assignment operator =)没有响应

我一直在尝试测试对我的结构的赋值运算符=的调用:

struct array{

    void* data;

    template<typename S, typename T>
    array& operator= (const map<S, T>& that){ cout << "worked...";  return *this;}

   private:
     array();           //i don't need this
};
Run Code Online (Sandbox Code Playgroud)

我像这样试驾:

map<int, string> var;
array arr = var;
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

Error: conversion from
'std::map<int, string, std::less<int>, std::allocator<std::pair<const int, string> > >'
to non-scalar type 'array' requested*/ 
Run Code Online (Sandbox Code Playgroud)

问题:到底是什么问题?如何使这样的运算符超载?我的意思是 operator=,它应该将不同类型的对象转换为自己的类类型。

c++ operator-overloading copy-constructor

1
推荐指数
1
解决办法
128
查看次数