C++ 怎么样? VIRTUAL函数不是多余的吗?
我现在正在学习 C++,但对于编程语言我并不是完全一无所知。有些事对我来说毫无意义。我的理解是类中的虚拟函数可以在子类中重写。但是,默认情况下不是允许的吗?例如:
class Color {
public:
void Declare() { std::cout <<"I am a generic color."; }
};
class Purple : public Color {
};
如果我创建一个 Purple 的实例,然后调用它的 Declare 函数,那么它显然会在控制台窗口输出“我是通用颜色”。如果我想在 Purple 类中重写这个函数,我可以简单地在那里定义它来生成:
class Purple : public Color {
public:
void Declare() { std::cout <<"I am purple."; }
};
当然,这会输出“我是紫色的”。到控制台。如果我可以默认覆盖函数,那么使用 VIRTUAL 函数来明确告诉编译器它可以被覆盖有什么意义呢?抱歉问了这个愚蠢的问题。 :/
Possible Duplicates:
Overriding vs Virtual
How am i overriding this C++ inherited member function without the virtual keyword being used?
I am learning C++ at this moment, but I'm not completely in the dark when it comes to programming languages. Something makes no sense to me. My understanding is that a VIRTUAL function in a class can be overridden in sub-classes. However, isn't that allowed by default? For example:
class Color {
public:
void Declare() { std::cout <<"I am a generic color."; }
};
class Purple : public Color {
};
If I create an instance of Purple and then call its Declare function, then it will obviously output the console window "I am a generic color." If I want to override this function in the Purple class, I can simply define it there to produce:
class Purple : public Color {
public:
void Declare() { std::cout <<"I am purple."; }
};
This, of course, outputs "I am purple." to the console. If I can override functions by default, then what is the point of having VIRTUAL functions to specifically tell the compiler it can be overridden? Sorry for the dumb question. :/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
考虑这段代码:
正如所写,这将打印出“I am a generic color”,即通过基类指针(
Color*
)调用Declare()将调用基类的函数实现。如果在 Declare() 的
Color
声明前面添加关键字virtual
,则会打印“I am Purple”。 virtual 关键字告诉编译器,通过基类指针对函数的调用应该分派到具体类 (Purple
) 实现。Consider this code:
As written, this will print out "I am a generic color", i.e. calling Declare() via the base class pointer (
Color*
) will invoke the base class's implementation of the function.If you add the keyword
virtual
in front ofColor
's declaration of Declare(), then "I am purple" will be printed. The virtual keyword tells the compiler that a call to the function via the base class pointer should be dispatched to the concrete class (Purple
) implementation.虚函数在处理多态性时很有用。非虚函数是在编译时查找的,因此创建
Color
类型的变量并调用其Declare()
方法将总是导致Color::Declare()
被调用,即使变量中的对象是Purple
。Virtual functions are useful when dealing with polymorphism. Non-virtual functions are looked up at compile time, so creating a variable of type
Color
and calling itsDeclare()
method will always result inColor::Declare()
being called, even if the object in the variable is aPurple
.区别在于,如果你有一个函数 foo ,它接受一个颜色并调用它的声明方法,如果它们是虚拟的,并且你传入一个紫色,它会说我是紫色,如果该方法不是虚拟的,它会说我是一个通用的颜色...
the difference is if you have a function foo which takes a colour and calls its declare method, if they are virtual, and you pass in a purple, it will say i am purple, if the method is not virtual, it will say i am a generic colour...
虚函数允许多态性——您可以有一个指向基类的指针,但它仍然调用派生类中的函数。在您的示例
中将输出“我是通用颜色”。如果
Color
中的Declare()
函数是virtual
,则Purple
中的函数将覆盖 它,因此这段代码将打印“我是紫色的”。Virtual functions allow polymorphism --- you can have a pointer to the base class and it still calls the function in the derived class. In your example
will output "I am a generic color". If the
Declare()
function inColor
wasvirtual
then the function inPurple
would override it, and thus this code would print "I am purple."重要的区别是在这种情况下发生的情况:
如果函数
Declare
被定义为非虚拟,那么调用将调用转到基类。如果该函数被声明为virtual
,则调用将转到派生类。因此,拥有一个函数将允许您使用基类指针调用派生类方法。基本上它支持多态性。The important difference is what happens in this case:
If the function
Declare
is defined as non-virtual then the call will call go to the base class. If the function is delcared asvirtual
then the call will go to the derived class. So having a function will allow you to call a derived class method using the base class pointer. That is basically it supports polymorphism.不,这并不多余。
尝试:
你希望它打印“我是紫色的”。只有当您将
Declare
声明为虚拟时,这种情况才会发生。选择允许覆盖派生类中的函数名称而不将其设为虚拟函数是 C++ 程序中错误的一个次要来源。不同的语言对此有不同的补充。
No, its not redundant.
Try:
You want it to print "I am purple." And that will only happen if you declare
Declare
as virtual.The choice of allowing to overwrite a function name in a derived class without it being virtual is a minor source of bugs in C++ programs. Different languages have different additudes on this.