Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: operator new only as static member possible?

Reply
Thread Tools

Re: operator new only as static member possible?

 
 
Robert Frunzke
Guest
Posts: n/a
 
      01-07-2004


Ernst Murnleitner wrote:
> Hello Readers,
>
> Is there a way that only one class can construct a class A and its inherited
> classes A2, A3 etc.?
>
> I want to construct a class A (and the inherited classes A2, A3 etc.) from a
> (factory) class Fa.
> I wanted to make that only F can call
>
> new A


You could make the constructor / all constructors private and declare
the factory as a friend.




Robert
 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      01-07-2004
"Robert Frunzke" <(E-Mail Removed)> wrote in message
news:btgseg$v55$02$(E-Mail Removed)-online.com
> Ernst Murnleitner wrote:
> > Hello Readers,
> >
> > Is there a way that only one class can construct a class A and its
> > inherited classes A2, A3 etc.?
> >
> > I want to construct a class A (and the inherited classes A2, A3
> > etc.) from a (factory) class Fa.
> > I wanted to make that only F can call
> >
> > new A

>
> You could make the constructor / all constructors private and declare
> the factory as a friend.
>
>
>
>
> Robert


That won't work. Consider


class A
{
friend class Fa;
private:
A()
{}
};

class A1 : public A
{
};

class A2 : public A
{
};

class Fa
{
public:
A* MakeA()
{
return new A;
}
A1* MakeA1()
{
return new A1;
}
A2* MakeA2()
{
return new A2;
}
};


int main()
{
Fa fa;
A *pa = fa.MakeA();
A1*pa1 = fa.MakeA1();
A2*pa2 = fa.MakeA2();
return 0;
}


This won't compile because, even though Fa calls new, the construction of a
derived class necessarily involves the derived class calling the constructor
of the base class. If the base class constructor is private, then it cannot
be called by the derived class.


--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Member vs. non-member operator overload disambiguation W Karas C++ 3 11-30-2012 06:34 PM
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
Problem with member and non-member binary operator in template class ghager C++ 6 01-19-2006 11:32 AM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
operator new only as static member possible? Ernst Murnleitner C++ 8 01-12-2004 04:06 PM



Advertisments