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

  • SEARCH
  • Home
  • 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 7906675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:04:46+00:00 2026-06-03T11:04:46+00:00

I have: class BASE{ public: virtual void func1() = 0; }; Then I have

  • 0

I have:

class BASE{
public:
    virtual void func1() = 0;
};

Then I have some derived classes, like:

class Derived1 : public BASE{
public:
    void func1() {...implementation....}
};

class Derived2 : public BASE{
    public:
        void func1() {...implementation....}
    };

In the main I would like to do something like (pseudo code):

if ( cond ){
   BASE b := new Derived1
}
else{
   BASE b := new Derived2
}
b.func1();

so that the func1() function invoked is the one specialized for the derived class.

I tried to do:

BASE b = new Derived1();

but the compiler complains.

Is it possible to do that in C++? How?

  • 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-06-03T11:04:48+00:00Added an answer on June 3, 2026 at 11:04 am

    There are two ways to do this:

    • Use a pointer of type base class:

      Base *b = new Derived1;

    • Use a const reference of type base class if you want to create a derived on copy construction:

      Base const& b = Derived;

    • Use a non-const reference of type base class if you want to assign a derived class object created elsewhere:

      Derived d;
      Base& b = d;

    Nits:

    • In order to inherit, you need to specify this in the class definition:

      class Derived1 : public Base {

    • Your functions need to have a return value.

      class B{
      public:
      virtual void func() = 0;
      };

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question, here are two classes below: class Base{ public: virtual void
Suppose I have a base and derived class: class Base { public: virtual void
I have these classes: class Base { public: virtual void foo(int x = 0)
I have 2 classes: class Base { public: virtual int Foo(int n); virtual void
I have two classes like this (simplified and renamed): class Base { public: virtual
I have a base class and several derived classes. The derived classes use some
If I have a base class: class Base { public: virtual void Test()=0; };
Say we have: class Base { virtual void f() {g();}; virtual void g(){//Do some
I have following situation class base { public: virtual void Accepted(SOCKET s) //// Event
I have seen this question http://www.careercup.com/question?id=384062 class Base { public : virtual void method

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.