Visual studio 2008 is being ridiculous.. Can you please assist with this error? It's telling me it doesn't know which constructor to call for my base class Name, when I define the default constructor for my derived class, derivedName..
Here is the code:
1 2 3 4 5 6 7 8
derivedName::derivedName() : Name() //whether I put :Name() or not, I receive the same error
{
// error C2668: 'Name::Name' : ambiguous call to overloaded function
lastName = "";
}
If you ever want to add a second parameter to the constructor, then you have to either
put it first in the parameter list or default it as well. By defaulting it, it means that any
code that currently uses the single parameter constructor continues to compile using the
default value for the second parameter, which may not be what you want. By not
defaulting either, the compiler will catch every place you need to update in your code
(compile error) for you. Less chance of coding bug.
By defaulting it, it means that any
code that currently uses the single parameter constructor continues to compile using the
default value for the second parameter
If/when that situation comes up, then I'd write a seperate ctor. As it stands now, both ctors construct the object the exact same way so writing two just means more code and more maintanance.
A more common bug that's easier to make would be when you add more member variables to the class -- you'll probably remember to initialize them in one constructor but might forget to do it in all your constructors if you have multiple.
Duplicate code is way worse, IMO. At least when you need to change the ctor interface you actually have to look at your ctors and how changes to them will impact your code.