Sashi said:
All, I had an interview today and I couldn't answer these questions.
Any good answers?
Why does a copy constructor param need to be const?
Technically, the parameter to a copy constructor _has_ to be
a const reference only if you will be copying from const
instances.
But I think most would agree that it's counter-intuitive for
a "copy" operation to change the thing that's being copied.
Making the parameter a const reference gives an assurance
that the source of the copy is not changed.
Ever need a virtual constructor?
Yes. The inability to make a copy (outside the heap) of an object
when you only know its subclass is one of the limitations of
using virtual functions for generic code as opposed to templates.
But allowing virtual constructors or something equivalent for
stack variables would probably require drastic additions to
the language syntax. I don't see any way to do it without
breaking the rule (in Standard C/C++) that any object created
in the stack has a size known at compile time. (I don't
know what the value of this rule is, and GNU gcc has extensions
that break it anyway.) Hard to see how to use virtual constructors
for statically allocated variables because of the issue of the
size not being known at compile time.
Major difference between public and private inheritance (other than the
obvious one)?
They're _really_ deap sea fishing with this one. Some differences:
1) Can't address instances with pointer or reference to private
base class.
2) Even public members of private base class are inaccessable
in a dervived class instance from outside the derived class.
3) Can't be used to represent an "is-a" relationship (which is
basically saying the same thing as #1 but in a more non-obvious way).