class myDate
{
public :
myDate(int month,int day, int year);
//...
}; myDate md(14,3,2011); 这样的初始化,是完全错误的,因为没有14个月份。
struct myDay
{
explicit myDay(int d)
:val(d){}
int val;
};
struct myMonth
{
explicit myMonth(int d)
:val(d){}
int val;
};
struct myYear
{
explicit myYear(int d)
:val(d){}
int val;
};
此时,我们的Date类就为
class myDate
{
public :
myDate(const myMonth & m,const myDay &d,const myYear & y);
//...
};
myDate md(14,3,2011); ////会报错
myDate myD(myMonth(3),myDay(3),myYear(2011)); ////////正确struct myMonth
{
public:
static myMonth jan(){return myMonth(1);}
static myMonth Feb(){return myMonth(2);}
static myMonth mar(){return myMonth(3);}
//.....
static myMonth Oct(){return myMonth(10);}
////....
}; myDate myD(myMonth::Feb(),myDay(3),myYear(2011)); std::tr1::shared_ptr<Investment> creatInvestment()
{
std::tr1::shared_ptr<Investment> retVal(static_cast<Investment*>(0),getRidOfInvestment);
retVal=...;
retrun retVal;
}
static_cast<Investment* >(0),强制转换为指针类型。■ 好的接口很容易被正确使用,不容易被误用.你应该在你的所有接口中努力达成这些性质.
■ "促进正确使用"的办法包括接口的一致性,以及与内置类型的行为兼容.
■ "阻止误用"的办法包括建立新类型、限制类型上的操作、束缚对象值以及消除客户的资源管理责任.
■ tr1::shared_ptr支持定制型删除器.这可防范DLL问题,可被用来自动解除互斥锁等等.
1930



被折叠的 条评论
为什么被折叠?



