Re: operator new only as static member possible?

Discussion in 'C++' started by Robert Frunzke, Jan 7, 2004.

  1. 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
    Robert Frunzke, Jan 7, 2004
    #1
    1. Advertising

  2. Robert Frunzke

    John Carson Guest

    "Robert Frunzke" <> wrote in message
    news:btgseg$v55$02$-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)
    John Carson, Jan 7, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ernst Murnleitner

    operator new only as static member possible?

    Ernst Murnleitner, Jan 7, 2004, in forum: C++
    Replies:
    8
    Views:
    359
    Ernst Murnleitner
    Jan 12, 2004
  2. ghager
    Replies:
    6
    Views:
    612
    ghager
    Jan 19, 2006
  3. dolphin
    Replies:
    3
    Views:
    1,324
    Pete Becker
    Dec 5, 2007
  4. xmllmx
    Replies:
    6
    Views:
    398
    xmllmx
    Feb 3, 2010
  5. W Karas
    Replies:
    3
    Views:
    244
    Victor Bazarov
    Nov 30, 2012
Loading...

Share This Page