哪个派生类是什么? [英] Which derived class is it?

查看:54
本文介绍了哪个派生类是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个函数,它将参数作为基类,但

的表现不同,具体取决于传递的派生类的类型。

我可以告诉哪个派生类被传递?


例如:


class Base

{// ... < br $>
};


类D1:公共基地

{// ...

};


D2级:公共基地

{// ...

};


void f(基数b)

{

//如果b是D1类型,则执行一项操作,如果b类型为D2,则执行另一项操作

}


这是正确的想法吗?或者我是否必须使用派生类重载的参数编写f()

的多个副本?


Brad

解决方案



" Brad Marts" <毫安*** @ phy.duke.edu>在留言中写道

news:pa **************************** @ phy.duke.edu .. 。

我希望有一个函数将参数作为基类,但
执行的方式不同,具体取决于传递的派生类的类型。
我可以告诉哪个派生类课程通过了?

例如:

班级基础
{// ...
};

班级D1:公共基地
{// ...
};

D2类:公共基地
{// ...
};

void f(基础b)
{
//如果b是D1类型,那么做一件事,如果b是D2类型则做另一件事
}
这是正确的想法吗?或者我是否必须使用派生类重载的参数编写f()
的多个副本?




我肯定不会写多个副本。您可能想要阅读更多

的多态性示例。这样做的好处就是你没有必要写b
写多份函数。另一方面,你通常不会想要检查子类型是什么。通常你想要做的就是实现不同的表现。通过调用传入的那些

对象上的函数。换句话说,你在子类本身中埋藏了不同的

性能。例如,在你的f()

函数中,你想做什么?让我们说它是显示关于

对象的东西。所以你写了一个名为display的函数。并将其放入Base并

将其设为虚拟。然后在D1和D2中再次定义这些函数,分别是
,但是你让它们做了两件不同的事情。例如,


void D1 :: display()

{

cout<< 这是D1类型的对象!;

}


无效D2 :: display()

{

cout<< 这是D2类型的对象!;

}


在你的f函数中,你只需写一下

b.display(),然后将显示正确的内容,具体取决于

类型,而无需您知道或关心代码。这里有一个问题

但是如果你按照这样的值传递Base参数你就不能这样做。

你必须通过引用传递它( Base& b),或pionter(Base * pB)。


在星期一,2003年12月15日10:34:48 -0700 in comp.lang.c ++," Brad Marts"

< ma *** @ phy.duke.edu>据称写了:

我希望有一个函数将参数作为基类,但
执行的方式取决于传递的派生类的类型。




Marshall Cline的C ++常见问题解答中涵盖了这个问题。请参阅主题

" [20]继承?虚函数在发布之前检查

常见问题总是好的。您可以在以下网址获取常见问题解答:
http:// www。 parashift.com/cpp-faq-lite/


Brad Marts< ma *** @ phy.duke.edu>在留言中写道

news:pa **************************** @ phy.duke.edu .. 。

我希望有一个函数将参数作为基类,但
执行的方式不同,具体取决于传递的派生类的类型。
我可以告诉哪个派生类课程通过了?

例如:

班级基础
{// ...
};

班级D1:公共基地
{// ...
};

D2类:公共基地
{// ...
};

void f(基础b)
{
//如果b是D1类型,那么做一件事,如果b是D2类型则做另一件事
}
这是正确的想法吗?或者我是否必须使用派生类重载的参数编写f()
的多个副本?

Brad




想法没问题。然而实现并不完全正确。


你的f函数可能会改变如下:


void f(Base& b)

{

if(typeid(b)== typeid(D1))/ *在这里做一件事* /


else if (typeid(b)== typeid(D2))/ *在这里做另一个* /

}


上面基本上使用的RTTI。


I would like to have a function that takes as an argument a base class but
performs differently depending on which type of derived class is passed.
Can I tell which derived class is passed?

For example:

class Base
{ //...
};

class D1: public Base
{ //...
};

class D2: public Base
{ //...
};

void f(Base b)
{
// Do one thing if b is of type D1, Do another if b is of type D2
}

Is this the right idea? Or do I have to write multiple copies of f()
with arguments overloaded with the derived classes?

Brad

解决方案


"Brad Marts" <ma***@phy.duke.edu> wrote in message
news:pa****************************@phy.duke.edu.. .

I would like to have a function that takes as an argument a base class but
performs differently depending on which type of derived class is passed.
Can I tell which derived class is passed?

For example:

class Base
{ //...
};

class D1: public Base
{ //...
};

class D2: public Base
{ //...
};

void f(Base b)
{
// Do one thing if b is of type D1, Do another if b is of type D2
}

Is this the right idea? Or do I have to write multiple copies of f()
with arguments overloaded with the derived classes?



I would certainly not write multiple copies. You might want to read up more
on examples of polymorphism. The power of this is that you don''t have to
write multiple copies of functions. On the other hand, you normally don''t
want to have to check what the subtype is, either. Normally what you want
to do is achieve "performing differently" by calling functions on those
objects that get passed in. In other words, you bury the different
performance within the subclasses themselves. For example, in your f()
function, what do you want to do? Let''s say it''s to display something about
the object. So you write a function called "display" and put it in Base and
make it virtual. Then in D1 and D2 you define those functions again,
separately, but you make them do 2 different things. For example,

void D1::display()
{
cout << "This is an object of type D1!";
}

void D2::display()
{
cout << "This is an object of type D2!";
}

In your f function, you merely write
b.display(), and then the correct thing will be displayed, depending on the
type, without you having to know or care in the code. There''s a catch here
though - you can''t do it if you pass the Base parameter by value like that.
You have to pass it by reference (Base& b), or pionter (Base* pB).


On Mon, 15 Dec 2003 10:34:48 -0700 in comp.lang.c++, "Brad Marts"
<ma***@phy.duke.edu> was alleged to have written:

I would like to have a function that takes as an argument a base class but
performs differently depending on which type of derived class is passed.



This issue is covered in Marshall Cline''s C++ FAQ. See the topic
"[20] Inheritance ? virtual functions". It is always good to check the
FAQ before posting. You can get the FAQ at:
http://www.parashift.com/cpp-faq-lite/


Brad Marts <ma***@phy.duke.edu> wrote in message
news:pa****************************@phy.duke.edu.. .

I would like to have a function that takes as an argument a base class but
performs differently depending on which type of derived class is passed.
Can I tell which derived class is passed?

For example:

class Base
{ //...
};

class D1: public Base
{ //...
};

class D2: public Base
{ //...
};

void f(Base b)
{
// Do one thing if b is of type D1, Do another if b is of type D2
}

Is this the right idea? Or do I have to write multiple copies of f()
with arguments overloaded with the derived classes?

Brad



The idea is all right. However the implementation is not quite right.

Your f function could be altered like this:

void f(Base& b)
{
if(typeid(b) == typeid(D1))/*Do one thing here*/

else if(typeid(b) == typeid(D2))/*Do another here*/
}

Basically used RTTI above.


这篇关于哪个派生类是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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