|

November 19th, 2016, 12:29 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
|
|
Hi Steve,
Quote:
|
Isn't this section incorrect? It implies that a parent / base class's constructor is only called if the derived class has a constructor (and then goes further to imply you have to use the :base (params) notation to specify which one.
|
I don't think that's what the author of this chapter intended. I think he was trying to say that you need to invoke a parent constructor if you want to determine which one is called.
Quote:
|
Though it's true you can specify a particular overloaded constructor in the base if you do nothing (e.g. either don't implement a constructor or do but don't add the base) the base *default* constructor is ALWAYS then called. In fact it's very hard to work a solution not to do this.
|
I make the distinction between the default constructor and a parameterless constructor. A parameterless constructor has no parameters (duh). The default constructor is a parameterless constructor that Visual Studio creates for you if you don't define any other constructors. That's a bit different from a parameterless constructor that you create.
If you make any constructors parameterless or with parameters, then Visual Studio does not create the default constructor. For example, if you only give a class a constructor that takes one parameter, then it has no parameterless constructors.
The program always tries to invoke some base class constructor. If you don't give the base class any constructors, then it invokes the default constructor.
If the child class explicitly invokes a base class constructor, then you know which one it will use.
The weird case occurs if you give the base class a parameterized constructor and no parameterless constructor, but then don't invoke a constructor explicitly in the child class constructor. In that case Visual Studio tries to invoke a parameterless base class constructor but there isn't one. That's when you get the "no constructor with 0 arguments" error.
Quote:
|
Actually on pg 163 it says "Although a clhild class inherits most of the code in its parent classs, it doesn't inherit the class's constructor" - again.. wrong (I think - this should say it doesn't automatically inherit the class's constructors that contain parameters!).
|
This may be a semantic issue. I don't think the child class actually inherits the constructors, but its constructors invoke them. For example, suppose Person has a constructor that takes 2 parameters, first and last name. Then if Employee inherited that, it should have a constructor that takes 2 parameters. But it doesn't. If you want Employee to have a 2-parameter constructor, you need to define one (probably invoking the Person class's constructor).
But the child class has some access to the base class's constructors so they are still there even if they are not inherited in the same way that other methods are inherited.
Overall, however, I think your intuition is more or less right and this section of the book is somewhat confusing.
I hope that helps. If it's still unclear, please post a reply.
|