C++ 怎么样? VIRTUAL函数不是多余的吗?

发布于 2024-09-27 21:13:10 字数 914 浏览 5 评论 0原文

可能的重复:
覆盖与虚拟
我如何覆盖这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

潜移默化 2024-10-04 21:13:10

考虑这段代码:

class Color
{
public:
    void Declare() { std::cout << "I am a generic color"; }
};

class Purple : public Color
{
public:
    void Declare() { std::cout << "I am purple"; }
}

Color* color = new Purple();
color->Declare();

正如所写,这将打印出“I am a generic color”,即通过基类指针(Color*)调用Declare()将调用基类的函数实现。

如果在 Declare() 的 Color 声明前面添加关键字 virtual,则会打印“I am Purple”。 virtual 关键字告诉编译器,通过基类指针对函数的调用应该分派到具体类 (Purple) 实现。

Consider this code:

class Color
{
public:
    void Declare() { std::cout << "I am a generic color"; }
};

class Purple : public Color
{
public:
    void Declare() { std::cout << "I am purple"; }
}

Color* color = new Purple();
color->Declare();

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 of Color'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.

孤檠 2024-10-04 21:13:10

虚函数在处理多态性时很有用。非虚函数是在编译时查找的,因此创建 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 its Declare() method will always result in Color::Declare() being called, even if the object in the variable is a Purple.

夏の忆 2024-10-04 21:13:10

区别在于,如果你有一个函数 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...

謌踐踏愛綪 2024-10-04 21:13:10

虚函数允许多态性——您可以有一个指向基类的指针,但它仍然调用派生类中的函数。在您的示例

Color *c=new Purple;
c->Declare();

中将输出“我是通用颜色”。如果 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

Color *c=new Purple;
c->Declare();

will output "I am a generic color". If the Declare() function in Color was virtual then the function in Purple would override it, and thus this code would print "I am purple."

优雅的叶子 2024-10-04 21:13:10

重要的区别是在这种情况下发生的情况:

Purple p;
Color* pC = &p;
pc->Declare();

如果函数 Declare 被定义为非虚拟,那么调用将调用转到基类。如果该函数被声明为virtual,则调用将转到派生类。因此,拥有一个函数将允许您使用基类指针调用派生类方法。基本上它支持多态性

The important difference is what happens in this case:

Purple p;
Color* pC = &p;
pc->Declare();

If the function Declare is defined as non-virtual then the call will call go to the base class. If the function is delcared as virtual 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.

活雷疯 2024-10-04 21:13:10

不,这并不多余。

尝试:

Color* color = new Purple();
color->Declare();

你希望它打印“我是紫色的”。只有当您将 Declare 声明为虚拟时,这种情况才会发生。

选择允许覆盖派生类中的函数名称而不将其设为虚拟函数是 C++ 程序中错误的一个次要来源。不同的语言对此有不同的补充。

No, its not redundant.

Try:

Color* color = new Purple();
color->Declare();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文