Question:
C++: Can a copy constructor be overloaded?
1970-01-01 00:00:00 UTC
C++: Can a copy constructor be overloaded?
Three answers:
flintroy
2017-01-18 08:25:25 UTC
type something{ public: something; //default +or something( const something& cloneMe ){ this->abc = cloneMe.abc }// something( const int& setThisAttribute ){ this->abc=setThisAttribute; } //records int abc; }; //so... something x; //makes use of the default +or to create x something y(x); //makes use of your replica +or to create a y this is a in basic terms like x something z(777); //makes use of the parametrized +or to set some value up in z
?
2011-05-18 22:31:09 UTC
I found this article interesting. This might be the answer to your question:

http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/
?
2011-05-19 06:20:28 UTC
Yes. A copy constructor for class T is, by definition (12.8[class.copy]/2 in the standard), a non-template constructor with a first parameter of type T& or of type const T& or of type volatile T& or of type const volatile T&. If it has more than one parameter, all other parameters must have default values.



This means that the following class has four overloaded copy-constructors:



#include

struct T

{

     T() {};

     T(T&) { std::cout << "copy ctor 1\n";}

     T(const T&) { std::cout << "copy ctor 2\n";}

     T(volatile T&) { std::cout << "copy ctor 3\n";}

     T(const volatile T&, int=1) { std::cout << "copy ctor 4\n";}

};

int main()

{

     T t;

     const T ct;

     volatile T vt;

     T t2 = t;

     T t3 = T(ct);

     T t4(vt);

     T t5 = const_cast(vt);

}



test: https://ideone.com/smGQv


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...