calling member functions from an initialiser list

Discussion in 'C++' started by John Harrison, Jun 25, 2003.

  1. 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::x, and what
    about members of the base class B? Any other gotchas in this situation?

    thanks,
    john
     
    John Harrison, Jun 25, 2003
    #1
    1. Advertising

  2. "John Harrison" <> 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::x, 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
     
    Victor Bazarov, Jun 25, 2003
    #2
    1. Advertising

  3. 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::x, 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.
     
    =?iso-8859-1?Q?Juli=E1n?= Albo, Jun 25, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Zimmermann
    Replies:
    2
    Views:
    490
    Rob Williscroft
    Apr 14, 2004
  2. kwikius
    Replies:
    11
    Views:
    532
    kwikius
    Jul 11, 2008
  3. Hicham Mouline
    Replies:
    0
    Views:
    435
    Hicham Mouline
    Apr 23, 2009
  4. Hicham Mouline
    Replies:
    1
    Views:
    421
    Michael DOUBEZ
    Apr 24, 2009
  5. Fraser Ross

    initialiser list

    Fraser Ross, Dec 24, 2010, in forum: C++
    Replies:
    1
    Views:
    270
    Fraser Ross
    Dec 24, 2010
Loading...

Share This Page