写了这个小类,每次创建这个类的对象时都会生成一个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,我该如何处理这种情况?
以下代码仅打印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) 任何人都可以向我解释为什么以下程序产生输出"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
我有以下代码:
#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) 我正在实施一个链表.我写了一个拷贝构造函数:
// --- 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++中覆盖默认的复制构造函数时遇到问题.我没有收到任何编译错误.前面的例子向我展示了下面的模式.下面列出了文件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问题.我花了很长时间试图弄清楚这里发生了什么,所以请不要投票.
我在网上发现了这段代码:
#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 ++代码(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) 我在这些问题上提到了很多StackOverflow链接,其中auto_ptr不能与STL很好地配合
的原因std::auto_ptr<>不能满足可复制构造和可分配的要求(因为auto_ptr有一个伪造的复制构造函数,它基本上可以转移所有权)。
但是,甚至unique_ptr没有复制ctor和赋值运算符(已禁用),那么如何满足可复制构造和可赋值的要求呢?
c++ ×10
copy-constructor ×10
c++11 ×2
constructor ×2
auto-ptr ×1
destructor ×1
return ×1
shared-ptr ×1
stl ×1
templates ×1
unique-ptr ×1