嗯...继承......嗯 [英] Hmm... inheritence... hmmm

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

问题描述



一直在考虑以下事项:


class Mammal

{

public:


虚拟无效伴侣(哺乳动物&)= 0;

};

以上对我来说似乎是理性的 - 如果有的话是否有资格成为哺乳动物,

然后它必须能够交配。


但是......那你就有了


class狗:公共哺乳动物

{

公众:


虚拟无效伴侣(Mammal&)

{

//等一下,我不会交配,除非

//它与另一只狗在一起!

}

};

有没有人遇到过这种情况?显然我的第一个念头是:

虚拟无效伴侣(Dog&);

但是这并没有超出基类功能......

我知道以下内容可行,但对我来说似乎有点无用:

#include< typeinfo>


虚拟无效伴侣(哺乳动物和哺乳动物)

{

//等一下,我不会交配,除非

//它与另一只狗!


尝试

{

Dog& doggy = dynamic_cast< Dog&>(哺乳动物);


//现在执行交配

}

catch(std: :bad_cast)

{


}

}

对此有何看法?

-JKop


Been thinking about the following:

class Mammal
{
public:

virtual void Mate(Mammal &) = 0;
};
The above seems rational to me - if something is to qualify as a Mammal,
then it must be able to mate.

But... then you have

class Dog : public Mammal
{
public:

virtual void Mate(Mammal &)
{
//Wait a minute, I''m not mating unless
//it''s with another dog!
}
};
Has anyone met this situation before? Obviously my first thought was:
virtual void Mate(Dog &);
but then that doesn''t overload the base class function...
I know the following would work, but it seems a bit inefficent to me:
#include <typeinfo>

virtual void Mate(Mammal &mammal)
{
//Wait a minute, I''m not mating unless
//it''s with another dog!

try
{
Dog& doggy = dynamic_cast<Dog&>(mammal);

//Now perform mating
}
catch( std::bad_cast )
{

}
}
Any thoughts on this?
-JKop

推荐答案



" JKop" < NU ** @ NULL.NULL> schrieb im Newsbeitrag

新闻:4A ******************* @ news.indigo.ie ...

"JKop" <NU**@NULL.NULL> schrieb im Newsbeitrag
news:4A*******************@news.indigo.ie...

一直在考虑以下几点:

类哺乳动物
公开:

虚拟空虚伴侣(Mammal&)= 0;
};

以上对我来说似乎是理性的 - 如果有资格成为哺乳动物,那么它必须能够交配。

但是......那么你就有了类狗:公共哺乳动物
{
公开:

虚拟无效伴侣(哺乳动物&) {
//等一下,我不会交配,除非
//与另一只狗交配!
}
};

有没有人遇到过这种情况?显然我的第一个念头是:

虚拟无效伴侣(Dog&);

但是这并没有超出基类功能......

我知道以下内容可行,但对我来说似乎有点无用:

#include< typeinfo>

虚拟无效伴侣(哺乳动物和哺乳动物) )
//等等一下,我不会交配,除非
//与另一只狗交配!

尝试
{狗狗& doggy = dynamic_cast< Dog&>(哺乳动物);

//现在进行交配
}
catch(std :: bad_cast)
{

}
}

对此有何看法?

Been thinking about the following:

class Mammal
{
public:

virtual void Mate(Mammal &) = 0;
};
The above seems rational to me - if something is to qualify as a Mammal,
then it must be able to mate.

But... then you have

class Dog : public Mammal
{
public:

virtual void Mate(Mammal &)
{
//Wait a minute, I''m not mating unless
//it''s with another dog!
}
};
Has anyone met this situation before? Obviously my first thought was:
virtual void Mate(Dog &);
but then that doesn''t overload the base class function...
I know the following would work, but it seems a bit inefficent to me:
#include <typeinfo>

virtual void Mate(Mammal &mammal)
{
//Wait a minute, I''m not mating unless
//it''s with another dog!

try
{
Dog& doggy = dynamic_cast<Dog&>(mammal);

//Now perform mating
}
catch( std::bad_cast )
{

}
}
Any thoughts on this?




实际恕我直言伴侣,不应该是会员功能,因为订单

在您的模型中似乎并不重要。

(非常类似于运营商+,......)


一般来说问题是多方法问题,不能直接用C ++语言结构解决
,因为C ++没有内置的

多种方法。在现代C ++设计中有一个很好的章节。来自

Andrei Alexandrescu如何实现Multimethods。


这通常使用函数查找表实现,您可以在其中简单地使用
"查找"给出一对操作数的配对操作。

(假设你需要运行时支持,因为你的虚拟建议,一个
编译时解决方案当然可以实现很多使用模板更容易

特化,其中非专用模板导致编译器错误,

这意味着所请求的配合未实现==不支持)


问候

Michael



