Hi,
i am a beginner in c++ programming.
i have 3 independent classes A,B,C.i do not want B to create A's object but C can create A's object.how to implement this scinario?
When I saw the title I thought he was talking about the singleton pattern, but I guess he wants something else. In general, restrictions on the instantiation of an object can be imposed by making its constructor(s) and its destructor private, and declaring friendship to a desired set of classes:
class A
{
friendclass C;
private:
A(){}
~A(){}
};
class B
{
public:
//uncommenting results in
//compilation error
//void b_func() {A a;}
};
class C
{
public:
void c_func() {A a;}
};
int main()
{
//uncommenting results in
//compilation error
//A a;
return 0;
}
Though, I don't see how that would be useful... (but maybe I'm just missing something)
As Disch said, if you don't want B to create an A object, just don't tell it to :P
EDIT: That's what I get for checking my posts two and three times before submitting them... -.-