私有虚函数和带有实体的纯虚函数 [英] private virtual functions and pure virtual functions with bodies

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

问题描述



您好,


Symbian OS第202页由Jo Stichbury解释说明


"全部虚拟功能,公共,受保护或私人,应该是

导出>


然后第203页状态


" ;在极少数情况下,纯虚函数体

有一个函数体,它必须被导出。


------- -----------------------


我假设如果派生类B实现了函数f
在基类A中声明私有和虚拟然后在

调用B :: f时将发生A :: f的调用尽管

事实那个f在A中是私有的。另外,是否有可能改变访问说明符(例如从私有到

受保护或在这种情况下公开?)。


另外,我从未见过带有

身体的纯虚拟功能。这是什么意思?我认为每当

函数是纯虚函数时,它必须以a = 0结尾,而

不包含任何实现。 (?)


反馈意见,


谢谢,


JG


Hello,

page 202 of Symbian OS Explained by Jo Stichbury states

"All virtual functions, public, protected or private, should be
exported"

then page 203 states

"In the rare cases where a pure virtual function body
has a function body, it must be exported."

------------------------------

I assume that if a derived class B implements a function f
declared private and virtual in a base class A then upon
invoking B::f an invocation of A::f will take place despite
the fact that f is private in A. Also, is it possible to
change the access specifier (e.g. from private to
protected or public in this case?).

Also, I have never seen a pure virtual function with a
body. What does this mean? I thought that whenever
a function is pure virtual it must end with a = 0 and
contain no implementation. (?)

Feedback appreciated,

Thanks,

JG

推荐答案



John Goche写道:

John Goche wrote:

您好,


Symbian OS第202页由Jo Stichbury解释


所有虚拟功能,公共,受保护或私有,应该是

导出"


然后第203页状态


"在极少数情况下,纯虚函数体

有一个函数体,必须导出。


-------------------- ----------


我假设如果派生类B实现了函数f

在基类中声明私有和虚拟然后在

调用B :: f时会发生A :: f的调用,尽管

事实上f是私有的A.
Hello,

page 202 of Symbian OS Explained by Jo Stichbury states

"All virtual functions, public, protected or private, should be
exported"

then page 203 states

"In the rare cases where a pure virtual function body
has a function body, it must be exported."

------------------------------

I assume that if a derived class B implements a function f
declared private and virtual in a base class A then upon
invoking B::f an invocation of A::f will take place despite
the fact that f is private in A.



不,它不会,如果A :: f是私有的,它是私有的包括

B衍生物。

即使A :: f是公共的,调用B :: f也不会调用A :: f。

No it won''t, if A::f is private, it''s private for all including for the
B derivative.
And even if A::f is public, calling B::f doesn''t invoke A::f.


此外,是否可以更改访问说明符(例如,
)从私人到

在这种情况下受保护还是公开?)。
Also, is it possible to
change the access specifier (e.g. from private to
protected or public in this case?).



什么?

What?


>

此外,我从未见过一个纯粹的虚拟函数,带有一个

的正文。这是什么意思?我认为每当

函数是纯虚函数时,它必须以a = 0结尾,而

不包含任何实现。 (?)
>
Also, I have never seen a pure virtual function with a
body. What does this mean? I thought that whenever
a function is pure virtual it must end with a = 0 and
contain no implementation. (?)



nope。这门课是抽象的。如果您选择,这并不妨碍您实现纯虚拟的
。一个很好的功能。

nope. The class is abstract. That does not prevent you from
implementing the pure-virtual if you choose to. A nice feature to have.


>

反馈意见,
>
Feedback appreciated,



与其他

语言不同,没有什么能阻止你调用纯虚函数。


#include< iostream>


类基地

{

公开:

虚拟~Base()= 0;

虚拟无效foo()const = 0;

};


