#include <string>
#include <iostream>
using namespace std;
class Outer {
class Inner {
public:
Inner(Outer& x) :parent(x) {}
void func() {
string a = "myconst1";
cout << parent.var << endl;
if (a == MYCONST)
{
cout << "string same" << endl;
}
else
{
cout << "string not same" << endl;
}
}
private:
Outer& parent;
};
public:
Outer()
:i(*this)// Outer object
, var(4)
{}
Outer(Outer& other)
:i(other)
, var(22)
{}
void func()
{
i.func();
}
private:
static const char* const MYCONST;
Inner i;
int var;
};
const char* const Outer::MYCONST = "myconst";
int main()
{
Outer o1;
Outer o2(o1);
o1.func();
o2.func();
return 0;
}
运行结果:
为什么不是以下结果 :
4
string not same
22
string not same