copy-constructor

boost::shared_ptr boost::mutex and copy constructor

送分小仙女□ 提交于 2020-01-04 03:48:50
问题 I need to protect the access to a data structure in my class. As I can't have mutex (because I can't copy it) I am considering to have shared_ptr and keep the mutex there. Here is a sample code of my idea: class Sample { typedef boost::lock_guard<boost::mutex> AcquireLock; boost::shared_ptr<boost::mutex> mutt; public: Sample() : mutt(new boost::mutex) {} void Method() { AcquireLock lock(*mutt); //do some work here } }; I've got the following questions: Is it a bad practice to use the mutex

Does binding temporary to a reference require a copy constructor in C++?

寵の児 提交于 2020-01-03 08:38:29
问题 Consider the following code: class A { A(const A&); public: A() {} }; int main() { const A &a = A(); } This code compiles fine with GCC 4.7.2, but fails to compile with Visual C++ 2010 with the following error: test.cc(8) : error C2248: 'A::A' : cannot access private member declared in class 'A' test.cc(2) : see declaration of 'A::A' test.cc(1) : see declaration of 'A' So is it necessary to have a copy constructor accessible when binding a temporary to a reference? This is somewhat related to

Does deleting a copy constructor or copy assignment operator count as “user declared”?

六眼飞鱼酱① 提交于 2020-01-02 03:25:11
问题 Per this presentation, if either the copy constructor or copy assignment operator is "user declared", then no implicit move operations will be generated. Does delete ing the copy constructor or copy assignment operator count as "user declared"? struct NoCopy { NoCopy(NoCopy&) = delete; NoCopy& operator=(const NoCopy&) = delete; }; Will implicit move operations be generated for the NoCopy class? Or does deleting the relevant copy operations count as "user declared" and thus inhibit implicit

C++, how to correctly copy std::vector<Class *> in copy constructor?

血红的双手。 提交于 2020-01-02 01:46:27
问题 I'm using this two classes // This is generic data structure containing some binary data class A { public: A(); A(const A&); ~A(); } // Main data container class B { public: B(); B( const B&); ~B(); protected: std::vector<A *> data; } // Copy constructor for class b B::B( const B& orig):data() { for( std::vector<A *>::const_iterator it = orig.data.begin(); it < orig.data.end(); ++it){ data.push_back( new A( *(*it))); } } I guess this class would do it job, but I'm finding to way how to reach

Copy constructor not called when initializing an object with return value of a function

北城以北 提交于 2019-12-31 22:23:41
问题 Consider the following code: #include <iostream> using namespace std; class A { public: int a; A(): a(5) { cout << "Constructor\n"; } A(const A &b) { a = b.a; cout << "Copy Constructor\n"; } A fun(A a) { return a; } }; int main() { A a, c; A b = a.fun(c); return 0; } The output of the above code with g++ file.cpp is: Constructor Constructor Copy Constructor Copy Constructor The output of the above code with g++ -fno-elide-constructors file.cpp is: Constructor Constructor Copy Constructor Copy

C++ copy constructor using pointers

只谈情不闲聊 提交于 2019-12-31 14:37:07
问题 Can anyone explain the meaning of *p=*q in this C++ code? Is this a copy constructor concept? class A{ //any code } int main(){ A *p=new A(); A *q=new A(); *p=*q; return 0; } 回答1: Is this a copy constructor concept? No, what you are referring to is a copy assignment concept. Consider this: int* p = new int{9}; int* q = new int{10}; *p = *q; As you can see above, only the value of the variable q which is pointed to is copied. This is the equivalent of the copy assignment for objects. If you

C++ copy constructor using pointers

不问归期 提交于 2019-12-31 14:34:56
问题 Can anyone explain the meaning of *p=*q in this C++ code? Is this a copy constructor concept? class A{ //any code } int main(){ A *p=new A(); A *q=new A(); *p=*q; return 0; } 回答1: Is this a copy constructor concept? No, what you are referring to is a copy assignment concept. Consider this: int* p = new int{9}; int* q = new int{10}; *p = *q; As you can see above, only the value of the variable q which is pointed to is copied. This is the equivalent of the copy assignment for objects. If you

The copy constructor creates dependent copy

落花浮王杯 提交于 2019-12-31 03:51:15
问题 I implemented the copy constructor as it is described here. But still the problem is that when I update route_copy , then the same update is applied to route . So, I don't understand what is wrong in my code? public class Route implements Comparable<Route> { private List<Site> sites; public Route() { sites = new ArrayList<Site>(); } public Route(List<Site> sites) { this.sites = sites; } /** * Copy constructor */ public Route(Route r) { this(r.sites); } public void deleteSite(Site s) { this

Why and when delete copy constructor and operator=

拥有回忆 提交于 2019-12-30 11:09:25
问题 As a c++ newbe I wondered why it is usefull to explicitely 'disable' or delete the = operator and copy constructor of a class: SomeClass& operator=(SomeClass&) = delete; SomeClass(SomeClass&) = delete; I guess this makes sence if the class is a singleton. But are there any other situations? (Maybe this has something to do with performance issues?) 回答1: This has nothing to do with performance. You disallow copying whenever it does not make sense to copy your class, i.e. if it is not clear what

Exception to the Rule of Three?

╄→гoц情女王★ 提交于 2019-12-30 02:48:12
问题 I've read a lot about the C++ Rule of Three. Many people swear by it. But when the rule is stated, it almost always includes a word like "usually," "likely," or "probably," indicating that there are exceptions. I haven't seen much discussion of what these exceptional cases might be -- cases where the Rule of Three does not hold, or at least where adhering to it doesn't offer any advantage. My question is whether my situation is a legitimate exception to the Rule of Three. I believe that in