Base :: ~Base(){std :: cout<< "〜基地()\\\
英寸; }

void Base :: foo()const {std :: cout<< "碱:: foo的()\\\
英寸; }


class派生:公共基地

{

公开:

派生(){}

~派生(){std :: cout<< "〜衍生()\\\
英寸; }

void foo()const

{

std :: cout<< " Derived :: foo()\ n";

Base :: foo();

}

};


int main()

{

派生的派生;

derived.foo();

}


/ *

派生:: foo()

Base :: foo()

~派生()

~Base()

* /

Nothing prevents you from calling a pure virtual function, unlike other
languages.

#include <iostream>

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

Base::~Base() { std::cout << "~Base()\n"; }
void Base::foo() const { std::cout << "Base::foo()\n"; }

class Derived : public Base
{
public:
Derived() { }
~Derived() { std::cout << "~Derived()\n"; }
void foo() const
{
std::cout << "Derived::foo()\n";
Base::foo();
}
};

int main()
{
Derived derived;
derived.foo();
}

/*
Derived::foo()
Base::foo()
~Derived()
~Base()
*/


12月4日下午12:57,John Goche < johngo ... @ gmail.comwrote:
On Dec 4, 12:57 pm, "John Goche" <johngo...@gmail.comwrote:

Hello,


Symbian OS第202页由Jo Stichbury解释状态


所有虚拟功能,公共,受保护或私有,应该是

导出的


然后第203页陈述


在极少数情况下,纯虚函数体

有一个函数体,必须将其导出。


------------------------------


我假设如果派生类B实现了一个函数f

在基类A中声明私有和虚拟,那么在

调用B :: f时调用A: :f将发生,尽管

事实上f是私人的A.
Hello,

page 202 of Symbian OS Explained by Jo Stichbury states

"All virtual functions, public, protected or private, should be
exported"

then page 203 states

"In the rare cases where a pure virtual function body
has a function body, it must be exported."

------------------------------

I assume that if a derived class B implements a function f
declared private and virtual in a base class A then upon
invoking B::f an invocation of A::f will take place despite
the fact that f is private in A.



不,私人功能无法从派生类。

No, a private function can not be accessed from the derived class.


此外,是否有可能改变访问说明符(例如从私有到


保护在这种情况下编辑还是公开?)。
Also, is it possible to
change the access specifier (e.g. from private to
protected or public in this case?).



是的,你可以声明一个函数public,如果它是私有的或受保护的

在基类中。

Yes, you can declare a function public if it was private or protected
in the base-class.


另外,我从未见过一个带有

体的纯虚函数。这是什么意思?我认为每当

函数是纯虚函数时,它必须以a = 0结尾,而

不包含任何实现。 (?)
Also, I have never seen a pure virtual function with a
body. What does this mean? I thought that whenever
a function is pure virtual it must end with a = 0 and
contain no implementation. (?)



考虑一下:


class Base {

virtual int foo() = 0;

public:

virtual int bar();

}


int Base :: foo(){return 1; }


int Base :: bar()return foo(); }


class派生:public Base {

virtual int foo();

public:

virtual int bar();

}


int Derived :: bar(){return A :: bar(); }


int Derived :: foo(){return 0; }


int main(){

B b;

std :: cout<< b.bar(); //将打印1

}


这允许您使用带有正文的私有纯函数,但是

请注意你还必须在Derived中提供一个foo()函数,因为它在Base中是纯粹的虚函数。


私有纯函数的使用身体可能不是那么好。

公共纯粹的身体功能在提供功能所需功能的部分非常有用,并且仍需要任何

派生类提供自己的实现,即使它只是

a Base :: foo() - 调用。


-

Erik Wikstr?m

Consider this:

class Base {
virtual int foo() = 0;
public:
virtual int bar();
}

int Base::foo() { return 1; }

int Base::bar() return foo(); }

class Derived : public Base {
virtual int foo();
public:
virtual int bar();
}

int Derived::bar() { return A::bar(); }

int Derived::foo() { return 0; }

int main() {
B b;
std::cout << b.bar(); // will print 1
}

This allows you to use a private pure function with a body, however
note that you must also provide a foo()-function in Derived, since it''s
a pure virtual function in Base.

The use of private pure functions with bodies are perhaps not so great.
Public pure functions with bodies can be very useful in providing part
of the functionality wanted from the function, and still requireing any
derived classes to provide their own implementation, even if it''s just
a Base::foo()-call.

--
Erik Wikstr?m




谢谢Erik和Peter的澄清,


这个例子真正展示了如何在派生类中实现纯虚函数

,并防止

基类来自直接实例化但是实现了b $ b基本类中的纯虚函数

可能仍然存在并且可以从同一基类中的某些其他函数访问



或使用来自派生类的访问说明符

例如。


但我不清楚的是,如果是私有的

函数无法从派生类调用,

那么制作这样一个什么点b / b $ b函数虚拟???


谢谢,


JG

er **** @ student.chalmers.se 写道:

12月4日下午12:57,John Goche < johngo ... @ gmail.comwrote:
On Dec 4, 12:57 pm, "John Goche" <johngo...@gmail.comwrote:

Hello,


Symbian OS第202页由Jo Stichbury解释状态


所有虚拟功能,公共,受保护或私有,应该是

导出的


然后第203页陈述


在极少数情况下,纯虚函数体

有一个函数体,必须将其导出。


------------------------------


我假设如果派生类B实现了一个函数f

在基类A中声明私有和虚拟,那么在

调用B :: f时调用A: :f将发生,尽管

事实上f是私人的A.
Hello,

page 202 of Symbian OS Explained by Jo Stichbury states

"All virtual functions, public, protected or private, should be
exported"

then page 203 states

"In the rare cases where a pure virtual function body
has a function body, it must be exported."

------------------------------

I assume that if a derived class B implements a function f
declared private and virtual in a base class A then upon
invoking B::f an invocation of A::f will take place despite
the fact that f is private in A.



不,私人功能无法从派生类。


No, a private function can not be accessed from the derived class.


此外,是否有可能改变访问说明符(例如来自私有)
在这种情况下,

受保护还是公开?)。
Also, is it possible to
change the access specifier (e.g. from private to
protected or public in this case?).



