前一点你可能是对了。
后一点你错了, 类中可以嵌套类声明的。
这个代码的目的在于模拟__property, 即把属性的set函数和get函数封装起来
举个例子:
class C{
struct Visible{
//Visible() //add default constructor if want to initialize to certain value
operator bool ()const{
return value;
}
bool operator=(bool v){
// 送出 before change的消息
value=v;
// 送出 after change 的消息
return value;
}
private:
bool value;
};
public:
Visible visible;
};
C c
有了上面的代码, 下面的代码都是合法的了
if(c.visible)
;
bool b=c.visible=false;
给end user的感觉就像一个成员变量一样亲切,而不是
if(c.get_visible)
;
c.set_visible(false);
bool b=c.get_visible();
而我碰到的问题是我要包装的是全局函数,比如
class C{
struct Visible{
//Visible() //add default constructor if want to initialize to certain value
operator bool ()const{
return some_global_function_which_rely_on_C_data( generate_C_this_from_this);
}
bool operator=(bool v){
// 送出 before change的消息
some_other_global_function_which_rely_on_C_data(generate_C_this_from_this,v);
// 送出 after change 的消息
return v;
}
//private:
// bool value; no longer required.
};
public:
Visible visible;
};