Sequence in derived class construction

F

Fred Zwarts

Consider the following two classes for a given type T_t:

class Base {
private:
T_t *const PointerToT;
public:
Base (T_t &T) : PointerToT (&T) {}
};

class Derived : public Base {
private:
T_t T;
public:
Derived () : Base (T) {}
}

Note that the member T of class Derived is passed to the constructor of its base class.
However, the constructor of class Base uses only the address of T.
Is this defined according to the C++ standard?
Is the layout of the the derived class defined when the constructor of the base class is called,
although the members of the derived class are not yet initialized?
Or does the C++ standard allow that the layout of the members of the derived class is delayed
till after the construction of the base class?

F.Z.
 
R

Rolf Magnus

Fred said:
Consider the following two classes for a given type T_t:

class Base {
private:
T_t *const PointerToT;
public:
Base (T_t &T) : PointerToT (&T) {}
};

class Derived : public Base {
private:
T_t T;
public:
Derived () : Base (T) {}
}

Note that the member T of class Derived is passed to the constructor of
its base class. However, the constructor of class Base uses only the
address of T.
Is this defined according to the C++ standard?

It is ok to pass a pointer to an object that is not yet fully constructed,
as long as you don't dereference it before the object is constructed.
However, I'm not sure whether the C++ standard allows this to be done with
a reference, too. It would probably work on most systems though.
Is the layout of the the derived class defined when the constructor of the
base class is called, although the members of the derived class are not
yet initialized?

The layout of a class doesn't change dynamically. It is determined at
compile-time.
Or does the C++ standard allow that the layout of the members of the
derived class is delayed till after the construction of the
base class?

Not sure what you mean here. The derived part is constructed after the base
part, so Base gets a pointer that it must not use in its constructor, since
the object it points to isn't yet constructed.
 
H

Howard

Fred Zwarts said:
Consider the following two classes for a given type T_t:

class Base {
private:
T_t *const PointerToT;
public:
Base (T_t &T) : PointerToT (&T) {}
};

class Derived : public Base {
private:
T_t T;
public:
Derived () : Base (T) {}
}

Note that the member T of class Derived is passed to the constructor of
its base class.
However, the constructor of class Base uses only the address of T.
Is this defined according to the C++ standard?
Is the layout of the the derived class defined when the constructor of the
base class is called,
although the members of the derived class are not yet initialized?
Or does the C++ standard allow that the layout of the members of the
derived class is delayed
till after the construction of the base class?

F.Z.

I'm not sure about references used before construction is complete, but you
can pass pointers, as long as they're not dereferenced prior to construction
completion. So you could change that Base constructor to take "const T_t*
T", and then use "PointerToT(T)" as the initializer for the Base constructor
and "Base(&T)" as the initializer for the Derived constructor. Just a
thought.

-Howard
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,660
Latest member
vidotip479

Latest Threads

Top