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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:04:21+00:00 2026-05-14T06:04:21+00:00

all. I’m pretty new to C++, and I’m writing a small library (mostly for

  • 0

all. I’m pretty new to C++, and I’m writing a small library (mostly for my own projects) in C++. In the process of designing a type hierarchy, I’ve run into the problem of defining the assignment operator.

I’ve taken the basic approach that was eventually reached in this article, which is that for every class MyClass in a hierarchy derived from a class Base you define two assignment operators like so:

class MyClass: public Base {
public:
    MyClass& operator =(MyClass const& rhs);
    virtual MyClass& operator =(Base const& rhs);
};

// automatically gets defined, so we make it call the virtual function below
MyClass& MyClass::operator =(MyClass const& rhs);
{
    return (*this = static_cast<Base const&>(rhs));
}

MyClass& MyClass::operator =(Base const& rhs);
{
    assert(typeid(rhs) == typeid(*this)); // assigning to different types is a logical error
    MyClass const& casted_rhs = dynamic_cast<MyClass const&>(rhs);
    try {
        // allocate new variables
        Base::operator =(rhs);
    } catch(...) {
        // delete the allocated variables
        throw;
    }
    // assign to member variables
}

The part I’m concerned with is the assertion for type equality. Since I’m writing a library, where assertions will presumably be compiled out of the final result, this has led me to go with a scheme that looks more like this:

class MyClass: public Base {
public:
    operator =(MyClass const& rhs); // etc
    virtual inline MyClass& operator =(Base const& rhs)
    {
        assert(typeid(rhs) == typeid(*this));
        return this->set(static_cast<Base const&>(rhs));
    }
private:
    MyClass& set(Base const& rhs); // same basic thing
};

But I’ve been wondering if I could check the types at compile-time. I looked into Boost.TypeTraits, and I came close by doing BOOST_MPL_ASSERT((boost::is_same<BOOST_TYPEOF(*this), BOOST_TYPEOF(rhs)>));, but since rhs is declared as a reference to the parent class and not the derived class, it choked.

Now that I think about it, my reasoning seems silly — I was hoping that since the function was inline, it would be able to check the actual parameters themselves, but of course the preprocessor always gets run before the compiler. But I was wondering if anyone knew of any other way I could enforce this kind of check at compile-time.

  • 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-14T06:04:21+00:00Added an answer on May 14, 2026 at 6:04 am

    You can’t perform this assert at compile time for the simple reason that the run-time types won’t be known until, well, run time.

    assert(typeid(rhs) == typeid(*this));
    return this->set(static_cast<Base const&>(rhs));
    

    In the non-inline version you had dynamic_cast. I would retain this so that you get a well-defined error and not undefined behaviour if your assertion is violated.

    If you do this the assertion is either overly restrictive or pointless. The dynamic_cast will throw a bad_cast exception in both debug and release builds. This is what you want.

    Personally, I would question the whole polymorphic assignment issue. I would follow Scott Meyers’ Effective C++ advise and make all your non-leaf nodes in the inheritance hierarchy abstract. You can then make the base class assignment operators protected and non-virtual.

    This enables you to use their implementation in derived classes assignment operator but prevents clients from slicing objects. If a client class has only a base class reference or pointer it is questionable whether the should be attempting to assign to the class anyway. If the do, they should be responsible for the casting and type safety guarantees.

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

Sidebar

Related Questions

All, I currently have two projects that are under SourceSafe that I am unable
All the STL containers that implement resize use copies to populate the new elements
all, I started out with what i thought was going to be a pretty
I am writing a query to fetch results for all the values in a
All, What should be the approach to writing a thread safe program. Given a
All, In Apple's sample code DateCell http://developer.apple.com/library/ios/#samplecode/DateCell/Introduction/Intro.html the ivar pickerView is declared in MyTableViewController.h
All, I'm trying to split my MSTest test run into multiple runs because I'm
All, I'm working on a proof of concept replacement for the windows taskbar. (Mostly
All the articles I've found via google are either obsolete or contradict one another.
All the recent VisualSVN Server posts made me want to check it out. I

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.