Actual IMHO "mate", should not be a member function at all, as the order
seems not important within your model.
(Quite similar to operator +, ...)

And the problem in general is a "multi method problem", which can not be
solved by C++ language constructs directly as C++ does not have built in
multi methods. There is a nice chapter within "Modern C++ Design" from
Andrei Alexandrescu how to implement Multimethods.

This is often implemented with a function lookup table, where you can simply
"lookup" the mate operaion given a pair of operands.
(assumed you need the runtime support, as your "virtual" suggested, a
compiletime solution can of course be implemented much easier with template
specialisation, where the not specialized template causes an compiler error,
which means that the requested mate is not implemented==not supported)

Regards
Michael


JKop< NU ** @ NULL.NULL>写道:
JKop <NU**@NULL.NULL> wrote:
一直在考虑以下内容:

类哺乳动物
{
公开:

虚拟void Mate(Mammal&)= 0;
};

以上对我来说似乎是理性的 - 如果有资格作为哺乳动物,
那么它必须能够伴侣。


你错了。为了使上述理性,每个哺乳动物必须能够与其他所有哺乳动物交配。但事实并非如此。


但是...那么你就有了类狗:公共哺乳动物
{
公开:

虚拟无效伴侣(哺乳动物&)
//等等一下,我不会交配,除非
//与另一只狗交配!
}
};

以前有人遇到过这种情况吗?


这与经典的矩形/方形问题相同。即使上面的

也不起作用......


无趣的乐趣(Dog& boy,Dog& boy){

boy.mate(男孩);

}


....

对此有何看法?
Been thinking about the following:

class Mammal
{
public:

virtual void Mate(Mammal &) = 0;
};
The above seems rational to me - if something is to qualify as a Mammal,
then it must be able to mate.
You are wrong. For the above to be rational, every Mammal must be able
to mate with every other Mammal; which is not the case.

But... then you have

class Dog : public Mammal
{
public:

virtual void Mate(Mammal &)
{
//Wait a minute, I''m not mating unless
//it''s with another dog!
}
};
Has anyone met this situation before?
This is the same as the classic rectangle/square problem. Even the above
doesn''t work...

void fun( Dog& boy, Dog& boy ) {
boy.mate( boy );
}

....
Any thoughts on this?




你试图概括一下没有概括的地方。



You are trying to generalize where no generalization is possible.


JKop写道:
JKop wrote:
一直在思考以下内容:

类哺乳动物



并立即在21天内自学C ++作者:Jesse Liberty来了我的脑海。


这是我的第一本C ++书。 :-)如果你看过它,请继续阅读。


{
公开:

虚拟无效伴侣(哺乳动物&)= 0;
};

以上对我来说似乎是理性的 - 如果有资格成为哺乳动物,那么它必须能够交配。

但是......那么你就有了类狗:公共哺乳动物
{
公开:

虚拟无效伴侣(哺乳动物&) {
//等一下,我不会交配,除非
//与另一只狗交配!
}
};

有没有人遇到过这种情况?显然我的第一个念头是:

虚拟无效伴侣(Dog&);

但是这并没有超出基类功能......

我知道以下内容可行,但对我来说似乎有点无用:

#include< typeinfo>

虚拟无效伴侣(哺乳动物和哺乳动物) )
//等等一下,我不会交配,除非
//与另一只狗交配!

尝试
{狗狗& doggy = dynamic_cast< Dog&>(哺乳动物);

//现在进行交配
}
catch(std :: bad_cast)
{

}
}

对此有何想法?
Been thinking about the following:

class Mammal

And immediately "Teach Yourself C++ in 21 Days" by Jesse Liberty comes
to my mind.

This was my first C++ book. :-) If you read it, keep reading it.

{
public:

virtual void Mate(Mammal &) = 0;
};
The above seems rational to me - if something is to qualify as a Mammal,
then it must be able to mate.

But... then you have

class Dog : public Mammal
{
public:

virtual void Mate(Mammal &)
{
//Wait a minute, I''m not mating unless
//it''s with another dog!
}
};
Has anyone met this situation before? Obviously my first thought was:
virtual void Mate(Dog &);
but then that doesn''t overload the base class function...
I know the following would work, but it seems a bit inefficent to me:
#include <typeinfo>

virtual void Mate(Mammal &mammal)
{
//Wait a minute, I''m not mating unless
//it''s with another dog!

try
{
Dog& doggy = dynamic_cast<Dog&>(mammal);

//Now perform mating
}
catch( std::bad_cast )
{

}
}
Any thoughts on this?




class Mammal

{

public:


虚拟无效伴侣(哺乳动物&)= 0;

};


class狗:公共哺乳动物

{

公开:


狗(哺乳动物&){}

狗(){}


虚拟无效伴侣(哺乳动物& x)

{

狗d = x;


// ...

}

};


-

Ioannis Vranos

http://www23.brinkster.com/noicys


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

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