How do I enforce the user create a object via operator new only?

Discussion in 'C++' started by Xie Yubo, Jan 28, 2007.

  1. Xie Yubo

    Xie Yubo Guest

    Hello every,
    I have a question, could you help me?
    How do I enforce the user create a object via operator new only?

    For example, there is a class C:

    class C
    {
    .....
    }

    Then the user can only create a C object with:
    C* p = new C;
    and
    C c;
    is forbided.

    Are there some ways to implement it?

    Thanks!
    Xie Yubo, Jan 28, 2007
    #1
    1. Advertising

  2. Xie Yubo

    Ian Collins Guest

    Xie Yubo wrote:
    > Hello every,
    > I have a question, could you help me?
    > How do I enforce the user create a object via operator new only?
    >
    > For example, there is a class C:
    >
    > class C
    > {
    > .....
    > }
    >
    > Then the user can only create a C object with:
    > C* p = new C;
    > and
    > C c;
    > is forbided.
    >
    > Are there some ways to implement it?
    >

    Make the constructor private and private a friend function to return an
    instance of the class.

    class X
    {
    X() {}
    friend X* makeX() { return new X; }
    };

    --
    Ian Collins.
    Ian Collins, Jan 28, 2007
    #2
    1. Advertising

  3. Xie Yubo

    Jerry Coffin Guest

    In article <>,
    says...
    > Hello every,
    > I have a question, could you help me?
    > How do I enforce the user create a object via operator new only?


    [ ... ]

    FAQ, 16.21

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Jan 28, 2007
    #3
  4. Xie Yubo

    Xie Yubo Guest

    On Jan 29, 7:27 am, Ian Collins <> wrote:
    > Xie Yubo wrote:
    > > Hello every,
    > > I have a question, could you help me?
    > > How do I enforce the user create a object via operator new only?

    >
    > > For example, there is a class C:

    >
    > > class C
    > > {
    > > .....
    > > }

    >
    > > Then the user can only create a C object with:
    > > C* p = new C;
    > > and
    > > C c;
    > > is forbided.

    >
    > > Are there some ways to implement it?Make the constructor private and private a friend function to return an

    > instance of the class.
    >
    > class X
    > {
    > X() {}
    > friend X* makeX() { return new X; }
    >
    > };--
    > Ian Collins.


    But in this way, the user only use X* p = makeX(); not X* p = new X;
    Xie Yubo, Jan 28, 2007
    #4
  5. Xie Yubo

    Xie Yubo Guest

    On Jan 29, 7:33 am, Jerry Coffin <> wrote:
    > In article <>,
    > says...
    >
    > > Hello every,
    > > I have a question, could you help me?
    > > How do I enforce the user create a object via operator new only?[ ... ]

    >
    > FAQ, 16.21
    >
    > --
    > Later,
    > Jerry.
    >
    > The universe is a figment of its own imagination.


    I have read it. But in that way, the user only use Fred* p =
    Fred::create, not Fred* p = new Fred;
    Xie Yubo, Jan 28, 2007
    #5
  6. Xie Yubo

    Jerry Coffin Guest

    In article <>,
    says...

    [ ... ]

    > I have read it. But in that way, the user only use Fred* p =
    > Fred::create, not Fred* p = new Fred;


    That's right. What you asked for isn't possible, and that's as close as
    you can get.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Jan 28, 2007
    #6
  7. Xie Yubo

    Xie Yubo Guest

    On Jan 29, 7:51 am, Jerry Coffin <> wrote:
    > In article <>,
    > says...
    >
    > [ ... ]
    >
    > > I have read it. But in that way, the user only use Fred* p =
    > > Fred::create, not Fred* p = new Fred;That's right. What you asked for isn't possible, and that's as close as

    > you can get.
    >
    > --
    > Later,
    > Jerry.
    >
    > The universe is a figment of its own imagination.


    Is it impossible? Ok, I see. Thanks every one!
    Xie Yubo, Jan 28, 2007
    #7
  8. Xie Yubo

    Chris Guest

    Xie Yubo wrote:

    > On Jan 29, 7:33 am, Jerry Coffin <> wrote:
    >> In article <>,
    >> says...
    >>
    >> > Hello every,
    >> > I have a question, could you help me?
    >> > How do I enforce the user create a object via operator new only?[ ... ]

    >>
    >> FAQ, 16.21
    >>
    >> --
    >> Later,
    >> Jerry.
    >>
    >> The universe is a figment of its own imagination.

    >
    > I have read it. But in that way, the user only use Fred* p =
    > Fred::create, not Fred* p = new Fred;



    Yes Xie that's right. This will ensure that Fred is dynamically created via
    the new operator. So, since Fred::create() calls "new Fred;" then the user
    is effectively calling new Fred.

    --
    Chris
    Chris, Jan 28, 2007
    #8
  9. Xie Yubo

    Grizlyk Guest

    Xie Yubo wrote:

    > Then the user can only create a C object with:
    > C* p = new C;
    > and
    > C c;
    > is forbided.


    class A
    {
    ~A(){}

    public:
    int i;
    static void destroy(A*);
    };

    void A::destroy(A* p){delete p;}

    int main()
    {

    A a; //error
    A *p=new A; //ok

    delete p; //error
    A::destroy(p); //ok
    }

    Probably you can overload "new" and "delete" operators for your class also.

    --
    Maksim A Polyanin
    Grizlyk, Jan 29, 2007
    #9
  10. Xie Yubo

    Xie Yubo Guest

    On Jan 29, 5:32 pm, "Grizlyk" <> wrote:
    > Xie Yubo wrote:
    > > Then the user can only create a C object with:
    > > C* p = new C;
    > > and
    > > C c;
    > > is forbided.

    >
    > class A
    > {
    > ~A(){}
    >
    > public:
    > int i;
    > static void destroy(A*);
    >
    > };
    >
    > void A::destroy(A* p){delete p;}
    >
    > int main()
    > {
    >
    > A a; //error
    > A *p=new A; //ok
    >
    > delete p; //error
    > A::destroy(p); //ok
    >
    > }
    >
    > Probably you can overload "new" and "delete" operators for your class also.
    >
    > --
    > Maksim A Polyanin


    Wonderful! Thank you very much!
    Xie Yubo, Jan 31, 2007
    #10
  11. On 30 Jan 2007 21:37:52 -0800, "Xie Yubo" <> wrote:

    >On Jan 29, 5:32 pm, "Grizlyk" <> wrote:
    >> Xie Yubo wrote:
    >> > Then the user can only create a C object with:
    >> > C* p = new C;
    >> > and
    >> > C c;
    >> > is forbided.

    >>
    >> class A
    >> {
    >> ~A(){}
    >>
    >> public:
    >> int i;
    >> static void destroy(A*);
    >>
    >> };
    >>
    >> void A::destroy(A* p){delete p;}
    >>
    >> int main()
    >> {
    >>
    >> A a; //error
    >> A *p=new A; //ok
    >>
    >> delete p; //error
    >> A::destroy(p); //ok
    >>
    >> }
    >>
    >> Probably you can overload "new" and "delete" operators for your class also.
    >>
    >> --
    >> Maksim A Polyanin


    This is "half" of a Factory Method pattern. Why not go all the way?

    class A
    {
    public:
    static A& create();
    static void destroy(A& a);

    private:
    A() {}
    ~A() {}
    };

    A& A::create() { return *new A; }
    void A::destroy(A& a) { delete &a; }

    int main()
    {
    A a; // error

    A& ar = A::create();
    A::destroy(ar);

    return 0;
    }

    This pattern is also useful in case you want to encapsulate the creation
    policy:


    template <class CreationPolicy>
    class A: private CreationPolicy
    {
    public:
    static A& create();
    static void destroy(A& a);

    private:
    typedef CreationPolicy Policy;
    A() {}
    ~A() {}
    };

    A& A::create()
    {
    A* pa = Policy::allocate(sizeof(A));
    new (pa) A();
    return *pa;
    }

    /* ... */

    int main()
    {
    A& ar0 = A<HeapCreationPolicy>::create();
    A& ar1 = A<PoolCreationPolicy>::create();

    /* ... */
    }
    Dave Rahardja, Jan 31, 2007
    #11
  12. Xie Yubo

    Grizlyk Guest

    Dave Rahardja wrote:
    >
    > This is "half" of a Factory Method pattern. Why not go all the way?



    Because he wants to use "new" and maybe hide stack protection from other
    parts of code, are expecting "new" to work.

    >>> > Then the user can only create a C object with:
    >>> > C* p = new C;
    >>> > and
    >>> > C c;
    >>> > is forbided.
    >>>


    --
    Maksim A Polyanin
    Grizlyk, Jan 31, 2007
    #12
  13. Xie Yubo

    Rossonera Guest

    I think there is a way to resolve your problem.

    you can declare the constructor as private(if you don't want it can be
    derived by other classes), or declare it as protected.
    then, you add a static member function to call the constructor, I
    think it will be better.

    here is a example to show how to do this:

    class C {
    public:
    static C* New();
    ~C();

    protected:
    C();
    };

    C* C::New() {
    C *self = new C();
    return self;
    }

    int main() {
    C *pC = C::New();

    // to destroy it, simply call delete.
    delete pC;
    pC = NULL;

    return 0;
    }
    Rossonera, Feb 1, 2007
    #13
    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. Jeff
    Replies:
    3
    Views:
    652
    Roedy Green
    Jul 31, 2003
  2. Mysooru
    Replies:
    5
    Views:
    1,909
    Jonathan Mcdougall
    Jul 28, 2003
  3. prady
    Replies:
    2
    Views:
    437
    Phil H
    Dec 29, 2007
  4. xmllmx
    Replies:
    6
    Views:
    412
    xmllmx
    Feb 3, 2010
  5. Mark
    Replies:
    1
    Views:
    204
Loading...

Share This Page