纯虚函数和虚函数有什么不同 [英] What's the different betteen pure virtual function and virtualfunction

查看:86
本文介绍了纯虚函数和虚函数有什么不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我遇到了一个问题,


我没有弄清楚他们的不同之处,


例如:


#include< iostream>


class Base

{

public:

virtual~Base();

virtual void pure()= 0;

};


inline Base :: ~Base()

{}


inline void Base :: pure ()

{

std :: cout<< Base :: pure()调用\ nn;

}


类派生:public Base

{

public:

virtual void pure();

};


inline void Derived :: pure()

{

Base :: pure();

std :: cout<< Derived :: pure()叫做\ n;;

}


int main()

{

派生派生;


derived.pure();

derived.Base :: pure();


派生* dp =&派生;


dp-> pure();

dp-> Base :: pure ();

}

Hi,

I meet a question with it ,

I did not get clear the different betteen them,

for example:

#include <iostream>

class Base
{
public:
virtual ~Base();
virtual void pure() = 0;
};

inline Base::~Base()
{}

inline void Base::pure()
{
std::cout << "Base::pure() called\n";
}

class Derived: public Base
{
public:
virtual void pure();
};

inline void Derived::pure()
{
Base::pure();
std::cout << "Derived::pure() called\n";
}

int main()
{
Derived derived;

derived.pure();
derived.Base::pure();

Derived *dp = &derived;

dp->pure();
dp->Base::pure();
}

推荐答案

Jack写道:
Jack wrote:

我没有弄清楚他们的不同之处,
I did not get clear the different betteen them,



鉴于下面的代码中没有错误(我对<不熟悉) br />
虚拟地知道所有的电话),我可以提供我的输入:

Given there''s no errors in the code below (am not familiar enough with
virtual to know for sure on all the calls), I can provide my input:


例如:

[..]

virtual~Base();

virtual void pure()= 0;
for example:
[..]
virtual ~Base();
virtual void pure() = 0;


inline void Base :: pure()

{

std :: cout< ;< Base :: pure()名为\ n" ;;

}
inline void Base::pure()
{
std::cout << "Base::pure() called\n";
}



我认为pure()= 0赋值类定义没有效果

因为你后来定义了这个函数,实际上我甚至不确定它是否是编译器允许的

I think the pure() = 0 assignment in the class definition has no effect
since you later define the function, actually I''m not even sure if it is
allowed by the compiler.


class派生:public Base

{

public:

virtual void pure();

};
class Derived: public Base
{
public:
virtual void pure();
};



这定义了Derived的新函数,如果你为Derived类型的对象调用

pure(),它将被调用。

This defines the new function for Derived which gets called if you call
pure() for an object of type Derived.


inline void Derived :: pure()

{

Base :: pure();

std :: cout<< " Derived :: pure()call\\\
" ;;

}
inline void Derived::pure()
{
Base::pure();
std::cout << "Derived::pure() called\n";
}



这首先调用Base类pure()然后添加自己的代码。

This first calls the Base class pure() and then adds its own code.


derived.pure();
derived.pure();



这会调用Derived.pure()(先调用Base.pure()),

输出将是:

Base :: pure()调用

Derived :: pure()调用

This calls Derived.pure() (which in turn calls Base.pure() first),
output will be:
Base::pure() called
Derived::pure() called


derived.Base :: pure ();
derived.Base::pure();



这会调用Base.pure()来获取对象,输出

Base :: pure()调用

This calls Base.pure() for object derived, output
Base::pure() called


dp-> pure();

dp-> Base :: pure();
dp->pure();
dp->Base::pure();



这两个调用完全相同,只是指针作为变量,

而不是对象。


我不确定我理解你的问题吗?


Lars

These two calls do exactly the same, just with a pointer as variable,
instead of an object.

I''m not sure I understand your problem?

Lars


Jack< Ja * *********@gmail.com写道:
Jack <Ja**********@gmail.comwrote:

我遇到了一个问题,


我没有弄清楚他们的不同之处,
I meet a question with it ,

I did not get clear the different betteen them,



首先,纯虚拟:


class Base1 {

public:

virtual void pure()= 0;

};


class Derived1 {}; //将无法编译


class Derived2 {

public:

void pure(){cout<< " pure\\\
英寸; }

};


int main(){

Base1 b; //将无法编译

}


现在虚拟:


类Base1 {

public:

virtual void vert();

};


class Derived1 {}; //将编译


int main(){

Base1 b; //将编译

}


这就是区别。

First, pure virtual:

class Base1 {
public:
virtual void pure() = 0;
};

class Derived1 { }; // will not compile

class Derived2 {
public:
void pure() { cout << "pure\n"; }
};

int main() {
Base1 b; // will not compile
}

Now virtual:

class Base1 {
public:
virtual void vert();
};

class Derived1 { }; // will compile

int main() {
Base1 b; // will compile
}

That''s the difference.


嗨Daniel,


这样我就可以从中学到一些东西:


Daniel T.写道:
Hi Daniel,

Just so I get to learn something from this:

Daniel T. wrote:

首先,纯虚拟:


class Base1 {

public:

virtual void pure()= 0 ;

};
First, pure virtual:

class Base1 {
public:
virtual void pure() = 0;
};



所以_pure_指的是一个虚函数定义,= 0将允许

用于类定义而没有实体函数体声明?并且

那么你可能不会使用基类,而且

So _pure_ refers to a virtual function definition and the = 0 will allow
for class definition without an actualy function body declaration? And
then you may not use the base class, and also


class Derived1 {}; //将无法编译
class Derived1 { }; // will not compile



_must_定义你想要使用的派生类中的虚函数?


你可以声明一个纯虚函数,然后从中得到另一个

,然后得到第三个并且只在
中定义函数体第三个,如果那是你要使用的函数?

例如:

class Base {public:virtual void pure()= 0; };

类派生:public Base {};

class TwiceDerived:public Derived {void pure(){cout<< 两次

派生纯; }


然后使用

TwiceDerived td;

td.pure();




最诚挚的问候,

Lars

_must_ define the virtual function in the derived class that you want to
use?

And could you declare a pure virtual function, then derive another one
from that, then derive a third one and ONLY define the function body in
the third one, if that is the function you''re going to use?
e.g.:
class Base { public: virtual void pure() = 0; };
class Derived : public Base { };
class TwiceDerived : public Derived { void pure() { cout << "twice
derived pure"; }

and then use
TwiceDerived td;
td.pure();
?

Best Regards,

Lars


这篇关于纯虚函数和虚函数有什么不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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