Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

I wrote the following to try and illustrate understand what's going on:

#include <iostream> using namespace std; class myClass { public: myClass(int x) { this -> x = x; cout << "int constructor called with value x = " << x << endl; } myClass(const myClass& mc) { cout << "copy constructor called with value = " << mc.x << endl; x = mc.x; } myClass & operator = (const myClass & that) { cout << "assignment called" << endl; if(this != &that) { x = that.x; } return *this; } private: int x; }; int main() { myClass x(3); myClass y = myClass(3); } 

When I compile and run this code I get the following output:

$ ./a.out int constructor called with value x = 3 int constructor called with value x = 3 

This would seem to indicate that there is no difference between the two calls made in the main function, but that would be wrong. As litblitb pointed out, the copy constructor must be available for this code to work, even though it gets elided in this case. To prove that, just move the copy constructor in the code above to the private section of the class definition. You should see the following error:

$ g++ myClass.cpp myClass.cpp: In function ‘int main()’: myClass.cpp:27: error: ‘myClass::myClass(const myClass&)’ is private myClass.cpp:37: error: within this context 

Also note that the assignment operator is never called.

I wrote the following to try and illustrate understand what's going on:

#include <iostream> using namespace std; class myClass { public: myClass(int x) { this -> x = x; cout << "int constructor called with value x = " << x << endl; } myClass(const myClass& mc) { cout << "copy constructor called with value = " << mc.x << endl; x = mc.x; } myClass & operator = (const myClass & that) { cout << "assignment called" << endl; if(this != &that) { x = that.x; } return *this; } private: int x; }; int main() { myClass x(3); myClass y = myClass(3); } 

When I compile and run this code I get the following output:

$ ./a.out int constructor called with value x = 3 int constructor called with value x = 3 

This would seem to indicate that there is no difference between the two calls made in the main function, but that would be wrong. As litb pointed out, the copy constructor must be available for this code to work, even though it gets elided in this case. To prove that, just move the copy constructor in the code above to the private section of the class definition. You should see the following error:

$ g++ myClass.cpp myClass.cpp: In function ‘int main()’: myClass.cpp:27: error: ‘myClass::myClass(const myClass&)’ is private myClass.cpp:37: error: within this context 

Also note that the assignment operator is never called.

I wrote the following to try and illustrate understand what's going on:

#include <iostream> using namespace std; class myClass { public: myClass(int x) { this -> x = x; cout << "int constructor called with value x = " << x << endl; } myClass(const myClass& mc) { cout << "copy constructor called with value = " << mc.x << endl; x = mc.x; } myClass & operator = (const myClass & that) { cout << "assignment called" << endl; if(this != &that) { x = that.x; } return *this; } private: int x; }; int main() { myClass x(3); myClass y = myClass(3); } 

When I compile and run this code I get the following output:

$ ./a.out int constructor called with value x = 3 int constructor called with value x = 3 

This would seem to indicate that there is no difference between the two calls made in the main function, but that would be wrong. As litb pointed out, the copy constructor must be available for this code to work, even though it gets elided in this case. To prove that, just move the copy constructor in the code above to the private section of the class definition. You should see the following error:

$ g++ myClass.cpp myClass.cpp: In function ‘int main()’: myClass.cpp:27: error: ‘myClass::myClass(const myClass&)’ is private myClass.cpp:37: error: within this context 

Also note that the assignment operator is never called.

deleted 1 characters in body
Source Link
Bill the Lizard
  • 407.3k
  • 213
  • 579
  • 892

I wrote the following to try and illustrate understand what's going on:

#include <iostream> using namespace std; class myClass { public: myClass(int x) { this -> x = x; cout << "int constructor called with value x = " << x << endl; } myClass(const myClass& mc) { cout << "copy constructor called with value = " << mc.x << endl; x = mc.x; } myClass & operator = (const myClass & that) { cout << "assignment called" << endl; if(this != &that) { x = that.x; } return *this; } private: int x; }; int main() { myClass x(3); myClass y = myClass(3); } 

When I compile and run this code I get the following output:

s$$ ./a.out int constructor called with value x = 3 int constructor called with value x = 3 

This would seem to indicate that there is no difference between the two calls made in the main function, but that would be wrong. As litb pointed out, the copy constructor must be available for this code to work, even though it gets elided in this case. To prove that, just move the copy constructor in the code above to the private section of the class definition. You should see the following error:

$ g++ myClass.cpp myClass.cpp: In function ‘int main()’: myClass.cpp:27: error: ‘myClass::myClass(const myClass&)’ is private myClass.cpp:37: error: within this context 

Also note that the assignment operator is never called.

I wrote the following to try and illustrate understand what's going on:

#include <iostream> using namespace std; class myClass { public: myClass(int x) { this -> x = x; cout << "int constructor called with value x = " << x << endl; } myClass(const myClass& mc) { cout << "copy constructor called with value = " << mc.x << endl; x = mc.x; } myClass & operator = (const myClass & that) { cout << "assignment called" << endl; if(this != &that) { x = that.x; } return *this; } private: int x; }; int main() { myClass x(3); myClass y = myClass(3); } 

When I compile and run this code I get the following output:

s$ ./a.out int constructor called with value x = 3 int constructor called with value x = 3 

