Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4321636
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:46:16+00:00 2026-05-21T08:46:16+00:00

Trying to create a Driver type class where, below, Base is the Driver that

  • 0

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");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T08:46:17+00:00Added an answer on May 21, 2026 at 8:46 am

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a simple setting in Orchard that appears in the
I'm trying to execute a more or less advanced query with Mongoid that basically
I am trying to use Nhibernate with the Sql 2008 Geography type and am
I'm trying to use MongoDB with MATLAB. Although there is no supported driver for
I'm trying to write an Eclipse template which will create getters and setters when
I am trying to figure out if it is possible to create a custom
See the code below. The drive() is in the scope , I can drive
I am trying to configure JTA Transactions with Atomkios in Tomcat 5.5 for jndi
I have connected 2 mice to PC and I wish one mouse to work
I'm having a bit of trouble setting up NHibernate, and I'm not too sure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.