Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiating Derived objects from the Base class

Trying to create a Driver type class where, below, Base is the Driver that is passed a type at instantiation. The type, the 2 in this case, is used to construct the correct derived object.

My compiler is throwing a Declaration syntax error on the "Class Base" line.

My end goal is to be able to do this:

Base *B;

    B = new Base(2);
    if(B)
    {
      B->DoStuff();
      B->DoMoreStuff();
      delete B;
    }

Here is my code that won't compile...

class Base
{
public:

    Base(int h);
    virtual ~Base();

private:
    int hType;
    Base *hHandle;
};


class Derived1 : public Base
{
public:
    Derived1();
    virtual ~Derived1();

};

class Derived2 : public Base
{
public:
    Derived2();
    virtual ~Derived2();

};

Base::Base(int h)
{
    hType = h;

    switch(h)
    {
        case 1:
            hHandle = new Derived1;
        break;

        case 2:
            hHandle = new Derived2;
        break;

    }
}


Derived1::Derived1():Base(1)
{
    printf("\nDerived1 Initialized\n\n");
}

Derived2::Derived2():Base(2)
{
    printf("\nDerived2 Initialized\n\n");
}

Below is updated code to show the full source. I think I now understand why it will not compile. As is pointed out below, I have an endless loop of calls to 'new'

#include <stdio.h>

class Base
{
public:

    Base();
    Base(int h);
    Create (int h);
    virtual ~Base();

private:
    int hType;
    Base *hHandle;
};


class Derived1 : public Base
{
public:
    Derived1();
    virtual ~Derived1();

};

class Derived2 : public Base
{
public:
    Derived2();
    virtual ~Derived2();

};

Base::Base()
{

}

Base::Base(int h)
{
    Create(h);
}

Base::Create(int h)
{
    hType = h;

    switch(h)
    {
        case 1:
            hHandle = new Derived1;
        break;

        case 2:
            hHandle = new Derived2;
        break;

    }
}

Derived1::Derived1()
{
    printf("\nDerived1 Initialized\n\n");
}

Derived2::Derived2()
{
    printf("\nDerived2 Initialized\n\n");
}
like image 616
Eric Avatar asked Nov 04 '22 23:11

Eric


1 Answers

It looks like you're trying to do a class factory.

I'd recommend that you have a static method in Base that returns either Derived1 or Derived2.

class Base
{
public:
    static Base* Create(int);
    virtual void DoStuff() = 0;
}

class Derived1 : public Base
{
    Derived1()
    {
        printf("\nDerived1 Initialized\n\n");
    }

    virtual void DoStuff()
    {
    }   
}

class Derived2 : public Base
{
    Derived2()
    {
        printf("\nDerived2 Initialized\n\n");
    }

    virtual void DoStuff()
    {
    }   
}

Base* Base::Create(int n)
{
    if (n==1)
        return new Derived1();
    else if (n==2)
        return new Derived2();
    else
        return nullptr;
}

void main()
{
    Base* B = Base::Create(2);
    if(B)
    {
        B->DoStuff();
        delete B;
    }
}
like image 163
Josh Brown Avatar answered Nov 12 '22 15:11

Josh Brown