copy-constructor

constructor invocation mechanism

 ̄綄美尐妖づ 提交于 2019-11-26 09:52:43
问题 struct my { my(){ std::cout<<\"Default\";} my(const my& m){ std::cout<<\"Copy\";} ~my(){ std::cout<<\"Destructor\";} }; int main() { my m(); //1 my n(my()); //2 } Expected output : 1 ) Default 2 ) Copy Actual output : What\'s wrong with my understanding of the constructor invoking mechanism? Note I have omitted header files for brevity. 回答1: Case 1) m is interpreted as a function return my and taking no arguments. To see the expected output remove () i.e use my m; Case 2) This is something

Disable copy constructor

允我心安 提交于 2019-11-26 09:17:15
问题 I have a class : class SymbolIndexer { protected: SymbolIndexer ( ) { } public: static inline SymbolIndexer & GetUniqueInstance ( ) { static SymbolIndexer uniqueinstance_ ; return uniqueinstance_ ; } }; How should I modify it to disable code like: SymbolIndexer symbol_indexer_ = SymbolIndexer::GetUniqueInstance ( ); and only allow code like : SymbolIndexer & ref_symbol_indexer_ = SymbolIndexer::GetUniqueInstance ( ); 回答1: You can make the copy constructor private and provide no implementation

Copy constructor is not inherited

我是研究僧i 提交于 2019-11-26 09:12:15
问题 I\'ve got the following code: class C { public: C(int) {} C(const C&) {} C() {} }; class D : public C { public: using C::C; }; int main() { C c; D d_from_c(c); // does not compile, copy ctor is not inherited D d_from_int(1); // compiles, C(int) is inherited } Derived class should inherit all ctors of base except the default ctor (it is explained here). But why copy ctor is not inherited as well? Arguments from the related question are not acceptable here. The code is compiled with g++ 4.8.1.

Why is the copy constructor not called?

限于喜欢 提交于 2019-11-26 08:24:44
问题 class MyClass { public: ~MyClass() {} MyClass():x(0), y(0){} //default constructor MyClass(int X, int Y):x(X), y(Y){} //user-defined constructor MyClass(const MyClass& tempObj):x(tempObj.x), y(tempObj.y){} //copy constructor private: int x; int y; }; int main() { MyClass MyObj(MyClass(1, 2)); //user-defined constructor was called. MyClass MyObj2(MyObj); //copy constructor was called. } In the first case, when MyClass(1, 2) calls the user-defined constructor and returns an object, I was

How are C++ array members handled in copy control functions?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 07:34:52
问题 This is something I have wondered for a long time. Take the following example: struct matrix { float data[16]; }; I know what the default constructor and destructor do in this specific example (nothing), but what about the copy constructor and the copy assignment operator? struct matrix { float data[16]; // automatically generated copy constructor matrix(const matrix& that) : // What happens here? { // (or here?) } // automatically generated copy assignment operator matrix& operator=(const

Why do we need copy constructor and when should we use copy constructor in java

假装没事ソ 提交于 2019-11-26 07:33:40
问题 I was going through Copy Constructors, I have gone through the links in stack over flow and others as well. But i am not clear on the following points. Why do we need a Copy Constructor When do we need a Copy Constructor I mean what is the exact situation or scenario we would need to use Copy Constructor. Can some one explain with an example or point out links so that i can go through and understand them in clear. Following are the links i have gone through to get an understanding of what is

The copy constructor and assignment operator

…衆ロ難τιáo~ 提交于 2019-11-26 07:28:12
问题 If I override operator= will the copy constructor automatically use the new operator? Similarly, if I define a copy constructor, will operator= automatically \'inherit\' the behavior from the copy constructor? 回答1: No, they are different operators. The copy constructor is for creating a new object. It copies a existing object to a newly constructed object.The copy constructor is used to initialize a new instance from an old instance. It is not necessarily called when passing variables by

Why is this code trying to call the copy constructor?

南笙酒味 提交于 2019-11-26 06:48:01
问题 I just spent an inordinate amount of time fiddling with a complilation error in Visual Studio. I have distilled the code into the small compilable example below and tried it on IdeOne and got the same error which you can see here. I am wondering why the following code tries to call B(const B&) instead of B(B&&) : #include <iostream> using namespace std; class A { public: A() : data(53) { } A(A&& dying) : data(dying.data) { dying.data = 0; } int data; private: // not implemented, this is a

Why user-defined move-constructor disables the implicit copy-constructor?

与世无争的帅哥 提交于 2019-11-26 06:30:58
问题 While I\'m reading boost/shared_ptr.hpp, i saw this code: // generated copy constructor, destructor are fine... #if defined( BOOST_HAS_RVALUE_REFS ) // ... except in C++0x, move disables the implicit copy shared_ptr( shared_ptr const & r ): px( r.px ), pn( r.pn ) // never throws { } #endif What does the comment \"generated copy constructor, destructor are fine except in C++11, move disables the implicit copy\" mean here? Shall we always write the copy ctor ourselves to prevent this situation

std::thread pass by reference calls copy constructor

自作多情 提交于 2019-11-26 05:57:15
问题 Well I have an issue with passing data into a thread using std::thread. I thought I understood the general semantics of copy constructors, etc. but it seems I don\'t quite grasp the problem. I have a simple class called Log that has hidden it\'s copy constructor thusly: class Log { public: Log(const char filename[], const bool outputToConsole = false); virtual ~Log(void); //modify behavior void appendStream(std::ostream *); //commit a new message void commitStatus(const std::string str);