C++对无名对象申明问题
学习C++转换运算符函数重载时碰到一个问题,就是网上一搜一堆的管宁的转换运算符函数重载代码
#include <iostream> using namespace std; class Test { public: Test(int a = 0) { cout<<this<<":"<<"载入构造函数!"<<a<<endl; Test::a = a; } Test(Test &temp) { cout<<"载入拷贝构造函数!"<<endl; Test::a = temp.a; } ~Test() { cout<<this<<":"<<"载入析构函数!"<<this->a<<endl; cin.get(); } operator int()//转换运算符,去掉则不会调用 { cout<<this<<":"<<"载入转换运算符函数的内存地址:"<<this->a<<endl; return Test::a; } public: int a; }; int main() { Test b=Test(99);//注意这里 cout<<"b的内存地址"<<&b<<endl; cout<<b.a<<endl; system("pause"); }