标签: copy-constructor

复制构造函数

复制构造函数的想法真让我困惑.我不明白为什么他们使用引用和为什么复制构造函数中的常量引用?

c++ copy-constructor

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

复制没有参数的构造函数

写了这个小类,每次创建这个类的对象时都会生成一个UUID.

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>    

class myClass {

public:

    boost::uuids::uuid GetUUID();

    virtual ~myClass();

    myClass() {   // constructor
        mId = boost::uuids::random_generator()();
        std::cout<< "object created with uuid " << mId <<std::endl;
    }

private:

    boost::uuids::uuid mId;

}
Run Code Online (Sandbox Code Playgroud)

在某些时候,我正在将这些对象推送到向量,并使用简单赋值运算符将该向量与另一个向量等同.为了确保新向量中的对象不生成新的UUID,我想编写一个复制构造函数.但正如您所看到的,构造函数不需要参数.所以我不知道如何编写复制构造函数.此外,如果我有多个变量而不是一个UUID,我该如何处理这种情况?

c++ copy-constructor

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

为什么返回不复制对象?

以下代码仅打印A::A(),但不打印A::A(const A&)operator=.为什么?

struct A
{
  A()               { cout << "A::A()" << endl; }
  A(const A& value) { cout << "A::A(const A&)" << endl; }

  A& operator=(const A& newValut)
  {
    cout << "A::operator=" << endl; 
    return *this;
  }
};

A foo()
{
  A a;      //Ok, there we have to create local object by calling A::A().
  return a; //And there we need to copy it, otherwise it will be destroyed
            //because it's local object. But we …
Run Code Online (Sandbox Code Playgroud)

c++ return copy-constructor

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

既不复制也不移动构造函数

可能重复:
为什么在这种情况下不调用复制构造函数?
什么是复制省略和返回值优化?

任何人都可以向我解释为什么以下程序产生输出"cpy:0"(至少在使用g ++ 4.5.2编译时):

#include<iostream>

struct A {

    bool cpy;

    A() : cpy (false) {
    }

    A (const A & a) : cpy (true) {
    }

    A (A && a) : cpy (true) {
    };

};

A returnA () { return A (); }

int main() {

    A a ( returnA () );
    std::cerr << "cpy: " << a.cpy << "\n";
}
Run Code Online (Sandbox Code Playgroud)

当我试图弄清楚这个例子看似奇怪的结果时出现了问题:使用常量数据成员或引用成员移动类的ctor

c++ copy-constructor move-constructor

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

为什么shared_ptr <T>期望在T中复制/移动构造函数?

我有以下代码:

#include <memory>

using namespace std;

template<typename U> class A;

template<typename U>
class B
{
    private:
        shared_ptr<const A<U>> _a;
    public:
        B (shared_ptr<const A<U>> __a) {
            _a = __a;
        }
};

template<typename U>
class A
{
    public:
        B<U> foo () const {
            return { make_shared<const A<U>> (this) };
        }
};

int
main ()
{
    A<int> a;
    B<int> b = a.foo ();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

G ++ 4.8.0和Clang 3.3svn报告该类A没有复制或移动构造函数.例如,G ++打印以下消息:

/home/alessio/Programmi/GCC/include/c++/4.8.0/ext/new_allocator.h:120:4: error: no      matching function for call to ‘A<int>::A(const A<int>* …
Run Code Online (Sandbox Code Playgroud)

c++ templates copy-constructor shared-ptr c++11

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

如何从复制赋值运算符调用复制构造函数?

我正在实施一个链表.我写了一个拷贝构造函数:

        // --- copy constructor ---
    IntList(const IntList& last_list) {
        first = new IntNode(last_list.getFirst()->getData());
        cout << "copy first " << first->getData() << endl;

        IntNode* last_node = first;
        IntNode* node = last_list.getFirst()->getNext();
        while(node!=NULL) {
            IntNode* new_node = new IntNode(node->getData());
            last_node->setNext(new_node);
            cout << "copy " << new_node->getData()<< endl;
            last_node = new_node;
            node = node->getNext();
        }
    }
Run Code Online (Sandbox Code Playgroud)

据我了解,我的副本赋值运算符(operator=)应该有2个目标:

  • 删除当前列表.
  • 复制新列表.

我可以通过调用我已编写的析构函数来实现这两个目标,然后调用复制构造函数.我该怎么做?

c++ destructor copy-constructor assignment-operator

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

更改默认的复制构造函数C++

我在理解如何在C++中覆盖默认的复制构造函数时遇到问题.我没有收到任何编译错误.前面的例子向我展示了下面的模式.下面列出了文件HashTable.cpp和摘录Hashtable.h.

Hashtable.h

HashTable& operator=(const HashTable& other);`
Run Code Online (Sandbox Code Playgroud)

HashTable.cpp

const HashTable& HashTable::operator=(const HashTable& other) {
    std::cout << "EQUAL OPERATOR METHOD" << std::endl;
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

main.cpp中

HashTable ht1 {9};
HashTable ht2 { ht1 };
Run Code Online (Sandbox Code Playgroud)

虽然在编译时,看起来好像没有调用复制构造函数.为了澄清,我试图将一个变量复制到另一个变量.

值得注意的是,我在Ubuntu 14.04上使用c ++ 11进行编码.由于Ubuntu中的编码c ++已经有很多挂机,我不确定这是c ++还是ubuntu问题.我花了很长时间试图弄清楚这里发生了什么,所以请不要投票.

c++ copy-constructor c++11

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

即使没有任何要复制的对象,也会自动调用复制构造函数?

我在网上发现了这段代码:

    #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." << endl;
   ptr = …
Run Code Online (Sandbox Code Playgroud)

c++ constructor copy-constructor

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

将右值绑定到左值引用

我有以下C ++代码(VS2013):

#include <iostream>
using namespace std;

class A {
    int i;
public:
    A(int i) : i(i) {
        cout << "Constructor: " << i << endl;
    }
    A(const A &o) : i(o.i) {
        cout << "Copy constructor: " << i << endl;
    }
    ~A() {
        cout << "Destructor: " << i << endl;
    }
};

A test(const A &a, A b, A *c) {
    return *c;
}

int main() {
    A b(10);
    cout << "START OF TEST" << endl;
    test(1, …
Run Code Online (Sandbox Code Playgroud)

c++ constructor type-conversion copy-constructor

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

为什么unique_ptr可以工作但auto_ptr不能与STL一起使用

我在这些问题上提到了很多StackOverflow链接,其中auto_ptr不能与STL很好地配合 的原因std::auto_ptr<>不能满足可复制构造和可分配的要求(因为auto_ptr有一个伪造的复制构造函数,它基本上可以转移所有权)。

但是,甚至unique_ptr没有复制ctor和赋值运算符(已禁用),那么如何满足可复制构造和可赋值的要求呢?

c++ stl auto-ptr copy-constructor unique-ptr

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