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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:47:18+00:00 2026-05-15T16:47:18+00:00

I’m quite confused about how to solve the following problem (search both on SO

  • 0

I’m quite confused about how to solve the following problem (search both on SO and google didn’t help much).

Say we have a class defining basic operators and a simple vector as only data and add only additional methods in inherited classes:

class Foo
{
    public:
        // this only copies the data
        Foo& operator=(const Foo& foo);

        // do something that computes a new Foo from *this
        Foo modifiedFoo();

    //..
    private:
        std::vector<int> data;
}

class Bar: public Foo
{
    public:
       void someNewMethod();
    //..
    // no new data
}

Inheritance now ensures that the operator= in the case bar1 = bar2 does the right thing.
But from the data point of view, both Bar and Foo are basically the same, so I’d like to be able to write both

Foo foo;
Bar bar;

foo = bar;  // this ...
bar = foo;  // .. and this;

and more specifically

bar = foo.modifiedFoo();

[
Edit:
And btw, this doesn’t work either obviously…

bar1 = bar2.modifiedFoo();

]

I thought it would be as easy as adding another Bar& operator=(const Foo & foo) in Bar, but somehow this is ignored (and I don’t like this anyways, what if I derive more and more classes?)

So what is the right way to go about this??

Thanks!! And sorry if this has been asked before.

  • 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-15T16:47:19+00:00Added an answer on May 15, 2026 at 4:47 pm

    Martin told you what went wrong, here’s what you should do instead:

    Bar bar;
    Foo& foo = bar;  // this works now
    

    Now foo is a reference to a an object, and you can have base class references (and pointers, BTW) refer to objects of derived classes.

    However, this

    bar = foo;
    

    will never work (at least not implicitly) and it shouldn’t. What you’re trying to do here is to assign a base class object (or reference) to a derived class.
    However, while a derived class can always stand in for a base class object, the opposite is not true. (You can’t just mindlessly use any vehicle when what you need is a boat, because not all vehicles are boats, the vehicle you’re using might be a car and you’d drown.)

    This is not to say that you cannot make it work. if you know how to create a derived class object from a base class object, you can write a function to do so. It’s just that the compiler doesn’t know how to make a car from a vehicle.

    As solipist has shown, such a conversion function can be a constructor. However, I would make conversion constructors explicit:

    class Bar: public Foo
    {
        public:
            explicit Bar(const Foo&);
        //
    }
    

    The explicit keyword makes sure that the compiler will never attempt to call this constructor unless you say so. If you remove it, then this code would compile:

    //void f(Foo); // you meant to call this, but forgot to include its header
    void f(Bar); // you didn't even know this was included
    
    Foo foo;
    f(foo);  // won't compile with the above Bar class
    

    and the compiler would silently generate this code: f(Bar(foo));
    While this seems handy at first, I think I have sooner or later been bitten by every single implicit conversions I allowed to creep into my code and had to remove them later. So many years ago I swore to never allow them anymore.
    Note that even with the explicit conversion constructor you can still call f(Bar) with a Foo object – you just have to say so explicitly:

    f(Bar(foo)); // compiles fine even with the explicit conversion constructor
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't have much knowledge about the IPv6 protocol, so sorry if the question
This could be a duplicate question, but I have no idea what search terms
I have been unable to fix a problem with Java Unicode and encoding. The
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.