class C1
{
public:
void Modify() const
{
data = 1;
}
private:
mutable int data;
};
Mutable allow a member function who has const keyword end of function name to modify data member (modify as mutable) value.
2008年7月21日星期一
2008年7月17日星期四
The explicit modify
The explicit Keyword
C++A constructor declared with only one argument and without the explicit keyword is a converting constructor. You can construct objects with a converting constructor using the assignment operator. Declaring a constructor of this type with the explicit keyword prevents this behavior. The explicit keyword controls unwanted implicit type conversions. It can only be used in declarations of constructors within a class declaration. For example, except for the default constructor, the constructors in the following class are converting constructors.
class A
{ public:
A();
A(int);
A(const char*, int = 0);
};
The following declarations are legal.
A c = 1;
A d = "Venditti";
The first declaration is equivalent to A c = A(1).
If you declare the constructor of the class with the explicit keyword, the previous declarations would be illegal.
For example, if you declare the class as:
class A
{ public:
explicit A();
explicit A(int);
explicit A(const char*, int = 0);
};
You can only assign values that match the values of the class type.
For example, the following statements will be legal:
A a1;
A a2 = A(1);
A a3(1);
A a4 = A("Venditti");
A* p = new A(1);
A a5 = (A)1;
A a6 = static_cast<a>(1);
Ref:
http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=/com.ibm.vacpp7l.doc/language/ref/clrc06explicit_keyword.htm
C++A constructor declared with only one argument and without the explicit keyword is a converting constructor. You can construct objects with a converting constructor using the assignment operator. Declaring a constructor of this type with the explicit keyword prevents this behavior. The explicit keyword controls unwanted implicit type conversions. It can only be used in declarations of constructors within a class declaration. For example, except for the default constructor, the constructors in the following class are converting constructors.
class A
{ public:
A();
A(int);
A(const char*, int = 0);
};
The following declarations are legal.
A c = 1;
A d = "Venditti";
The first declaration is equivalent to A c = A(1).
If you declare the constructor of the class with the explicit keyword, the previous declarations would be illegal.
For example, if you declare the class as:
class A
{ public:
explicit A();
explicit A(int);
explicit A(const char*, int = 0);
};
You can only assign values that match the values of the class type.
For example, the following statements will be legal:
A a1;
A a2 = A(1);
A a3(1);
A a4 = A("Venditti");
A* p = new A(1);
A a5 = (A)1;
A a6 = static_cast<a>(1);
Ref:
http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=/com.ibm.vacpp7l.doc/language/ref/clrc06explicit_keyword.htm
2008年7月3日星期四
2008年7月2日星期三
point to member function 成员函数指针
class CA
{
public:
CA(void (*pfn)(int));
};
class CB
{
public:
CB()
{
CA a(&Fx1);
};
public:
void Fx1(int)
{};
};
/////////////
error C2276: '&' : illegal operation on bound member function expression
solution:
class CA;
class CB
{
public:
CB(void (CA::*)()){};
};
class CA
{
public:
CA()
{
CB b(&CA::Fx);
};
void Fx();
};
{
public:
CA(void (*pfn)(int));
};
class CB
{
public:
CB()
{
CA a(&Fx1);
};
public:
void Fx1(int)
{};
};
/////////////
error C2276: '&' : illegal operation on bound member function expression
solution:
class CA;
class CB
{
public:
CB(void (CA::*)()){};
};
class CA
{
public:
CA()
{
CB b(&CA::Fx);
};
void Fx();
};
2008年6月30日星期一
virtual inherit
class B
{
void Fb(){...};
};
class CA : virtual public B
{
void Fca(){...};
};
class CB : virtual public B
{
void Fcb(){...};
};
class CS: public CA, public CB
{
Fb();
};
B
----
CA
----
CB
----
CS
Err:
class B
{
public:
void Fb()
{
printf("Fbfffffffffffffffffffffffffffffffffffffffffffffffff");
}
};
class CA : public B
{
};
class CB : public B
{
};
class CS: public CA, public CB
{
public:
void Do()
{
Fb();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
CS c;
c.Do();
return 0;
}
//////////////////////////////////////////////////////
1>f:\project\rreg\tt\tt.cpp(28) : error C2385: ambiguous access of 'Fb'
1> could be the 'Fb' in base 'B'
1> or could be the 'Fb' in base 'B'
1>f:\project\rreg\tt\tt.cpp(28) : error C3861: 'Fb': identifier not found
{
void Fb(){...};
};
class CA : virtual public B
{
void Fca(){...};
};
class CB : virtual public B
{
void Fcb(){...};
};
class CS: public CA, public CB
{
Fb();
};
B
----
CA
----
CB
----
CS
Err:
class B
{
public:
void Fb()
{
printf("Fbfffffffffffffffffffffffffffffffffffffffffffffffff");
}
};
class CA : public B
{
};
class CB : public B
{
};
class CS: public CA, public CB
{
public:
void Do()
{
Fb();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
CS c;
c.Do();
return 0;
}
//////////////////////////////////////////////////////
1>f:\project\rreg\tt\tt.cpp(28) : error C2385: ambiguous access of 'Fb'
1> could be the 'Fb' in base 'B'
1> or could be the 'Fb' in base 'B'
1>f:\project\rreg\tt\tt.cpp(28) : error C3861: 'Fb': identifier not found
订阅:
博文 (Atom)