[英]C++ inheritance, calling the given classes function instead of its parent?
Really bad title, couldn't think of how to word it, sorry. 真的很糟糕的标题,想不出怎么说出来,对不起。
So say I had the following code: 所以说我有以下代码:
class A {
virtual int getSize() {
return 0;
}
}
class B : public A {
int getSize() {
return 32;
}
}
void doStuff(A a) {
std::cout << a.getSize() << std::endl;
}
int main() {
B b;
doStuff(b);
}
It would print out 0
, however I want it to print out 32
. 它将打印出
0
,但我希望它打印出32
。 In other words, I want to pass it the class and it prints out that classes function, so I could create a class C
, where the size is 64, and if I pass that C instance to the doStuff
function, I want it to print 64. 换句话说,我想将它传递给类,并打印出类函数,所以我可以创建一个类
C
,其中大小为64,如果我将该C实例传递给doStuff
函数,我希望它打印64。
Is there any way I can do this in C++, would I have to use templates or some fancy C++ feature I don't know about? 有什么方法可以用C ++做到这一点,我是否必须使用模板或一些我不知道的奇特的C ++特性?
A one-byte patch: 一个单字节补丁:
void doStuff(A &a) {
std::cout << a.getSize() << std::endl;
}
Your version takes the argument by value, which means that the function makes a copy of b
(a copy which is an A
) and then calls the copy's getSize()
. 您的版本按值获取参数,这意味着该函数会复制
b
(副本为A
),然后调用副本的getSize()
。 In this version, the function takes the argument by reference, and calls b
's own getSize()
, which is B::getSize()
. 在这个版本中,函数通过引用获取参数,并调用
b
自己的getSize()
,即B::getSize()
。
You should use pointers, or even better: smart pointers! 你应该使用指针,甚至更好:智能指针! That way, the function of the runtime type gets called.
这样,就会调用运行时类型的函数。 It's a basic example of polymorhpism.
这是多态性的一个基本例子。 If you want to avoid pointers, Beta's slicing approach is equally valid.
如果你想避免指针,Beta的切片方法同样有效。
#include <iostream>
#include <memory>
class A {
virtual int getSize() {
return 0;
}
}
class B : public A {
virtual int getSize() {
return 32;
}
}
void doStuff(std::shared_ptr<A> a) {
std::cout << a->getSize() << std::endl;
}
int main() {
std::shared_ptr<A> b(new B());
doStuff(b); // Will output '32'.
}
This should correctly call the function as implemented by B. 这应该正确调用B实现的功能。
Slicing the object is one approach, and in addition I think you're asking for, I think, a pretty straightforward use of polymorphism in C++. 切片对象是一种方法,此外我认为你要求我在C ++中非常简单地使用多态。 http://www.cplusplus.com/doc/tutorial/polymorphism/
http://www.cplusplus.com/doc/tutorial/polymorphism/
That's almost immediately applicable, just call your class A Shape, and B and C could be Square and Triangle. 这几乎是立即适用的,只需将您的A类称为A形,B和C可以是Square和Triangle。 Your DoStuff function could take a pointer to a Shape, then you can pass it a triangle or a square, and when you deference the Shape in the function, it will call the correct function.
你的DoStuff函数可以指向一个Shape,然后你可以传递一个三角形或一个正方形,当你在函数中使用Shape时,它会调用正确的函数。
So you'd have (also you need to make the members public, I think): 所以你有(你也需要公开成员,我认为):
class A {
public:
virtual int getSize() {
return 0;
}
};
class B : public A {
public:
int getSize() {
return 32;
}
};
void doStuff(A* a) {
std::cout << a->getSize() << std::endl;
}
int main() {
B b;
doStuff(&b);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.