是的,你可以声明一个函数public,如果它是私有的或受保护的

在基类中。


Yes, you can declare a function public if it was private or protected
in the base-class.


另外,我从未见过一个带有

体的纯虚函数。这是什么意思?我认为每当

函数是纯虚函数时,它必须以a = 0结尾,而

不包含任何实现。 (?)
Also, I have never seen a pure virtual function with a
body. What does this mean? I thought that whenever
a function is pure virtual it must end with a = 0 and
contain no implementation. (?)



考虑一下:


class Base {

virtual int foo() = 0;

public:

virtual int bar();

}


int Base :: foo(){return 1; }


int Base :: bar()return foo(); }


class派生:public Base {

virtual int foo();

public:

virtual int bar();

}


int Derived :: bar(){return A :: bar(); }


int Derived :: foo(){return 0; }


int main(){

B b;

std :: cout<< b.bar(); //将打印1

}


这允许您使用带有正文的私有纯函数,但是

请注意你还必须在Derived中提供一个foo()函数,因为它在Base中是纯粹的虚函数。


私有纯函数的使用身体可能不是那么好。

公共纯粹的身体功能在提供功能所需功能的部分非常有用,并且仍需要任何

派生类提供自己的实现,即使它只是

a Base :: foo() - 调用。


-

Erik Wikstr?m


Consider this:

class Base {
virtual int foo() = 0;
public:
virtual int bar();
}

int Base::foo() { return 1; }

int Base::bar() return foo(); }

class Derived : public Base {
virtual int foo();
public:
virtual int bar();
}

int Derived::bar() { return A::bar(); }

int Derived::foo() { return 0; }

int main() {
B b;
std::cout << b.bar(); // will print 1
}

This allows you to use a private pure function with a body, however
note that you must also provide a foo()-function in Derived, since it''s
a pure virtual function in Base.

The use of private pure functions with bodies are perhaps not so great.
Public pure functions with bodies can be very useful in providing part
of the functionality wanted from the function, and still requireing any
derived classes to provide their own implementation, even if it''s just
a Base::foo()-call.

--
Erik Wikstr?m


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

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