????C++???????????????????????“????”?????????C++???????????????????????????и?????????С?????????????СС??????
???????????????????????????????????
??????????????Effective C++?? lostmouse???????
class TestInt
{
public:
int GetData()const{ return i;};
TestInt(int ii):i(ii){}; //????????
private:
int i;
};
void fun(TestInt t)
{
cout<<t.GetData()<<endl;
}
int main()
{
fun(10);
return 0;
}
???????н????
????10
??????fun???????????TestInt???????????????????int ????????
????????????????????????????????????±???????????????error: conversion from ‘int’ to non-scalar type ‘TestInt
?????????е?ü????????????ó????????????????????й?????????C++?е?????????????
?????????????????fun?????int?????????????TestInt????????????????TestInt?????????int?????????????TestInt??
????????????????????????????????????????????????? const TestInt t??10?????????????????????
??????Effective C++????????????
????class Month {
????public:
????static const Month Jan() { return 1; }
????static const Month Feb() { return 2; }
????...
????static const Month Dec() { return 12; }
????int asInt() const           // ???????Month
????{ return monthNumber; }     // ?????????int
????private:
????Month(int number): monthNumber(number) {}
????const int monthNumber;
????};
????????????????ε????????????? static const Month Jan() { return 1; }
??????????????????к????????????????????Month????????????????int???
?????????????y?????????????????δε?????????ū?????????÷???
?????????????????????const???·??Month jan = Month::Jan(); ?????????????·?????
?????? static const Month Jan() { return 1; } ?????????????????????????????????????????
??????е?????????????????μ?month??