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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:07:39+00:00 2026-05-16T22:07:39+00:00

I have encountered the following problem which proved to me that I know far

  • 0

I have encountered the following problem which proved to me that I know far too little about the workings of C++.

I use a base class with pure virtual functions

class Base
    ...

and a derived classes of type

class Derived : public Base{
private:
  Foo* f1;
 ...

Both have assignment operators implemented. Among other things, the assignment operator for Derived copies the data in f1. In my code, I create two new instances of class Derived

Base* d1 = new Derived();
Base* d2 = new Derived();

If I now call the assignment operator

*d1 = *d2;

the assignment operator of Derived is not called, and the data in f1 is not copied! It only works if I do

*dynamic_cast<Derived*>(d1) = *dynamic_cast<Derived*>(d2);

Can someone explain why the assignment operators are not overloaded?

Thanks!

  • 1 1 Answer
  • 3 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-16T22:07:39+00:00Added an answer on May 16, 2026 at 10:07 pm

    It’s hard to say without seeing the relevant code. Here’s an example that works:

    #include <iostream>
    
    using namespace std;
    
    class A {
      public:
        virtual A& operator=(A& a) {}
    };
    
    class B : public A {
      public:
        B& operator=(A& a) { cout << "B" << endl; }
    };
    
    int main() {
      A* foo = new B();
      A* bar = new B();
      *foo = *bar;
      return 0;
    }
    

    This will print B when run.

    Things that you might be doing wrong:

    1. You might have forgotten to make operator= virtual in the base class.
    2. You might have given the child’s class operator= as signature that’s more restrictive than that of the parent’s operator=, so you’re not actually overriding the parent’s definition. For example if you change B& operator=(A& a) { cout << "B" << endl; } to B& operator=(B& a) { cout << "B" << endl; } in the example above, it will no longer print B.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following problem that other people must have encountered. I am working
I'm learning C# and I encountered the following problem. I have two classes: base
More than one time I have encountered the following problem when building code with
I am just starting working with jquery and have encountered the following problem <div
I'm new to PHP, HTML and MySQL, and have encountered the following problem: I
Guys, please help me with the following problem. I have encountered the famous cannot
I am encountering a problem which I have never encountered before. I am working
I am developing a silverlight navigation application and have encountered the following problem. I
I have encountered a slightly unusual problem. Consider the following code: class parser {
I'm getting started with Qt and have encountered the following issue: when i compile

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.