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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:53:41+00:00 2026-06-10T03:53:41+00:00

Consider the following invalid C++ code. #include <assert.h> class NodeInterface { public: virtual ~NodeInterface

  • 0

Consider the following invalid C++ code.

#include <assert.h>

class NodeInterface {
public:
    virtual ~NodeInterface () {}

    virtual int f (const int& n) const = 0;
};

class ChildNodeInterface : public NodeInterface {
public:
    virtual ~ChildNodeInterface () {}
};

class ParentNodeInterface : public NodeInterface {
public:
    virtual ~ParentNodeInterface () {}
};

class ChildNode : public ChildNodeInterface {
public:
    virtual ~ChildNode () {}

    virtual int f (const int& n) const {
        return 2*n;
    }
};

class ParentNode : public ParentNodeInterface, private ChildNodeInterface {
public:
    explicit ParentNode () :
        mChild (new ChildNode ())
    {
    }

    virtual ~ParentNode () {}

    ChildNodeInterface* GetChildHandle () {
        return this;
    }

    virtual int f (const int& n) const {
        return 3*n;
    }

private:
    ChildNode* const mChild;

    // How do I specify that I would like to override ChildNodeInterface::f?
    virtual int f (const int& n) const { // On MSVC2010: C2535 member function already defined or declared
        return 1 + mChild->f (n);
    }
};

int main()
{
    ParentNode parent;
    assert (parent.f (2) == 6);
    ChildNode node;
    assert (node.f (2) == 4);
    ChildNodeInterface* child (parent.GetChildHandle ());
    assert (child->f (2) == 5);
    return 0;
}

It is my aim to make ParentNode privately look like a ChildNode, so that it can add some additional functionality on top of ChildNode‘s implementation of ChildNodeInterface. ParentNode could thereby effectively be treated as a ChildNode-handle in disguise, indicated by the simplicity of GetChildHandle.
Clearly, if ParentNode would not repeatedly inherit from NodeInterface, there would be no problem. Since, one can then easily disambiguate the overrides. This is illustrated by the following correct example:

#include <assert.h>

class ChildNodeInterface {
public:
    virtual ~ChildNodeInterface () {}

    virtual int ChildMethod (const int& n) const = 0;
};

class ParentNodeInterface {
public:
    virtual ~ParentNodeInterface () {}

    virtual int ParentMethod (const int& n) const = 0;
};

class ChildNode : public ChildNodeInterface {
public:
    virtual ~ChildNode () {}

    virtual int ChildMethod (const int& n) const {
        return 2*n;
    }
};

class ParentNode : public ParentNodeInterface, private ChildNodeInterface {
public:
    explicit ParentNode () :
        mChild (new ChildNode ()),
        mValue (1)
    {
    }

    ChildNodeInterface* GetChildHandle () {
        return this;
    }

    virtual int ParentMethod (const int& n) const {
        return 3*n;
    }

private:
    ChildNode* const mChild;
    const int mValue;

    virtual int ChildMethod (const int& n) const {
        return mValue + mChild->ChildMethod (n);
    }
};

int main()
{
    ParentNode parent;
    assert (parent.ParentMethod (2) == 6);
    ChildNode node;
    assert (node.ChildMethod (2) == 4);
    ChildNodeInterface* child (parent.GetChildHandle ());
    assert (child->ChildMethod (2) == 5);
    return 0;
}

However, in the special case where ParentNodeInterface and ChildNodeInterface both inherit from NodeInterface, the ambiguity arises.
As should be clear from the assertions in main, I do not aim for virtual inheritance of NodeInterface. It is my intention to have truly distinct implementations of NodeInterface::f in ParentNode.
I wonder how (if possible) I can discriminate between the implementation of ParentNodeInterface::f and ChildNodeInterface::f in ParentNode.

  • 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-10T03:53:42+00:00Added an answer on June 10, 2026 at 3:53 am

    I think your design is wrong. You are inherited ChildNodeInterface and you have a ChildNode member in your ParentNode Class?

    Inherited ChildNodeInterface doesn’t allow you to use f function implementation since ChildNode class is not in the inheritance tree.

    Redifining twice a function with the same signature is not allowed by C++. So writing 2 implementation of the same virtual function can’t be done.

    So you have 2 options:

    1. [Best] Try to redefine your class design using inheritance in the correct way. Diamond inheritance is often due to a bad design.
    2. [Fastest] Remove inheritance from ChildNodeInterface and prefer calling mChild->f() in the ParentNode::f() function to add the Child behavior to the parent:

      virtual int f (const int& n) const
      {
      return 3*n + mChild->f(n);
      }
      But I don’t know if it’s the behavior you want.

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

Sidebar

Related Questions

Consider the following code. #include <stdio.h> #include <vector> #include <iostream> struct XYZ { int
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider following 2 programs giving same error First calss: public class Testing { Testing
Consider the following code. class MainWindow { // ... Phonon::MediaObject media; Phonon::AudioOutput audio_output; };
Consider the following: class Example : boost::noncopyable { HANDLE hExample; public: Example() { hExample
Consider following simple DAO example: public abstract class DAOFactory { public abstract AccountDAO getAccountDAO();
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider the following code: int main() { char* s = (char*) malloc(sizeof(char)*10); s=hello; free(s);
Consider the constant in the following Java snippet : public class ConsumerServiceTestFixture { private
Consider the following model and controller: public class SimpleModel { [Required(ErrorMessage=Email Address is required.)]

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.