This would seem to indicate that there is no difference between the two calls made in the main function, but that would be wrong. As litb pointed out, the copy constructor must be available for this code to work, even though it gets elided in this case. To prove that, just move the copy constructor in the code above to the private section of the class definition. You should see the following error:

$ g++ myClass.cpp myClass.cpp: In function ‘int main()’: myClass.cpp:27: error: ‘myClass::myClass(const myClass&)’ is private myClass.cpp:37: error: within this context 

Also note that the assignment operator is never called.

I wrote the following to try and illustrate understand what's going on:

#include <iostream> using namespace std; class myClass { public: myClass(int x) { this -> x = x; cout << "int constructor called with value x = " << x << endl; } myClass(const myClass& mc) { cout << "copy constructor called with value = " << mc.x << endl; x = mc.x; } myClass & operator = (const myClass & that) { cout << "assignment called" << endl; if(this != &that) { x = that.x; } return *this; } private: int x; }; int main() { myClass x(3); myClass y = myClass(3); } 

When I compile and run this code I get the following output:

$ ./a.out int constructor called with value x = 3 int constructor called with value x = 3 

This would seem to indicate that there is no difference between the two calls made in the main function, but that would be wrong. As litb pointed out, the copy constructor must be available for this code to work, even though it gets elided in this case. To prove that, just move the copy constructor in the code above to the private section of the class definition. You should see the following error:

$ g++ myClass.cpp myClass.cpp: In function ‘int main()’: myClass.cpp:27: error: ‘myClass::myClass(const myClass&)’ is private myClass.cpp:37: error: within this context 

Also note that the assignment operator is never called.

added 28 characters in body
Source Link
Bill the Lizard
  • 407.3k
  • 213
  • 579
  • 892

I wrote the following to try and illustrateillustrate understand what's going on:

#include <iostream> using namespace std; class myClass { public: myClass(int x) { this -> x = x; cout << "int constructor called with value x = " << x << endl; } myClass(const myClass& mc) { cout << "copy constructor called with value = " << mc.x << endl; x = mc.x; } myClass & operator = (const myClass & that) { cout << "assignment called" << endl; if(this != &that) { x = that.x; } return *this; } private: int x; }; int main() { myClass x(3); myClass y = myClass(3); } 

When I compile and run this code I get the following output:

s$ ./a.out int constructor called with value x = 3 int constructor called with value x = 3 

This would seem to indicate that there is no difference between the two calls made in the main function, but that would be wrong. As litb pointed out, the copy constructor must be available for this code to work, even though it gets elided in this case. To prove that, just move the copy constructor in the code above to the private section of the class definition. You should see the following error:

$ g++ myClass.cpp myClass.cpp: In function ‘int main()’: myClass.cpp:27: error: ‘myClass::myClass(const myClass&)’ is private myClass.cpp:37: error: within this context 

Also note that the assignment operator is never called.

I wrote the following to try and illustrate what's going on:

#include <iostream> using namespace std; class myClass { public: myClass(int x) { this -> x = x; cout << "int constructor called with value x = " << x << endl; } myClass(const myClass& mc) { cout << "copy constructor called with value = " << mc.x << endl; x = mc.x; } myClass & operator = (const myClass & that) { cout << "assignment called" << endl; if(this != &that) { x = that.x; } return *this; } private: int x; }; int main() { myClass x(3); myClass y = myClass(3); } 

When I compile and run this code I get the following output:

s$ ./a.out int constructor called with value x = 3 int constructor called with value x = 3 

This would seem to indicate that there is no difference between the two calls made in the main function, but that would be wrong. As litb pointed out, the copy constructor must be available for this code to work, even though it gets elided in this case. To prove that, just move the copy constructor in the code above to the private section of the class definition. You should see the following error:

$ g++ myClass.cpp myClass.cpp: In function ‘int main()’: myClass.cpp:27: error: ‘myClass::myClass(const myClass&)’ is private myClass.cpp:37: error: within this context 

Also note that the assignment operator is never called.

I wrote the following to try and illustrate understand what's going on:

#include <iostream> using namespace std; class myClass { public: myClass(int x) { this -> x = x; cout << "int constructor called with value x = " << x << endl; } myClass(const myClass& mc) { cout << "copy constructor called with value = " << mc.x << endl; x = mc.x; } myClass & operator = (const myClass & that) { cout << "assignment called" << endl; if(this != &that) { x = that.x; } return *this; } private: int x; }; int main() { myClass x(3); myClass y = myClass(3); } 

When I compile and run this code I get the following output:

s$ ./a.out int constructor called with value x = 3 int constructor called with value x = 3 

This would seem to indicate that there is no difference between the two calls made in the main function, but that would be wrong. As litb pointed out, the copy constructor must be available for this code to work, even though it gets elided in this case. To prove that, just move the copy constructor in the code above to the private section of the class definition. You should see the following error:

$ g++ myClass.cpp myClass.cpp: In function ‘int main()’: myClass.cpp:27: error: ‘myClass::myClass(const myClass&)’ is private myClass.cpp:37: error: within this context 

Also note that the assignment operator is never called.

Source Link
Bill the Lizard
  • 407.3k
  • 213
  • 579
  • 892
Loading