post  test question: protected constructor and copy cons

chrisben (22)   Link to this post

class A{
public:
void f();
protected:
A(){
cout << "A:A()" << endl;
}
A(const A&){}
};


Referring to the sample code above, why are the default and copy constructors declared as protected?
1 To ensure that instances of A cannot be copied
2 To ensure that instances of A cannot be created via new by a more derived class
3 To ensure that instances of A can only be created by subclasses of A
4 To ensure that A cannot be instantiated on the stack
5 To ensure that A cannot be used as a base class


I was not sure on this one. I think 1,2, 4 all make sense somehow.

Thanks
Disch (1029)   Link to this post
Remember what protected means:

public = Accessible to anything
private = Only accessilbe to this class*
protected = Only accessible to this class* and its children (or "subclasses" as this problem refers to them)

* and its friends - but friends aren't a consideration for this scenario.

Keeping that in mind, and keeping in mind constructors must be accessible for you to create instances of a class... what does it imply if the constructors are protected?

Think about it a bit more ;D


This topic is archived - New replies not allowed.