Consider this code

class A{
      int i;
      public:
            A(){
                   cout<<"default constructor\n";
            }
            ~A(){
                   cout<<"default destrutor\n";
             }

            /* uncommenting this causes error
            A(A& obj){
                  i=obj.i;
                  cout<<"copy constructor\n";
            }
            */
};

A fun(){      
      A b;
      cout<<" address of b = "<<&b<<endl;
      return b;
}

int main(){
      A a=fun();
      cout<<" address of a = "<<&a<<endl;
      return 0;
}

Doubts:
1. Why only 1 destructor is getting called.
2. Why address of a and b are same.
3. If I use copy constructor here, then the first line of main() is giving error
4. Constructor is getting called for object b which it should be, but destructor is getting called for object a.
5)Why am i not allowed to create a copy-constructor in this class?

2. Why address of a and b are same.

They are not, i ran it on my computer and i get 2 different addresses

3. If I use copy constructor here, then the first line of main() is giving error

Look at my answer to #5

4. Constructor is getting called for object b which it should be, but destructor is getting called for object a.

I don't think so, I removed the function fun() and left only a, and it gave me both destructor/constructor messages...i think the des/con aren't being called on B :?

5)Why am i not allowed to create a copy-constructor in this class?

Because it should be like this:

class A{
      int i;
      public:
            A(){
                   cout<<"default constructor\n";
            }
            ~A(){
                   cout<<"default destrutor\n";
             }


            A(A& obj);
};

and this:

A::A(A& obj)
{
                  i=obj.i;
                  cout<<"copy constructor\n";
};

Here, your code should be:

#include <iostream>

using namespace std;

class A{
      int i;
      public:
            A(){
                   cout<<"default constructor\n";
            }
            ~A(){
                   cout<<"default destrutor\n";
             }

};


A fun(){
      A b;
      cout<<" address of b = "<<&b<<endl;
      return b;
}

int main(){
      A a;
      fun();
      cout<<" address of a = " << &a<<endl;
      return 0;
}
commented: Lots of wrong info there buddy...check once before posting. +0

@athlon32
Check your answers before posting.

1>

They are not, i ran it on my computer and i get 2 different addresses

The address should be same because the OP is returning the object b address and assigning it to a.
2>

I don't think so, I removed the function fun() and left only a, and it gave me both destructor/constructor messages...i think the des/con aren't being called on B :?

How can you ever get destructor and constructor after removing fun() object b is getting declared within fun().
3>

Because it should be like this:
class A{
int i;
public:
A(){
cout<<"default constructor\n";
}
~A(){
cout<<"default destrutor\n";
}


A(A& obj);
};

and this:
A::A(A& obj)
{
i=obj.i;
cout<<"copy constructor\n";
};

You don't need to just a const suffices look at my post.

@ramthegreatcv
Answers :

>1. Why only 1 destructor is getting called.
Because even though you think you are dealing with two objects you are actually dealing with one,because when you used return b in fun(),what you are actually doing is returning the address of that object and assigning it to object a by the statement a=fun(); so only one object is under observation.Here destructor is called only for object a before returning from main.

>2. Why address of a and b are same.
Look at answer for 1.

>3. If I use copy constructor here, then the first line of main() is giving error
Just use your copy constructor as :

A(const A& obj){
                  i=obj.i;
                  cout<<"copy constructor\n";
            }

4>Constructor is getting called for object b which it should be, but destructor is getting called for object a.
Look at answer 1.

5>Why am i not allowed to create a copy-constructor in this class?
Because you were not doing it the right way.

commented: Thanks Man, you showed me I was wrong and I appreciate it :D +1

Thanks a lot to csurfer

>1. Why only 1 destructor is getting called.
Because even though you think you are dealing with two objects you are actually dealing with one,because when you used return b in fun(),what you are actually doing is returning the address of that object and assigning it to object a by the statement a=fun(); so only one object is under observation.Here destructor is called only for object a before returning from main.

Why doesn't this happen for primitive data types like int ,char or float ?
Is it always okay to return a local object like this? or is this code not portable?

