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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:38:15+00:00 2026-06-13T06:38:15+00:00

I have the following: class Base { protected: std::string _name; public: virtual ~Base(){} const

  • 0

I have the following:

class Base
{
protected:
    std::string _name;

public:
    virtual ~Base(){}

    const std::string &name;

    Base()
        : _name ("(no name)")
        , name(_name)
    {}
};

template <typename T>
class BaseH : public Base
{
public:
    virtual ~BaseH() {}
    BaseH() : Base() {}

    T& operator~(){ ; return static_cast<T&>(*this);}
};

class One : public BaseH<One>
{
public:
    One() : BaseH<One>() { _name = "One"; }

};

class Two
    : public One
    , public BaseH<Two>
{
public:
    Two() : BaseH<Two>() { _name = "Two"; }

};


int main(int argc, char *argv[])
{
    std::cout << Two().name << std::endl;

    return 0;
}

I want to derive Two from both One and BaseH<Two>, because Two is a specialization of One, and the operator~ in BaseH must always return a reference of the type of the object that’s calling it.

Compilation errors are obviously:

In constructor ‘Two::Two()’:
  error: reference to ‘_name’ is ambiguous
  error: candidates are: std::string Base::_name
  error:                 std::string Base::_name
In function ‘int main(int, char**)’:
  error: request for member ‘name’ is ambiguous
  error: candidates are: const string& Base::name
  error:                 const string& Base::name

How do I make _name and name accessible in both One and Two, while setting the const references via constructor delegation? What is the cleanest way to do that?

  • 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-13T06:38:17+00:00Added an answer on June 13, 2026 at 6:38 am

    Looks like you’ve got the diamond problem. Have you tried virtual inheritance?

    e.g.:

    template <typename T>
    class BaseH : virtual public Base
    {
        // ...
    };
    
    class One : virtual public BaseH<One>
    {
        // ...
    };
    

    edit: further reading on the diamond problem here: http://www.cprogramming.com/tutorial/virtual_inheritance.html

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

Sidebar

Related Questions

I have the following scenario :- template <typename T> class Base { public: virtual
Let's say I have the following class hierarchy: class Base { protected: virtual void
I have the following class: class Tileset { //base class public: static std::vector<Tileset*> list;
assume the following class is given: class Base{ public: Base() {} Base( const Base&
I have the following method in a base class that implements System.ComponentModel.INotifyPropertyChanged: protected virtual
I have the following class: public abstract class TableServiceEntity { protected TableServiceEntity(); protected TableServiceEntity(string
I have the following classes: abstract class Transport{ protected String name; protected Transport(String name){
Suppose I have the following (trivially simple) base class: public class Simple { public
I have the following class: public class DocketType : Enumeration<DocketType, int, string> { public
I have the following two constructors in my base class: protected ExternalSystemException( params Object[]

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.