Create object instance of different types

Discussion in 'C++' started by f.f, Jan 3, 2007.

  1. f.f

    f.f Guest

    class A
    {
    ....
    };

    class B : public A
    {
    ....
    };

    class C : public A
    {
    ....
    };

    T* createObject()
    {
    T* ptr = new T();
    return ptr;
    }

    I wanna use createObject() to create object instance of either class B
    or class C?
    What approach should I use? Should I use template?

    f.f
     
    f.f, Jan 3, 2007
    #1
    1. Advertisements

  2. f.f

    Andre Kostur Guest

    "f.f" <> wrote in news:1167836437.480073.131750
    @i12g2000cwa.googlegroups.com:

    > class A
    > {
    > ...
    > };
    >
    > class B : public A
    > {
    > ...
    > };
    >
    > class C : public A
    > {
    > ...
    > };
    >
    > T* createObject()
    > {
    > T* ptr = new T();
    > return ptr;
    > }
    >
    > I wanna use createObject() to create object instance of either class B
    > or class C?
    > What approach should I use? Should I use template?


    How does createObject know which of A, B, or C to create? Offhand I
    don't see how a template makes it easier:

    template<class T>
    T * createObject()
    {
    return new T();
    }

    All this seems to do is obfuscate the code by hiding the use of "new",
    since you'd have to specify the type to be created at the call site
    anyway. Why not just use "new" whererever you're using "createObject"?
     
    Andre Kostur, Jan 3, 2007
    #2
    1. Advertisements

  3. f.f

    red floyd Guest

    f.f wrote:
    > class A
    > {
    > ...
    > };
    >
    > class B : public A
    > {
    > ...
    > };
    >
    > class C : public A
    > {
    > ...
    > };
    >
    > T* createObject()
    > {
    > T* ptr = new T();
    > return ptr;
    > }
    >
    > I wanna use createObject() to create object instance of either class B
    > or class C?
    > What approach should I use? Should I use template?
    >


    You might want to google for "Factory Pattern"
     
    red floyd, Jan 3, 2007
    #3
  4. Sandip Shahane, Jan 3, 2007
    #4
  5. f.f

    f.f Guest

    Thank you all. I will try the factory pattern.

    On Jan 4, 12:20 am, red floyd <> wrote:
    > f.fwrote:
    > > class A
    > > {
    > > ...
    > > };

    >
    > > class B : public A
    > > {
    > > ...
    > > };

    >
    > > class C : public A
    > > {
    > > ...
    > > };

    >
    > > T* createObject()
    > > {
    > > T* ptr = new T();
    > > return ptr;
    > > }

    >
    > > I wanna use createObject() to create object instance of either class B
    > > or class C?
    > > What approach should I use? Should I use template?You might want to google for "Factory Pattern"
     
    f.f, Jan 4, 2007
    #5
    1. Advertisements

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. Sandra-24
    Replies:
    18
    Views:
    2,304
    Steve Holden
    Apr 29, 2006
  2. Dmitry Kulinich
    Replies:
    4
    Views:
    1,294
    Dmitry Kulinich
    Feb 19, 2007
  3. jon wayne
    Replies:
    9
    Views:
    1,014
    Jim Langston
    Sep 22, 2005
  4. Daniel Lipovetsky
    Replies:
    2
    Views:
    488
    Jordan Greenberg
    Mar 12, 2007
  5. __PaTeR
    Replies:
    7
    Views:
    664
    Barry Schwarz
    Jan 1, 2009
  6. mat
    Replies:
    0
    Views:
    361
  7. yuesefa
    Replies:
    4
    Views:
    233
    Leslie Viljoen
    Jul 25, 2006
  8. RichardOnRails
    Replies:
    2
    Views:
    100
    RichardOnRails
    Oct 26, 2009
Loading...