Can a base class be its own factory?

Discussion in 'C++' started by shaun roe, Mar 9, 2006.

  1. shaun roe

    shaun roe Guest

    If I have a baseclass A and have subclasses B and C, can I make a
    parametrized constructor for A, say A(chooseClass), which then uses the
    parameter to do:


    A * p_theBaseclass;
    if (chooseClass == 0){
    p_theBaseclass = new B;
    } else {
    p_theBaseclass = new C;
    }



    and then for all the methods which A describes, delegate the
    implementation to B or C? so for example if they all have method
    'whoAmI', I could do:

    A * p_myClass = new A(0);
    cout<< p_myClass->whoAmI()<<endl; //returns 'I am B'

    A * p_myClass = new A(1);
    cout<< p_myClass->whoAmI()<<endl; //returns 'I am C'


    or Does It Get A Little Confused?

    this is all off the top of my head so probably full of craziness and
    mistakes.


    cheers
    s
    shaun roe, Mar 9, 2006
    #1
    1. Advertising

  2. shaun roe

    Noah Roberts Guest

    shaun roe wrote:
    > If I have a baseclass A and have subclasses B and C, can I make a
    > parametrized constructor for A, say A(chooseClass), which then uses the
    > parameter to do:
    >
    >
    > A * p_theBaseclass;
    > if (chooseClass == 0){
    > p_theBaseclass = new B;
    > } else {
    > p_theBaseclass = new C;
    > }
    >
    >
    >
    > and then for all the methods which A describes, delegate the
    > implementation to B or C? so for example if they all have method
    > 'whoAmI', I could do:
    >
    > A * p_myClass = new A(0);
    > cout<< p_myClass->whoAmI()<<endl; //returns 'I am B'
    >
    > A * p_myClass = new A(1);
    > cout<< p_myClass->whoAmI()<<endl; //returns 'I am C'
    >
    >
    > or Does It Get A Little Confused?
    >
    > this is all off the top of my head so probably full of craziness and
    > mistakes.


    No. It would be a really bad idea to try and replace 'this' inside a
    constructor. Create a factory method and make it static.

    class A
    {
    static A * factory(params) { A * ret = 0; if (xxx) ret = new B();
    else ret = new C(); return ret; }
    };

    A * a = A::factory(params);

    You will notice a lot of dependencies here though. The base must know
    about all of its subclasses this way. Might want to look into an
    Abstract Factory instead.
    Noah Roberts, Mar 9, 2006
    #2
    1. Advertising

  3. shaun roe wrote:
    > If I have a baseclass A and have subclasses B and C, can I make a
    > parametrized constructor for A, say A(chooseClass), which then uses the
    > parameter to do:
    >
    >
    > A * p_theBaseclass;
    > if (chooseClass == 0){
    > p_theBaseclass = new B;
    > } else {
    > p_theBaseclass = new C;
    > }


    You can (or, rather, nothing is there to stop you), except that it's
    a BAD IDEA(tm).

    > and then for all the methods which A describes, delegate the
    > implementation to B or C? so for example if they all have method
    > 'whoAmI', I could do:
    >
    > A * p_myClass = new A(0);
    > cout<< p_myClass->whoAmI()<<endl; //returns 'I am B'
    >
    > A * p_myClass = new A(1);
    > cout<< p_myClass->whoAmI()<<endl; //returns 'I am C'
    >
    >
    > or Does It Get A Little Confused?
    >
    > this is all off the top of my head so probably full of craziness and
    > mistakes.


    If you have to make those three classes inter-related, perhaps it's better
    to make 'B' and 'C' _nested_ in 'A'...

    V
    --
    Please remove capital As from my address when replying by mail
    Victor Bazarov, Mar 9, 2006
    #3
    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. Medi Montaseri
    Replies:
    17
    Views:
    834
    Medi Montaseri
    Sep 3, 2003
  2. Saverio M.
    Replies:
    0
    Views:
    499
    Saverio M.
    Jul 3, 2006
  3. C#
    Replies:
    4
    Views:
    386
  4. thunk
    Replies:
    1
    Views:
    290
    thunk
    Mar 30, 2010
  5. thunk
    Replies:
    0
    Views:
    451
    thunk
    Apr 1, 2010
Loading...

Share This Page