如何理解c++ 03 13.5.3/2的含义
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;
}
问题1>这个问题可能与问题2&3有关。
参考:c++ 03 13.5.3/2
注意:对于具有基类B的派生类D,其虚拷贝赋值已经声明,拷贝赋值操作符在D不能覆盖B的虚拟拷贝赋值操作符。
下面的语句用简单的英语是什么意思?
D中的拷贝赋值操作符不覆盖B的虚拷贝赋值运算符。
问题2>为什么下面的语句调用' D::operator=(int)
*bptr = 99; // ditto
问题3>为什么下面的语句调用隐式D::operator=(const D&)
dobj1 = dobj2; // calls implicitly D::operator=(const D&)
问题1。对于B
,拷贝赋值操作符为B::operator=(const B&)
或类似的操作符。对于D
,是D::operator=(const D&)
。一个不能覆盖另一个,因为参数类型不同。
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 如何理解C++标准N3337中的expr.const.cast子句8
- C++,我收到一个无法理解的编译器错误
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 理解c++中的引用
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- Python中的for循环与C++有何不同
- 通过实例理解std::move及其目的
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 试图理解类对象的行为
- 难以理解某些人解决IOI问题的源代码
- 有人能帮我理解这个c++代码吗
- 理解循环C++中的循环
- 理解这行C++指针代码
- 理解GCC中的std::pow实现
- 正在理解智能指针,但出现错误:未分配正在释放的指针
- 如何理解新的运算符重载?
- 为什么理解这个递归示例如此难以转化为直觉?
- 我正在尝试理解代码块中的这些错误
- 在C++中释放内存期间,迭代器与指针有何不同