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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:06:53+00:00 2026-05-27T14:06:53+00:00

I would like to have a C++ Interface that must be overridden (if this

  • 0

I would like to have a C++ Interface that must be overridden (if this is possible) when inherited. So far, I have the following:

class ICommand{

public:
    //  Virtual constructor. Needs to take a name as parameter
    //virtual ICommand(char*) =0;
    //  Virtual destructor, prevents memory leaks by forcing clean up on derived classes?
    //virtual ~ICommand() =0; 
    virtual void CallMe() =0;
    virtual void CallMe2() =0;
};

class MyCommand : public ICommand
{
public:
    // Is this correct?
    MyCommand(char* Name) { /* do stuff */ }
    virtual void CallMe() {}
    virtual void CallMe2() {}
};

I have purposely left how I think the constructor/destructor’s should be implemented in ICommand. I know if I remove the comments, it will not compile. Please could someone:

  1. Show me how to declare the constructor/destructor’s in ICommand and how they are meant to be used in MyCommand
  2. Have I set things up correctly in ICommand so that MyCommand must override CallMe and CallMe2.
  • 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-27T14:06:53+00:00Added an answer on May 27, 2026 at 2:06 pm

    C++ does not allow for virtual constructors. A simple implementation (without the virtual constructor) would look something like this:

    class ICommand {
    public:
        virtual ~ICommand() = 0;
        virtual void callMe() = 0;
        virtual void callMe2() = 0;
    };
    
    ICommand::~ICommand() { } // all destructors must exist
    

    Note that even a pure virtual destructor must be defined.

    A concrete implementation would look exactly like your example:

    class MyCommand : public ICommand {
    public:
        virtual void callMe() { }
        virtual void callMe2() { }
    };
    

    You have a couple of options for the constructor. One option is to disable the default constructor for ICommand, so that subclasses will have to implement a constructor that calls your ICommand constructor:

    #include <string>
    
    class ICommand {
    private:
        const std::string name;
        ICommand();
    public:
        ICommand(const std::string& name) : name(name) { }
        virtual ~ICommand() = 0;
        virtual void callMe() = 0;
        virtual void callMe2() = 0;
    };
    
    ICommand::~ICommand() { } // all destructors must exist
    

    A concrete implementation would now look something like this:

    class MyCommand : public ICommand {
    public:
        MyCommand(const std::string& name) : ICommand(name) { }
        virtual void callMe() { }
        virtual void callMe2() { }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this C# class structure that I would like to refactor to use
I have an interface that defines some methods I would like certain classes to
I have a class that implements multiple interfaces. I would like to register these
I would like to have a class that handles population of lists of data
Using Guava, I would like to have a map that has the following properties:
I have this weird question. I would like to know if it is possible
I have a problem that I would like have solved via a SQL query.
I would like to have a nice template for doing this in development. How
Let us say we have some code that looks like below: @interface SomeClass :
i would like to know if it is possible to have a function in

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.