A quick check...

Discussion in 'C++' started by Tomás, Mar 8, 2006.

  1. Tomás

    Tomás Guest

    class Base
    {
    public:

    int* const p;

    Base(int* const x) : p(x) {}
    };

    class Derived : public Base
    {
    public:

    int monkey[8];

    Derived() : Base(monkey) {}
    };

    int main()
    {
    Derived k;
    }


    Is this okay? Can I be certain that "monkey" will have been created before
    the constructor of the Base class is called?

    -Tomás
     
    Tomás, Mar 8, 2006
    #1
    1. Advertising

  2. Tomás

    Phlip Guest

    Tomas wrote:

    > class Base
    > {
    > public:
    >
    > int* const p;
    >
    > Base(int* const x) : p(x) {}
    > };
    >
    > class Derived : public Base
    > {
    > public:
    >
    > int monkey[8];
    >
    > Derived() : Base(monkey) {}
    > };


    > Is this okay? Can I be certain that "monkey" will have been created before
    > the constructor of the Base class is called?


    Construction happens from top to bottom. Base will fully construct, with the
    address where monkey will be, before monkey constructs.

    However, monkey already has storage, so it can point to it.

    New question; how defined is this?

    Base(int* x) : p(x)
    {
    *p;
    p[3] = 42;
    int whatever = p[4];
    }

    All those lines deference valid storage containing an unconstructed int.
    Ints have trivial constructors, but I thought I heard that all three of
    those lines generate technically undefined behavior.

    They will probably "work". Don't do any of this in professional code.

    --
    Phlip
    http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
     
    Phlip, Mar 8, 2006
    #2
    1. Advertising

  3. Tomás wrote:
    > class Base
    > {
    > public:
    >
    > int* const p;
    >
    > Base(int* const x) : p(x) {}
    > };
    >
    > class Derived : public Base
    > {
    > public:
    >
    > int monkey[8];
    >
    > Derived() : Base(monkey) {}
    > };
    >
    > int main()
    > {
    > Derived k;
    > }
    >
    >
    > Is this okay? Can I be certain that "monkey" will have been created before
    > the constructor of the Base class is called?


    It is OK. The storage for 'Derived' is allocated before the constructor
    is invoked. So, 'monkey' array is allocated and the address of it is well
    known. Passing that address to the base class for storing is OK.

    This practice, however, is shady because if I attempt to access 'x' for
    whatever reason in 'Base's constructor, then the behaviour is undefined.

    V
    --
    Please remove capital As from my address when replying by mail
     
    Victor Bazarov, Mar 8, 2006
    #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. JKop
    Replies:
    11
    Views:
    923
  2. Ioannis
    Replies:
    12
    Views:
    1,049
    BusyGuy
    Jun 2, 2006
  3. Replies:
    11
    Views:
    495
  4. Replies:
    9
    Views:
    293
    Jack Klein
    Feb 1, 2008
  5. Tom Cloyd
    Replies:
    7
    Views:
    150
    Eric Hodel
    Dec 15, 2008
Loading...

Share This Page