Using copy constructors from base class

When you initialize a copy constructor in a class, called derived_class_, that publicly inherits another class, called base_class_. You can use the initializer base_class_<ItemType>(an_object) to call the base_class_ constructor to initialize the data in derived_class_.

1. Why is this possible? Wouldn't base_class_<ItemType>(an_object) create an object of type base_class_, and not an object of derived_class_? How would you access the data using derived_class_ methods if it's created inside base_class_?

2. Also, why doesn't the syntax base_class_<ItemType>(an_object) require a variable name?

An example of implementation of what I am talking about:

1
2
3
4
template < class ItemType>
derived_class_<ItemType>::derived_class_( const derived_class_<ItemType>& an_object) : base_class_<ItemType>(an_object)
{
}
Last edited on
1. The base class is constructed first, then the derived class is constructed on top of it. This is just the syntax Stroustrup chose for deciding which base class constructor to use.

2. Because there is no such variable, only the object itself.
Topic archived. No new replies allowed.