我们可以operator++为预增量和后增量重载吗?即正确调用SampleObject++和++SampleObject结果.
class CSample {
public:
int m_iValue; // just to directly fetch inside main()
CSample() : m_iValue(0) {}
CSample(int val) : m_iValue(val) {}
// Overloading ++ for Pre-Increment
int /*CSample& */ operator++() { // can also adopt to return CSample&
++(*this).m_iValue;
return m_iValue; /*(*this); */
}
// Overloading ++ for Post-Increment
/* int operator++() {
CSample temp = *this;
++(*this).m_iValue;
return temp.m_iValue; /* temp; */
} */
};
Run Code Online (Sandbox Code Playgroud)
我们不能仅仅基于返回类型重载函数,并且即使我们将其视为允许的,它也不能解决问题,因为调用解析的模糊性.
由于提供了运算符重载以使内置类型的行为类似于用户定义的类型,为什么我们不能同时为我们自己的类型利用前置和后置增量.
请考虑以下代码:
#...
class A {};
class B: public A{};
class C: virtual public A{};
class D: virtual public C{};
// No More Classes
...
int _tmain(int argc, _TCHAR* argv[]) {
cout<<sizeof(A)<<" ";
cout<<sizeof(B)<<" ";
cout<<sizeof(C)<<" ";
cout<<sizeof(D)<<".";
...
}
Run Code Online (Sandbox Code Playgroud)
O/P:1 1 4 8.
题:
*.这是我的第一个问题,如果你发现任何错误,请纠正我.
由于明显的原因,下面的程序无法编译:
#include <iostream>
using namespace std;
class A {
public:
A() { pVirt(); }
virtual void pVirt() const = 0 { count<<"A::pVirt()"; }
};
int main() {
A aObj;
aObj.pVirt();
reutrn 0;
}
Run Code Online (Sandbox Code Playgroud)
问题: 1. 签名中的 0 "virtual void pVirt() const = 0" 是什么意思?,这表示 vtable 中的 NULL 内存偏移量还是只是语法约束?
我已经扫描了MSDN中的所有页面,但仍未找到以下内容.
请告诉我如何在Win32中执行此操作.