« May 2007 »
S M T W T F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
You are not logged in. Log in

Programming Tips
Friday, 18 May 2007
Invoking constructor of a constructed class!!
Mood:  bright
Topic: C++

class MyClass
{

   public:

      MyClass()
      {
           cout << "Previous value: " << m_nNum << endl;
           m_nNum = 20;
      }

   private:
      int m_nNum;
};

void main()
{
     // Constructor invoked once here
     MyClass mc;

     // Now we are going to invoke once more
    ::new(( void* )&mc) MyClass;

     // First time printed number will be garbage, second time number will be 20
}


Posted by Nibu babu thomas at 3:26 PM
Updated: Monday, 21 May 2007 11:22 AM

Monday, 21 May 2007 - 7:11 AM

Name: "Steve"

This code will not compile: the constructor is private!

Monday, 21 May 2007 - 10:41 AM

Name: thomasnibu
Home Page: http://thomasnibu.tripod.com

Yeah thanks, now fixed. :)

View Latest Entries