q0987
q0987

Reputation: 35992

How to understanding the meaning of C++03 13.5.3/2

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

struct B {
    virtual int operator= (int rhs)
    {
        m_iValue = rhs;
        return m_iValue;
    }

    virtual B& operator= (const B& rhs)
    {
        m_iValue = rhs.m_iValue;
        return *this;
    }

    int m_iValue;
};
struct D : B {
    virtual int operator= (int rhs)
    {
        m_iValue = rhs;
        return m_iValue;
    }

    virtual D& operator= (const B& rhs)
    {
        m_iValue = rhs.m_iValue;
        return *this;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    D dobj1;
    D dobj2;
    B* bptr = &dobj1;

    bptr->operator=(99);     // calls D::operator=(int)
    *bptr = 99;              // ditto
    bptr->operator=(dobj2);  // calls D::operator=(const B&)
    *bptr = dobj2;           // ditto
    dobj1 = dobj2;           // calls implicitly-declared
                             // D::operator=(const D&)
    return 0;
}

Question 1> This question maybe is related to the question 2&3.

Reference: C++03 13.5.3/2

Note: for a derived class D with a base class B for which a virtual copy assignment has been declared, the copy assignment operator in D does not override B’s virtual copy assignment operator.

What does the following statement mean in plain English?

the copy assignment operator in D does not override B’s virtual copy assignment operator.

Question 2> why the following statement call `D::operator=(int)

    *bptr = 99;              // ditto

Question 3> Why the following statement call implicit D::operator=(const D&)

    dobj1 = dobj2;           // calls implicitly D::operator=(const D&)

Upvotes: 0

Views: 108

Answers (1)

n. m. could be an AI
n. m. could be an AI

Reputation: 120079

Question 1. For B, the copy assignment operator is B::operator=(const B&) or similar. For D it's D::operator=(const D&). One cannot override the other because argument types are different.

Upvotes: 1

Related Questions