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??
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.
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:
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..
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)
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)
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)
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)
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)
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)