这是正确的想法吗?或者我是否必须使用派生类重载的参数编写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 butperforms 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 messagenews: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 moreon examples of polymorphism. The power of this is that you don''t have towrite multiple copies of functions. On the other hand, you normally don''twant to have to check what the subtype is, either. Normally what you wantto do is achieve "performing differently" by calling functions on thoseobjects that get passed in. In other words, you bury the differentperformance within the subclasses themselves. For example, in your f()function, what do you want to do? Let''s say it''s to display something aboutthe object. So you write a function called "display" and put it in Base andmake 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 writeb.display(), and then the correct thing will be displayed, depending on thetype, without you having to know or care in the code. There''s a catch herethough - 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 butperforms 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 theFAQ before posting. You can get the FAQ at: http://www.parashift.com/cpp-faq-lite/Brad Marts <ma***@phy.duke.edu> wrote in messagenews: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? BradThe 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. 这篇关于哪个派生类是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 09:16
查看更多