由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教c++ interface class问题
相关主题
question about const referenceC++问题几个
[合集] 为什么 const member 不能是 static.why use static function here?
C++ questionC++ question
C++ 关于 Named Ctor Idom 的小问题C++ cast 小结
一道c++的考古题请问如何把初始化一个const 的vector (or array) in a class?
C++ (direct vs indirect initialization)C++默认的copy constructor的疑惑
const objectWhat're the three types of memory allocated for C++ variables?
关于C/C++里的Static variable的memory allocation/initializac++里面caveats太多了
相关话题的讨论汇总
话题: person话题: realperson话题: std话题: static话题: create
进入Programming版参与讨论
1 (共1页)
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.
1 (共1页)
进入Programming版参与讨论
相关主题
c++里面caveats太多了一道c++的考古题
c++ 语法C++ (direct vs indirect initialization)
c++ 弱问题:static const char* const 这两个const 分别是什么意思?const object
static vector 怎么 initialize ?关于C/C++里的Static variable的memory allocation/initializa
question about const referenceC++问题几个
[合集] 为什么 const member 不能是 static.why use static function here?
C++ questionC++ question
C++ 关于 Named Ctor Idom 的小问题C++ cast 小结
相关话题的讨论汇总
话题: person话题: realperson话题: std话题: static话题: create