constructor-destructor issue

This is a discussion on constructor-destructor issue within the C++ Programming forums, part of the General Programming Boards category; I have a question about constructor. When is constructor called and when is not? Look at this sample code Code: ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    constructor-destructor issue

    I have a question about constructor. When is constructor called and when is not?
    Look at this sample code
    Code:
    #include <iostream.h>
    
    class SimpleClass
    {
    public:
    	SimpleClass();
    	~SimpleClass();
    	friend ostream & operator<<(ostream &,SimpleClass&);
    	int GetData();
    	void SetData(int);
    	void testfunction(SimpleClass);
    private:
    	int itsData;
    };
    
    SimpleClass::SimpleClass():itsData(0){cout<<"Default constructor called!"<<endl;}
    SimpleClass::~SimpleClass(){cout<<"Destructor called!"<<endl;}
    
    int SimpleClass::GetData(){return itsData;}
     
    ostream& operator<<(ostream &o, SimpleClass &rhs)
    {
    	o<<rhs.GetData();
    	return o;
    }
    
    void SimpleClass::testfunction(SimpleClass obj)
    {	
    	//why at this place default constructor is not called
    	obj.SetData(2);
    }//Destructor is called when obj goes out of scope
    
    void SimpleClass::SetData(int i)
    {
    	itsData=i;
    }
    
    int main()
    {
    	SimpleClass obj; //this will cause message "Default constructor called"
    	cout<<obj<<endl;
    	obj.testfunction(obj);
    	return 0;
    }

    When this is executed I get messages

    Default constructor called!
    0
    Destructor called!
    Destructor called!

    Default constructor is called when I explicitly create object at line SimpleClass obj;
    destructor is called when going out of function and at the end of the program, But why constructor is not called in function to create object obj or it is?
    How can destructor be called and constructor cannot or some other constructor is called.
    This is a bit confusing for me, so please help.

  2. #2
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    Iam not good with con/destructors here. But if you do something like this in your main function;

    Code:
    int main()
    {
                    char s[20];
                    cout << "push a button then enter to create object";
                    cin >> s;
    	SimpleClass obj; //this will cause message "Default 
                    cout << "push a button to do the next line";
                    cin >> s;
    	cout<<obj<<endl;
                    cout << "push a button then enter again to execute testfunction";
                    cin >> s;
    	obj.testfunction(obj);
                    cout << "all done";
                   cin >> s;
    	return 0;
    }
    I added cin >> s; to pause so you can take your code stepbystep to help you understand more.

    Hope that helps!

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    >>obj.testfunction(obj);

    You are passing by value and so are invoking the Copy Constructor...This is implicitly provided by the compiler, but you can add it to your class and track the object's creation when you create a copy

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Here is the "problem" why constructor/destructor isnīt called equally times. Because when user defined object are passed as argument
    Code:
    obj.testfunction(obj);
    the parameter (obj in SimpleClass::testfunction)
    Code:
    void SimpleClass::testfunction(SimpleClass obj)
    {	
    	//why at this place default constructor is not called
    	obj.SetData(2);
    }//Destructor is called when obj goes out of scope
    is calling the copy constructor. When you define and initialize an user defined object
    e.i.
    Code:
    SimpleClass obj1;
    SimpleClass obj2(obj1);//<-This line invokes the copyconstructor
    the copy constructor is invoked. The same mechnism occur when passing user defined objects to functions.
    Also be aware that some compiler optimization can reduce some constuctor- and destuctorcalls, e.i Named Return Value.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  5. #5
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Thanks guys, I understand now

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21