Doubt regarding Copy Constructor and return value


 
Thread Tools Search this Thread
Top Forums Programming Doubt regarding Copy Constructor and return value
# 1  
Old 02-06-2009
Doubt regarding Copy Constructor and return value

Hi All,

I have made the simple following program :-

Code:
#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( A aa)
{
        cout<<"In function f()"<<endl;
//        return aa;
        }
A  ret()
{
A d;
return d;
}
int main(int pppargc, char **argv, char **envp)
{
    A abc;
    A aa=abc;
   f(ret());
        return 0;
}

I am not returning any value in the defination of function f but then the program is compiling fine and is giving me the following O/P:-

In A()
In copy c'tor
In A()
In copy c'tor
In function f()

I am not able to figure out how is this happening.
Could someone please help me in this ????

Secondly :-

when I am uncommeting the return statement in the function f
then the copy constructor is called else it is not called .

The following is the O/P:-

In A()
In copy c'tor
In A()
In copy c'tor
In function f()
In copy c'tor


could somebody please tell me how is the copy constructor called only when the retirn statement is uncommented else it is not getting called ?

Thanks You.

Last edited by vino; 02-07-2009 at 01:48 PM.. Reason: added code tags
# 2  
Old 02-06-2009
Please use code tags for code, they make code readable, otherwise the board strips out all spacing. Code tags work like [ code ] stuff [ /code ] except without the extra spaces in the tags.
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

Doubts on C++ copy constructor concept

Hi, If I run the following program 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; } (1 Reply)
Discussion started by: royalibrahim
1 Replies

3. 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

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