Adobe Interview Question
Developer Program EngineersCountry: India
Interview Type: In-Person
I think it's OK to make them private too, right? I mean, it'll prevent the subclasses from being able to have a default = operator and copy constructor, but that makes sense: if a class is uncopiable, its subclasses should be too.
If they're protected, the subclass will be able to make copies of instances of the base class.
@eugene If you make all the constructors of the base class protected then you can only call those constructors for base class initialization when calling derived class constructor using this pointer, There's no way for you to create its object, so also you can't copy.
I think it is better than making private.
Since by making private you will make derived classes uncopyable too, which is not what we want
Why not? If a base class is uncopiable, it makes sense for derived classes to also be so, because a derived class includes the superclass in some sense. Also, you can still override the = operator for subclasses and define code for copying the objects, where appropriate. The issue I'm seeing is that if you make the copy constructors protected, you're introducing the possibility that someone implementing a subclass will create instances of the base class and copy them, thus bypassing the uncopiable rule.
By making instances of base class what do you mean, creating objects of base class i.e. you will have base class objects as member objects in derived class.
I'm saying that if the copy constructors and assignment operations are protected, the subclasses could then contain code like BaseClass b = new BaseClass(); BaseClass b2 = b;
Reading the problem description, this should not be allowed, it seems. That's why I think they should be private. Yes, that does make the subclasses uncopiable unless they implement their own copy constructor and assignment operators.
Just make the copy constructor and the assignment operator protected for the base class
- alexander July 04, 2012