Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > calling member functions from an initialiser list

Reply
Thread Tools

calling member functions from an initialiser list

 
 
John Harrison
Guest
Posts: n/a
 
      06-25-2003
What are the rule concerning calling member functions from an initialiser
list? Suppose I have

class C : public B
{
public:
C() : x(), y(f()), z() {}
private:
Y f();
X x;
Y y;
Z z;
};

B, X, Y, Z are other classes.

What am I allowed to do in C::f()? Presumably I'm not allowed to access C::y
or C::z since they haven't been constructed yet, what about C:, and what
about members of the base class B? Any other gotchas in this situation?

thanks,
john


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-25-2003
"John Harrison" <(E-Mail Removed)> wrote...
> What are the rule concerning calling member functions from an initialiser
> list? Suppose I have
>
> class C : public B
> {
> public:
> C() : x(), y(f()), z() {}
> private:
> Y f();
> X x;
> Y y;
> Z z;
> };
>
> B, X, Y, Z are other classes.
>
> What am I allowed to do in C::f()? Presumably I'm not allowed to access

C::y
> or C::z since they haven't been constructed yet, what about C:, and what
> about members of the base class B? Any other gotchas in this situation?


The rule is that you may do that, but (a) it will resolve statically
(no virtual calls), and (b) the function should not try to use any
parts of the object (members and base classes) that haven't been
constructed yet.

Perhaps in your case it's better to make 'f' static?.. And if you need
to use 'x' there, just pass it as an argument...

Victor


 
Reply With Quote
 
 
 
 
=?iso-8859-1?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      06-25-2003
John Harrison escribió:

> class C : public B
> {
> public:
> C() : x(), y(f()), z() {}
> private:
> Y f();
> X x;
> Y y;
> Z z;
> };
>
> B, X, Y, Z are other classes.
>
> What am I allowed to do in C::f()? Presumably I'm not allowed to accessC::y
> or C::z since they haven't been constructed yet, what about C:, and what
> about members of the base class B? Any other gotchas in this situation?


The base class is constructed before any members. x is constructed, then
you can use it.

Regards.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
initialiser list Fraser Ross C++ 1 12-24-2010 02:50 PM
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
array initialiser list, order of initialisation kwikius C++ 11 07-11-2008 08:06 PM
Dereferencing this in constructors initialiser list Martin Zimmermann C++ 2 04-14-2004 02:07 PM



Advertisments