GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Jul-2008, 05:51
u_peerless u_peerless is offline
New Member
 
Join Date: Jun 2008
Posts: 7
u_peerless has a little shameless behaviour in the past

Initializer List


Hi C++ Coders,
I have a query. I was going through practising some C++ programs. We know that base class constructor is invoked first before derived class constructor.

Now suppose I have a program:
CPP / C++ / C Code:
class foo
{
      public:
             foo(int i)
             {
                     cout<<"Foo's Constructoe Called With:"<< i<<":\n";
                     }
      };
class bar:public foo
{
      public:
             bar():foo(10)
             {
                          cout<<"Bar's Constructr:\n";
                          }
      };

int main()
{
//    foo f(12);
    bar b;
    getch();
}

I have created an initializer list to initialize i as 10. But the foo class constructor should be invoked first before bar constructor. When I am running the program, then foo constructor is displaying as 10. My qs is how 10 value is getting when derived class constructor has not been invoked yet?
Last edited by admin : 11-Jul-2008 at 21:54. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 11-Jul-2008, 08:07
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: Initializer List


CPP / C++ / C Code:
#include <iostream>
using namespace std;

class foo1 {
        public:
                foo1 (int i) { cout << "FOO1 [" << i << "]" << endl; }
}; 
class foo2 {
        public:
                foo2 (int i) { cout << "FOO2 [" << i << "]" << endl; }
}; 

class bar1 : public foo1, foo2 {
        public:
                bar1 () : foo1 (42), foo2 (13) { cout << "BAR1" << endl ; }
};

class bar2 : public foo2, foo1 {
        public:
                bar2 () : foo1 (42), foo2 (13) { cout << "BAR2" << endl ; }
};

int main () {
        bar1 b1;
        bar2 b2;
        return 0;
}

Quote:
Originally Posted by output
FOO1 [42]
FOO2 [13]
BAR1
FOO2 [13]
FOO1 [42]
BAR2
Realize that when you declare the class, you have to declare any base classes at the same time. This allows the compiler to know what constructors are needed before the constructor of the current class is invoked. Also notice that the order of the initializer list is not necessarily important, while the order in the declaration is.
Quote:
Originally Posted by Stroustroup
... constructors are called in the order they are declared in the class rather than the order they appear in the initializer list.
  #3  
Old 11-Jul-2008, 08:15
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 586
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Initializer List


Quote:
Originally Posted by u_peerless
My qs is how 10 value is getting when derived class constructor has not been invoked yet?
The initialization list specifies which base class constructor is to be executed.

Consider the following changes to your posted code:
CPP / C++ / C Code:
#include <iostream>

using namespace std;

class foo {
protected:
    int v;
public:
    foo()  { 
        cout << "foo::foo() default constructor" << endl;
        v = 0;  
        cout << "v = " << v << endl; 
    }
    foo(int i) { 
        cout << "foo::foo(" << i << ") constructor" << endl;
        v = i;  
        cout << "v = " << v << endl; 
    }
};

class bar : public foo {
public:
    bar() : foo() { 
        cout << "bar::bar() default constructor" << endl; 
        cout << "v = " << v << endl;
    }
    bar(int i) : foo(i)  { 
        cout << "bar::bar(" << i << ") constructor" << endl;
        cout << "v = " << v << endl;
    }
};

int main()
{
    bar a(1);
    bar b;

    return 0;
}
Execution yields:
Code:
foo::foo(1) constructor v = 1 bar::bar(1) constructor v = 1 foo::foo() default constructor v = 0 bar::bar() default constructor v = 0
  #4  
Old 12-Jul-2008, 22:28
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Initializer List


Initializer List called before enter into {}.

I hope this help.
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Str_Misaligned in Double Link List Peter_APIIT C Programming Language 1 29-Feb-2008 20:50
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
need 'pointers' on deaing with segmentation faults aijazbaig1 C Programming Language 7 17-Aug-2007 07:43
C++ class -- Please help vnca_1 C++ Forum 3 14-Jun-2006 12:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:08.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.