A**u 发帖数: 2458 | 1 小弟在读effective c++, 第31节
讲到interface class, 里面提到 factory function, 有些不明白, 请大家指点
下面是代码.
interface class
class Person{
public:
static std::tr1::shared_ptr create(const std::string& name);
virtual ~Person();
virtual std::string name() const = 0;
}
这里的create 就是书里说的factory 函数
class RealPerson: public Person{
public:
RealPerson(const std::string& name):thename(name){}
virtual ~RealPerson();
std::string name() const;
private:
std::string the name;
}
奇怪的是 create 实现为
std::tr1::shared_ptr Person::create(const std::string& name)
{
return std::tr1::shared_ptr(new RealPerson(name));
}
关于create的实现,我有几点不明白
1.
create的实现是基类Person的, 它怎么能调用继承类的构造函数呢
2
create是static函数,static函数应该不可以访问非static数据和函数, 这里不是调
用了ctor?
谢谢指教 | t****t 发帖数: 6806 | 2
you can call RealPerson::RealPerson anywhere, because it is public.
same: you can call RealPerson::RealPerson anywhere, because it is public.
static member function can visit non-static members, as long as it provide
an object. you can visit non-static members anywhere, as long as you provide
an object (and it is public, if you visit it from non-member).
for ctor, you don't even need an object, because ctor create one.
【在 A**u 的大作中提到】 : 小弟在读effective c++, 第31节 : 讲到interface class, 里面提到 factory function, 有些不明白, 请大家指点 : 下面是代码. : interface class : class Person{ : public: : static std::tr1::shared_ptr create(const std::string& name); : virtual ~Person(); : virtual std::string name() const = 0; : }
| A**u 发帖数: 2458 | 3 谢谢你的解答
一切都清除了
包子奉上
provide
【在 t****t 的大作中提到】 : : you can call RealPerson::RealPerson anywhere, because it is public. : same: you can call RealPerson::RealPerson anywhere, because it is public. : static member function can visit non-static members, as long as it provide : an object. you can visit non-static members anywhere, as long as you provide : an object (and it is public, if you visit it from non-member). : for ctor, you don't even need an object, because ctor create one.
| f******y 发帖数: 2971 | 4 Effective C++ is using tr1? Which version are you reading?
【在 A**u 的大作中提到】 : 小弟在读effective c++, 第31节 : 讲到interface class, 里面提到 factory function, 有些不明白, 请大家指点 : 下面是代码. : interface class : class Person{ : public: : static std::tr1::shared_ptr create(const std::string& name); : virtual ~Person(); : virtual std::string name() const = 0; : }
| A**u 发帖数: 2458 | 5 3rd 最新的
【在 f******y 的大作中提到】 : Effective C++ is using tr1? Which version are you reading?
| r*******y 发帖数: 1081 | 6 I tried this but it is not working:
class A{
static A * f(){A * p= new B(); return p;}
};
class B: public A{
};
So what is the reason? Thanks.
provide
【在 t****t 的大作中提到】 : : you can call RealPerson::RealPerson anywhere, because it is public. : same: you can call RealPerson::RealPerson anywhere, because it is public. : static member function can visit non-static members, as long as it provide : an object. you can visit non-static members anywhere, as long as you provide : an object (and it is public, if you visit it from non-member). : for ctor, you don't even need an object, because ctor create one.
| t****t 发帖数: 6806 | 7 declare first, use later. so put A::f() after B.
【在 r*******y 的大作中提到】 : I tried this but it is not working: : class A{ : static A * f(){A * p= new B(); return p;} : }; : class B: public A{ : }; : So what is the reason? Thanks. : : provide
| r*******y 发帖数: 1081 | 8 Thanks a lot.
【在 t****t 的大作中提到】 : declare first, use later. so put A::f() after B.
|
|