关于继承的愚蠢问题 [英] silly question about inheritance

查看:79
本文介绍了关于继承的愚蠢问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




假设这种情况:


class primitive

{

virtual void IsPointInside()= NULL;

};

class sphere:public primitive

{...};

class tetrahedron:public primitive

{...};


std :: vector< sphere> v_sphere;

std :: vector< tetrahedron> v_tetrahedron;


//我可以这样做:


std :: vector< primitive *> v_pPrimitives;

for(i = v_sphere.begin; i!= v_sphere.end(); ++ i)

v_pPrimitives.push_back(& * i); < br $>
....


//并调用v_pPrimitives [i] .IsPointInside(); ??


< head scratch>


我不能在一个阵列中存储不同的类型,但我可以存储它们的

指针,没有!?


-

-Gernot

int main(int argc, char ** argv){printf

("%silto%c%cf%cgl%ssic%ccom%c"," ma",58,''g'',64," ; ba,46,10);}


________________________________________

寻找好游戏?亲自动手吧!

GLBasic - 你可以这样做
www.GLBasic .com

推荐答案



" Gernot Frisch" < Me@Privacy.net>在消息中写道

新闻:2u ************* @ uni-berlin.de ...

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2u*************@uni-berlin.de...


假设这种情况:

类原语
{void void IsPointInside()= NULL;


NULL不保证为0,所以


virtual void IsPointInside()= 0;

};
类球体:公共原语
{...};
类四面体:公共原语
{...};

标准:: vector< sphere> v_sphere;
std :: vector< tetrahedron> v_tetrahedron;

//我可以这样做:

std :: vector< primitive *> v_pPrimitives;
for(i = v_sphere.begin; i!= v_sphere.end(); ++ i)
v_pPrimitives.push_back(& * i);
...

//并调用v_pPrimitives [i] .IsPointInside(); ??


v_pPrimitives [i] - > IsPointInside();

< head scratch>

我不能存储单个数组中的不同类型,但是我可以存储它们的指针,不行!?
Hi,

assume this situation:

class primitive
{
virtual void IsPointInside() =NULL;
NULL isn''t guaranteed to be 0, so

virtual void IsPointInside() = 0 ;
};
class sphere : public primitive
{ ... };
class tetrahedron : public primitive
{ ... };

std::vector<sphere > v_sphere;
std::vector<tetrahedron> v_tetrahedron;

// Can I do this:

std::vector<primitive*> v_pPrimitives;
for(i=v_sphere.begin; i!=v_sphere.end(); ++i)
v_pPrimitives.push_back(&*i);
...

// and call the v_pPrimitives[i].IsPointInside(); ??
v_pPrimitives[i]->IsPointInside();

<head scratch>

I can''t store different types in a single array, but I can store their
pointers, no!?




我会说你所存储的只是原始*,但由于那些

primitive *将指向类型派生自原始的对象,你可以使用通常的虚函数机制。


这真的没有什么不同


原语* p = new sphere();

p-> IsPointInside();


john



Well I would say that all you are storing is primitive*, but since those
primitive* will be pointing to objects whose type derives from primitive you
can use the usual virtual function mechanism.

It''s really no different to this

primitive* p = new sphere();
p->IsPointInside();

john




" Gernot Frisch" < Me@Privacy.net>在消息中写道

"Gernot Frisch" <Me@Privacy.net> wrote in message


假设这种情况:

类原语
{void void IsPointInside()= NULL;
};
类球体:公共原语
{...};
类四面体:公共原语
{...};

std :: vector< sphere> v_sphere;
std :: vector< tetrahedron> v_tetrahedron;

//我可以这样做:

std :: vector< primitive *> v_pPrimitives;
for(i = v_sphere.begin; i!= v_sphere.end(); ++ i)


i = v_sphere.begin()

v_pPrimitives.push_back(& * i);
...
//并调用v_pPrimitives [i] .IsPointInside(); ??
Hi,

assume this situation:

class primitive
{
virtual void IsPointInside() =NULL;
};
class sphere : public primitive
{ ... };
class tetrahedron : public primitive
{ ... };

std::vector<sphere > v_sphere;
std::vector<tetrahedron> v_tetrahedron;

// Can I do this:

std::vector<primitive*> v_pPrimitives;
for(i=v_sphere.begin; i!=v_sphere.end(); ++i)
i=v_sphere.begin()
v_pPrimitives.push_back(&*i); ...
// and call the v_pPrimitives[i].IsPointInside(); ??




v_pPrimitives [i] - > IsPointInside()


我现在没有看到任何问题。


Sharad



v_pPrimitives[i]->IsPointInside()

I don''t see any problem now.

Sharad


这真的没什么不同

primitive * p = new sphere();
p-> IsPointInside();
It''s really no different to this

primitive* p = new sphere();
p->IsPointInside();




一秒......然后,如果我有a

std :: vector< primitive> prims;


我可以push_back一个球体,这将保留所有额外的球体

特定信息吗?


我开始以为我从未真正了解过遗产......



One second... Then, if I have a
std::vector<primitive> prims;

can I push_back a sphere, which will keep all the additional sphere
specific information?

I get to thinking I never really understood about inheritances...


这篇关于关于继承的愚蠢问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