访问基类中的虚拟主机 [英] Accessing virtuals in base class

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

问题描述




有没有一种通用的方式来访问基类

类中的(虚拟)函数?


喜欢:


if(!this-> Foo(something))return(((Base *)this) - > Foo(something) ); // for

普通会员功能

if(!this-> Foo(something))return(this-> Base :: Foo(something)); // for

a虚拟会员功能


我想以通用的方式做到这一点,因此无需支付

这一行在每个班级中都指定了正确的基类


干杯,


Jo

解决方案

Jo写道:


我想以通用的方式做到这一点,因此无需放入

这一行在每个类中指定正确的基类



编译器如何知道*哪个*版本的函数你是什么?b如果你没有指定,$ b想打电话吗?


只需致电:Base :: Foo(某事);


< blockquote> Juha Nieminen写道:


> Jo写道:


>> ;我想以通用方式执行此操作,因此无需在每个指定t的类中添加此行他是一个合适的基类



编译器怎么知道*如果你不打算*你要打电话给哪个*版本的功能?指定它?


只需调用:Base :: Foo(某事物);



我的意思是直接基类一上来在继承树中。


编译器应该没问题。


Jo写道:


Juha Nieminen写道:


> Jo写道:


>>我想以通用方式执行此操作,因此无需在每个类中指定正确的基类


如果您没有指定,编译器如何知道*您要调用哪个*版本的函数?

只需调用:Base :: Foo(某物);



我的意思是直接基类一上在继承树中。


编译器应该没问题。



不是吗?怎么样:


A级

{

公开:

虚拟~A() {}

virtual void foo(){std :: cout<< " A :: foo\\\
英寸; }

};


B级

{

公开:

virtual~B(){}

virtual void foo(){std :: cout<< " B :: foo\\\
英寸; }

};


class C

:public A,public B

{

public:

virtual~C(){}

virtual void foo(){std :: cout<< "Ç:: foo\\\
英寸; }

};


什么函数应该调用C,因为它继承自定义相同虚函数的两个不同的
类?另一方面,我不知道你在指定基类时有什么问题,

需要的时候。


问候,


Zeppe


Hi,

Is there a generic way to access the (virtual) functions in the base
class of a class?

Like in:

if (!this->Foo(something)) return(((Base*)this)->Foo(something)); // for
a normal member function
if (!this->Foo(something)) return(this->Base::Foo(something)); // for
a virtual member function

I would like to do this in a generic way, thus without having to put
this line in every class specifying the proper base class

Cheers,

Jo

解决方案

Jo wrote:

I would like to do this in a generic way, thus without having to put
this line in every class specifying the proper base class

How could the compiler know *which* version of the function you
want to call if you don''t specify it?

Just call: Base::Foo(something);


Juha Nieminen wrote:

>Jo wrote:

>>I would like to do this in a generic way, thus without having to put
this line in every class specifying the proper base class


How could the compiler know *which* version of the function you
want to call if you don''t specify it?

Just call: Base::Foo(something);

I mean the immediate base class "one up" in the inheritance tree.

That should be no problem for the compiler.


Jo wrote:

Juha Nieminen wrote:

>Jo wrote:

>>I would like to do this in a generic way, thus without having to put
this line in every class specifying the proper base class


How could the compiler know *which* version of the function you
want to call if you don''t specify it?

Just call: Base::Foo(something);


I mean the immediate base class "one up" in the inheritance tree.

That should be no problem for the compiler.

no? what about this:

class A
{
public:
virtual ~A() {}
virtual void foo() { std::cout << "A::foo\n"; }
};

class B
{
public:
virtual ~B() {}
virtual void foo() { std::cout << "B::foo\n"; }
};

class C
: public A, public B
{
public:
virtual ~C() {}
virtual void foo() { std::cout << "C::foo\n"; }
};

what function should call C, given that inherits from two different
classes that define the same virtual function? On the other side, I
don''t see what kind of problem do you have in specifying the base class,
when it''s needed.

Regards,

Zeppe


这篇关于访问基类中的虚拟主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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