5 Replies - 1046 Views - Last Post: 05 February 2011 - 02:53 AM Rate Topic: -----

#1 abhishekkore21@gmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-February 11

initialization list and declaration order in constructor in C++

Posted 04 February 2011 - 10:49 PM

pl tel me how values are put in satck ...how declaration order is imp for initialization.. ex class A{
public:Pint c,b,a;
A():a(10),b(a),c(B)
{
}
};
gives garbage....but if A():c(10),b©,a(B)..gives 10 to c,b,a..bcoz declaration order is int c,b,a.

Is This A Good Question/Topic? 0
  • +

Replies To: initialization list and declaration order in constructor in C++

#2 Slumdog  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 116
  • Joined: 26-November 10

Re: initialization list and declaration order in constructor in C++

Posted 04 February 2011 - 10:52 PM

Please revise your question and ask us in a way we can understand. Thank you.
Was This Post Helpful? 0
  • +
  • -

#3 abhishekkore21@gmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-February 11

Re: initialization list and declaration order in constructor in C++

Posted 05 February 2011 - 12:10 AM

means when i write class A{
public : int c;
int b;
int a;
A():c(10),b©,a(B)
{}
};
10 value is assigned to c,b,a bcoz order of declarartion in class is same as order of initialization in constructor.

But when i do same class A{
public : int c;
int b;
int a;
A():a(10),b(a),c(B)
{}
};
values assigned to ,b,c are garbage bcoz order of declarartion in class is NOT same as order of initialization in constructor....why is this so??wat is order whn variables are put on stack..

why smiley cuming i dnt knw..first initializer list is A():c(10),b©,a(B)
second isA():a(10),b(a),c(B)

a has b as argument in first list
c has b as argument in second list
Was This Post Helpful? 0
  • +
  • -

#4 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: initialization list and declaration order in constructor in C++

Posted 05 February 2011 - 01:05 AM

This is because the initialization list initializes the variables as they are created and so it depends on the order of declaration.

Normal initialization within ctor body : First all creation then initialization.
Initialization list : Simultaneous creation and initialization.

I hope its clear why the situation is arising.

This post has been edited by chinchang: 05 February 2011 - 01:06 AM

Was This Post Helpful? 0
  • +
  • -

#5 abhishekkore21@gmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-February 11

Re: initialization list and declaration order in constructor in C++

Posted 05 February 2011 - 02:13 AM

ya but how the variable are pushed on stack...whn declared or when intialized..??
Was This Post Helpful? 0
  • +
  • -

#6 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: initialization list and declaration order in constructor in C++

Posted 05 February 2011 - 02:53 AM

Suppose u have :

int a,b,c;


As comma(,) operator is left-to-right associative, it creates variables from left to right (a, then and then c).

More here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1