class *a = new ?;

Discussion in 'C++' started by feribiro, Sep 28, 2009.

  1. feribiro

    feribiro Guest

    Hi,

    could you tell me please how it is possible to pass the type of a
    class to a function as a parameter, and then initiate a new class of
    that type?

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

    void Main()
    {
    Create(B);
    Create(C);
    }

    void Create(??? param)
    {
    A *a = new ??param??;
    delete a;
    }

    Thank you very much
    feribiro, Sep 28, 2009
    #1
    1. Advertising

  2. feribiro

    SG Guest

    On 28 Sep., 13:14, feribiro <> wrote:
    > Hi,
    >
    > could you tell me please how it is possible to pass the type of a
    > class to a function as a parameter, and then initiate a new class of
    > that type?
    >
    > class A {...}
    > class B : public A {...}
    > class C : public A {...}
    >
    > void Main()
    > {
    >  Create(B);
    >  Create(C);
    > }
    >
    > void Create(??? param)
    > {
    >  A *a = new ??param??;
    >  delete a;
    > }
    >
    > Thank you very much


    You cannot do this dynamically like in other languages which keep
    enough meta data about classes in memory for "reflection" (I'm
    thinking of Java in this case) -- at least not without getting your
    hands dirty (building registries, registering types, etc by yourself).

    But you can do it easily at compile-time. In your case ("Create(B);")
    you already know the type at compile-time (B)...

    class A
    {
    public:
    virtual ~A() {}
    //...
    };

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

    template<typename T>
    void Create()
    {
    A* a = new T;
    delete a;
    }

    int main()
    {
    Create<B>();
    Create<C>();
    }

    Note: The class A must have a virtual destructor because you invoke
    delete with a pointer of type A* that points to a possibly derived
    object. Inheritance must be public in your case. The main function's
    name is all lower case and its return type is int.

    Cheers,
    SG
    SG, Sep 28, 2009
    #2
    1. Advertising

  3. SG <> writes:

    > On 28 Sep., 13:14, feribiro <> wrote:
    >> Hi,
    >>
    >> could you tell me please how it is possible to pass the type of a
    >> class to a function as a parameter, and then initiate a new class of
    >> that type?
    >>
    >> class A {...}
    >> class B : public A {...}
    >> class C : public A {...}
    >>
    >> void Main()
    >> {
    >>  Create(B);
    >>  Create(C);
    >> }
    >>
    >> void Create(??? param)
    >> {
    >>  A *a = new ??param??;
    >>  delete a;
    >> }
    >>
    >> Thank you very much

    >
    > You cannot do this dynamically like in other languages which keep
    > enough meta data about classes in memory for "reflection" (I'm
    > thinking of Java in this case) -- at least not without getting your
    > hands dirty (building registries, registering types, etc by yourself).


    Well this can be done easily enough with boost::lambda.
    Or somewhat less easily by defining factory functions.

    typedef A* (*factory)();
    A* makeInstance_B(){ return new B(); }
    A* makeInstance_C(){ return new C(); }

    void Create(factory f){
    A* a=f();
    a->doSomething();
    delete a;
    }

    Create(&makeInstance_B);
    Create(&makeInstance_C);

    --
    __Pascal Bourguignon__
    Pascal J. Bourguignon, Sep 28, 2009
    #3
  4. feribiro

    feribiro Guest

    Thank you guys!
    feribiro, Sep 29, 2009
    #4
    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. E11
    Replies:
    1
    Views:
    4,720
    Thomas Weidenfeller
    Oct 12, 2005
  2. dpr

    "new" vs "new class"

    dpr, Feb 12, 2004, in forum: C++
    Replies:
    6
    Views:
    313
    Rolf Magnus
    Feb 13, 2004
  3. christopher diggins
    Replies:
    16
    Views:
    738
    Pete Becker
    May 4, 2005
  4. Joseph Turian
    Replies:
    5
    Views:
    578
  5. Replies:
    2
    Views:
    442
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page