>3. If I use copy constructor here, then the first line of main() is giving error
Just use your copy constructor as :

A(const A& obj){
                  i=obj.i;
                  cout<<"copy constructor\n";
            }

Thanks a lot,you are right.this is because of temporary objects ore something right?

One more thing,if u notice our copy constructor is still not getting called!!

>Why doesn't this happen for primitive data types like int ,char or float ?
I really didn't get your question.But this will give you your answers I suppose. and also this and this.

To be frank I think you don't know the use of copy constructor at all so please read about it first and then ask your doubts if you don't mind.You need to brush up a lot.

#include<iostream>
using namespace std;

class A{
        int i;
        public:
	A(){
		cout<<"Default Constructor\n\n";	
	};
	A(const A& a){
		cout<<"Why am i(Copy Constructor) not used at all?\n\n";
		i=a.i;
	}
	~A(){
		cout<<"Why am i(Destructor) called here only, and not after fun2 for destroying aA?\n(*****csurfer can u explain this better******)\n\n";
	}
};

int fun1(){
	int aInt;
	cout<<"Why is address of aInt "<<&aInt;
	return aInt;
}

A fun2(){
	
	A aA;
	cout<<"Why is address of aA "<<&aA;

	return aA;
}

int main(){
	A bA = fun2();//What constructor is used here?
	cout<<" same as bA "<<&bA<<"\nbut ";
	
	int bInt = fun1();
	cout<<" not same as bInt "<<&bInt<<"\n\n";
}

@csurfer:
I know the use of dynamic allocation.If u notice i haven't returned the local variable's reference ,address or anything like that.
And as far as copy constructor goes can u please explain its use (in case the way i am using it is wrong) ?

Answers:

1>Assume that there is a class as:

class check{

int a;

public:

check()
{ 
cout<<"Constructor"; 
}

check(int i)
{ 
//copy constructor code 
}

void setvalue()
{ 
//sets value for a 
}
};

//Inside main way 1 normal
check c;
c.setvalue();

//Inside main way 2 copy constructor
check c(5);

Above shown are the two types of initializations for initialising integer a within the class object.
First method is to call the setvalue function as we normally do to set value to that integer.But you can directly initialize the static variables present within the class object as

check c(5);

if you have written code for it properly.

2>Address of bA and aA are same I have answered already why.

3>Value of aInt and bInt cannot be same because you are returning the value of aInt so aInt's value gets assigned to bInt so their values are same but their addresses differ because they are separate variables.

4>I have already explained why the destructor is called at end of main only,its because aA is not destroyed at the end of fun2 its returned and assigned to bA so now after main destructor for this object is called and that is seen by you.

Alas the answer for addresses being the same is "return value optimization".
i hope this is what csurfer meant, when he said they are the same objects.But this is not universal,it is compiler dependent.
I would suggest others to read the summary part of following link.
for more details

i am pretty sure copy constructor is of check(const Check&) form only and no other.
i.e Check(int),Check(char) etc are not called copy constructors.

Answers:

1>Assume that there is a class as:

class check{

int a;

public:

check()
{ 
cout<<"Constructor"; 
}

check(int i)
{ 
//copy constructor code 
}
.
.

Look at this:

check(int i)
{ 
//copy constructor code 
}

Why would this be a copy constructor?
It's just an overloaded constructor, it doesn't make a copy.

Both of you are right.

Copy constructor is a constructor used only in situations where we come across situations two objects A and B of same class type assigned as

A=B;

I wanted to go with example

X a=X(X const& copyFromMe);

example but it ended up this way.(In making sure I don't give him the thing he seeks in a golden platter ;))

@ramthegreatcv : Ya its return value optimization and about the copy constructor I didn't find any other way to make you understand,so used this bizzare approach.

@tux4life :I was trying to explaining op the purposes of using a copy constructor with that,thats it,I know the way I took is hopeless.

>I was trying to explaining op the purposes of using a copy constructor with that,thats it,I know the way I took is hopeless.

Why not just providing some links?
http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html

http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/
http://www.learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying/

:P

commented: Good work tuxxy !!! ;) +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.