Doubts on C++ copy constructor concept


 
Thread Tools Search this Thread
Top Forums Programming Doubts on C++ copy constructor concept
# 1  
Old 03-11-2011
Doubts on C++ copy constructor concept

Hi,

If I run the following program

Code:
class A {
public:
    A()  { cout << "default" << endl; }
    A(const A&) { cout << "copy" << endl; }
};

A tmp; 
A fun() {
    return tmp;
}

A test() {
    A tmp;
    cout << &tmp << endl;
    return tmp;
}

int main() {
    A a = fun();
    A b = test();
    cout << &b << endl;
}

I am getting the following output:

default
copy
default
0xbfbe1792
0xbfbe1792

My doubts are:

1) Why 'const' keyword is so necessary (without which compiler is throwing error) here? why it is not optional and why compiler is stressing to add it which is not happening in normal times?

2) Copy constructor is called when an object is returned by value from a function, as it is one of the place where copy constructor should be called. But, why it is not happening during the second object b's creation?

3) In the above code the output shows the same address on the stack for the 2 objects, but the same code in C using structures instead of classes shows a different address. I know local variables (and objects) inside a function are created in the stack and getting destroyed after the function call. Hence, theoretically it should point to different addresses, with a local copy for each one. But, why in C++, it is holding the same address??
# 2  
Old 03-11-2011
Quote:
Originally Posted by royalibrahim
1) Why 'const' keyword is so necessary (without which compiler is throwing error) here? why it is not optional and why compiler is stressing to add it which is not happening in normal times?
It's a sanity check to help guarantee that your copy constructor won't have any weird side-effects. You're passing a reference into it, which is handy since no copy's made -- very bad juju to use a copy constructor inside a copy constructor! -- but they don't want to let you change the original object, just read from it! So they demand a 'const' from you as a sign of good faith. Smilie
Quote:
2) Copy constructor is called when an object is returned by value from a function, as it is one of the place where copy constructor should be called. But, why it is not happening during the second object b's creation?
Good question. For some reason the scope of the variable seems to change what constructor it uses. Perhaps it's optimizing the extra copy away.
Quote:
3) In the above code the output shows the same address on the stack for the 2 objects
That's really, really weird. Hmmm...
Quote:
...but the same code in C using structures instead of classes shows a different address. I know local variables (and objects) inside a function are created in the stack and getting destroyed after the function call. Hence, theoretically it should point to different addresses, with a local copy for each one. But, why in C++, it is holding the same address??
I figured it out! The optimizer noticed that you were never doing anything with the objects and optimized the function call away, pasting it inside your code, like this:
Code:
int main() {
    A a = fun();
    A b;
    cout << &b << endl;
    cout << &b << endl;
}

They call that "inlining". g++ seems to do a lot of aggressive inlining by default and nothing I've found actually, really turns it off here, from -O0 up to and including __attribute__ ((noinline)) and the asm("") inline-assembly kludge.

Last edited by Corona688; 03-11-2011 at 12:24 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Programming

Constructor?

I am learning about C++ and today am reading concepts for Constructor but it seems a bit difficult to grab it fully. Please anyone explain in simple words about Constructor? (1 Reply)
Discussion started by: ggiwebsinfo
1 Replies

2. Programming

c++ object constructor question

I have the following code class Param{ public: Param(int aa, int bb){ a=aa; b=bb; } int a,b; }; void function(Param); int main(){ function(2,3); return 0; } (6 Replies)
Discussion started by: santiagorf
6 Replies

3. Programming

Doubt regarding Copy Constructor and return value

Hi All, I have made the simple following program :- #include <string> #include <iostream> using namespace std; class A{ private: int val; public : A(){cout<<"In A()"<<endl;} A (const A& aa) { cout<<"In copy c'tor"<<endl; } }; A f(... (1 Reply)
Discussion started by: shubhranshu
1 Replies

4. Programming

how do you handle a constructor and destructor that fail

helo i m new in c++ on linux can u tell me with an simple example that how do you handle constructor and destructor that fail? Regards, Amit (4 Replies)
Discussion started by: amitpansuria
4 Replies

5. Programming

what is diff b/w copy constructor and overloaded assignment operator

Helo i m new in c++. i m confuse about what is exact difference b/w copy constructor and overloaded assignment operator. Regards, Amit (3 Replies)
Discussion started by: amitpansuria
3 Replies

6. Programming

why constructor cannot be virtual

helo i read many books but i cant find the proper answer that why constructor cannot be virtual can u explain me in simple term that why constructor cannot be virtual Regards, Amit (2 Replies)
Discussion started by: amitpansuria
2 Replies

7. UNIX for Dummies Questions & Answers

Constructor problem

Hi guys I am new to these forums but since I am taking a class at college I would appreciate any help that is possible for this program. My instructor said that when its complete the program should be able to store all 3 fields instead of just 1. public class Greeter2Test { public static... (4 Replies)
Discussion started by: woot4moo
4 Replies
Login or Register to Ask a Question