C++ interview question: Copy Constructor vs assignment operator with const

(Last Updated On: May 15, 2010)

C++ interview question: Copy Constructor vs assignment operator with const

Structures and classes are the same. The only difference is that structures are defaulted at being public while classes are defaulted at being private. Structures are a C feature so they do not have any C++ features like inheritance.
Copy constructors can overload the assignment operator to allow deep copying instead of default shallow coping. This invoked when one object is initialized to make another.
Both copy constructor and assignment operator are the same where they perform shallow copies by default. As said, you can override the default copy constructor or overload the assignment for deep copies. An KEY DIFFERENCE sample is:
sample x1=x2; //call copy constructor
sample x1;
x1=x2l //calls assignment operator
Copy constructors need a const so that the constructor does not change the state of where it is copying from. This means it cannot be altered within the function. If a function attempts to change the value, the program will not compile. It is advised to use a const in very constructor parameter. Be advised of this potential compiler error:
Foo::Foo(Foo&) {} /constructor without const
const Foo a;
Foo b(a);// error
In a nutshell remember,
1. Copy-Constructor without ‘const’ compiles.
2. Copy-Constructor without ‘const’ is called.
3. ‘const’ in CC prevents passd object from being modified.

There are numerous reasons why you should never call a virtual method from a derived class constructor. This loses its meaning as it will case its base implementation instead.

Also, you should note order of execution in this code:
using namespace std;

class Base{
Base(){cout << "Base()" << endl;} Base(const Base&){cout << "Base(const Base&)" << endl;} ~Base(){cout << "~Base()" << endl;} }; Base func(Base b){return b;} void test1(){ Base a; func(a); } void test2(){ Base a; Base b = func(a); } int main(){ test1(); test2(); return 0; } Output is: Test1 Base() Base(const Base&) Base(const Base&) ~Base() ~Base() ~Base() Test2 Base() Base(const Base&) Base(const Base&) ~Base() ~Base() ~Base() Reasons are: Test1 Step-1 Default constructor will be called at Base a; Step-2 Copy constructor will be called when passing a as formal parameter i.e func(a). Step-3 Copy constructor will be called for return object. Step-4 Destructor will be called for actual parameter of func. Step-5 Now this return obj will act as nameless obj in test1() and will get destroyed after line func(a) calling destructor one more time. Step-6 Finally Destructor will be called for a. You can see same for test2();

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

Subscribe For Latest Updates

Sign up to best of business news, informed analysis and opinions on what matters to you.
Invalid email address
We promise not to spam you. You can unsubscribe at any time.


Check NEW site on stock forex and ETF analysis and automation

Scroll to Top