虚拟功能说明 [英] Virtual functions clarifications

查看:115
本文介绍了虚拟功能说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有理解虚函数的问题???喜欢它的工作原理。


我的朋友问我一些问题


1)什么是静态功能?

我回复了在没有实例化该类对象的情况下使用的函数。


2)可以使用这个指针调用静态函数???

我告诉不!但我不知道为什么???这是因为我的基本原理尚不清楚。

需要一些关于为什么???的解释。如果可能的话,使用日常生活的例子就像Ganon11(我说他对纯虚函数的解释)。这是非常好的。


3)什么是虚函数???

我知道定义(来自书),但基础不明确。请解释一下日常生活中的例子。


4)虚拟功能可以静态化吗????


5)我可以打电话吗一个构造函数中的虚函数????


这些是我的一些问题,我需要将它们弄清楚。请帮助


提前致谢

Hi guys,
I have problem understanding virtual functions??? Like how it works.

My friend asked me some questions

1) What is a static function???
I replied functions to be used without intantiating an object of that class.

2) Can static function be called using this pointer???
I told no!!! but I dont know why??? This is because my funda is not clear.
Need some exaplantion about why???. If possible using day to day life example as like Ganon11 (where I say his explanation for pure virtual functions). It was really good one.

3) What is virtual function???
I know the defination (from book) but the funda is not clear. Please explain with day to day life example .

4) Can virtual functions be made static????

5) Can i call a virtual function from within a constructor????

These are some of the questions I have and I need to make them clear. Please help

Thanks in advance

推荐答案

对一些问题的简短回复(问题3很长,无法正确回答,你可以在网上和书上找到很好的解释。


1)你的答案是对的。并且你只能在静态函数中使用静态和局部变量。


2)是的,他们是班级成员。但你只需要试着找出来。


3)简短回答:它们是派生类可以重新定义的函数。然后在运行时,最新的是最新的。函数的版本(即派生类的版本)将被调用(除非您明确指定要调用的那个),无论函数调用的位置如何。

纯虚函数(在函数声明的末尾= 0)与虚函数相同,但您不能在基类中定义它们。你只需要声明它们。因此,您无法实例化基类的对象,因为存在未定义的函数。


但是还有更多详细信息可以找到,只需谷歌查看虚函数和纯虚函数。


4)不,我不知道为什么。但是再一次,试着找出来。


5)显然是的,这是一个像所有其他人一样的功能。


Sandro
A short reply to some questions (question 3 is quite long to be properly answered, and you can find good explanations both on the web and on books).

1) Your answer is right. And you can use only static and local variables within a static function.

2) Yes, they are class members. But you just have to try to find out.

3) Short answer: they are functions which a derived class can redefine. Then at runtime the "most new" version (i.e., those of the derived class) of the function will be called (unless you explicitly specify which one you are going to call), no matter from where the function is called.
Pure virtual functions (with =0 at the end of the function declaration) are the same as virtual functions, but you don''t define them in the base class. You just declare them. Therefore you cannot instantiate objects of the base class, since there are undefined functions.

But there are more details to find out, just google for virtual functions and pure virtual functions.

4) No, I don''t know exactly why. But again, just try and find out.

5) Obviously yes, it is a function like all the others.

Sandro



3)简答:它们是派生类可以重新定义的函数。然后在运行时,最新的是最新的。将调用函数的版本(即派生类的版本)(除非您明确指定要调用的那个),无论函数调用的位置如何。
3) Short answer: they are functions which a derived class can redefine. Then at runtime the "most new" version (i.e., those of the derived class) of the function will be called (unless you explicitly specify which one you are going to call), no matter from where the function is called.



实际上,它们是派生类必须定义的函数。任何派生类都可以覆盖任何基类的函数,但它可以选择这样做。将函数声明为虚拟需要子类来重新定义函数。

在运行时,最新版本不一定是被调用的版本。例如,假设您有两个类,Base和Derived。在Base中,您声明一个名为function()的虚函数。因此,Derived也必须具有function()。你回答的方式,总是会调用Derived :: function(),因为它是最新的。但是如果你有一个Base类型的对象并调用function(),则使用Base :: function()。当您在任何Base或Derived对象上调用function()时,计算机将确定该对象所在的类并使用相应的函数。

Actually, they are functions which a derived class must define. Any derived class can override any base class''s function, but it has the option of doing so. Declaring a function as virtual requires subclasses to redefine the function.

At runtime, the ''most new'' version is not necessarily the one that is called. For instance, suppose you have two classes, Base and Derived. In Base, you declare a virtual function called function(). Thus, Derived must have function() also. The way you answered, the Derived::function() would always be called, since it is the "most new". But if you have an object of type Base and you call function(), the Base::function() is used. When you call function() on any Base or Derived object, the computer determines which class the object is and uses the appropriate function.



例如,假设您有两个类,Base和Derived。在Base中,您声明一个名为function()的虚函数。因此,Derived必须有function()也
For instance, suppose you have two classes, Base and Derived. In Base, you declare a virtual function called function(). Thus, Derived must have function() also



你确定这个陈述是正确的。如果是的话

Are you sure this statement is correct. If yes

展开 | 选择 | Wrap | < span class =codeLinkonclick =LineNumbers(this);>行号


这篇关于虚拟功能说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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