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 7005883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:24:26+00:00 2026-05-27T21:24:26+00:00

I usually program in C# but am trying to do a bit of C++

  • 0

I usually program in C# but am trying to do a bit of C++ and am struggling somewhat trying to implement interfaces in C++.

In C# I’d do something like this:

class Base<T>
{
    public void DoSomething(T value)
    {
        // Do something here
    }
}

interface IDoubleDoSomething
{
    void DoSomething(double value);
}

class Foo : Base<double>, IDoubleDoSomething
{
}

In C++ I’ve implemented it like this:

template <class T>
class Base
{
public:
    virtual void DoSomething(T value)
    {
        // Do something here
    }
};

class IDoubleDoSomething
{
public:
    virtual void DoSomething(double value) = 0;
};

class Foo : public Base<double>, public IDoubleDoSomething
{
};

The problem is that I cannot instantiate Foo because it is abstract (doesn’t implement DoSomething). I realise I can implement DoSomething and just call the method on Base but I was hoping there is a better way of doing this. I have other classes which inherit from base with different data types and I have other classes which inherit from IDoubleDoSomething which don’t use Base.

Any help appreciated.

  • 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-27T21:24:27+00:00Added an answer on May 27, 2026 at 9:24 pm

    As others have noted, the two functions in the base classes are unrelated (despite having the same name and argument types), as they have no common base class. If you want them to be related, you need to give them a common base class.

    Also, in general, if you want multiple inheritance to work properly, you need to declare your non-private base classes as virtual. Otherwise, if you ever have common base classes (as you often need for this style of code), bad things will happen.

    So given that, you can make your example work as follows:

    template <class T>
    class IDoSomething {
    public:
        virtual void DoSomething(T value) = 0;
    };
    
    template <class T>
    class Base : public virtual IDoSomething<T>
    {
    public:
        virtual void DoSomething(T value)
        {
            // Do something here
        }
    };
    
    class IDoubleDoSomething : public virtual IDoSomething<double>
    {
    };
    
    class Foo : public virtual Base<double>, public virtual IDoubleDoSomething
    {
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Usually when I need to fork in C, I do something like this: pid_t
As an exercise in Haskell, I'm trying to implement heapsort. The heap is usually
This might seem like a silly question, but after asking some questions on stackoverflow
I'm trying to set a callback function in Jack. This is usually quite simple,
I have this program I'm trying to reverse engineer. I don't know whether or
When I pass the options in the program (a computational biology experiment) I usually
I'm working on a C# windows program with Visual Studio 2008. Usually, I work
I'm trying to ask the user for a value of some variable, but at
I am trying to use this code for the Porter stemming algorithm in a
Experts !! I know this question is one of the lousy one , but

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.