且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

访问基类中的虚拟主机

更新时间:2023-12-01 20:36:16

Jo写道:

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

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



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


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


Juha Nieminen写道:


> Jo写道:


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



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


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



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


编译器应该没问题。


Jo写道:

Juha Nieminen写道:

> Jo写道:

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


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

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



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


编译器应该没问题。



不是吗?怎么样:


A级

{

公开:

虚拟~A() {}

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

};


B级

{

公开:

virtual~B(){}

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

};


class C

:public A,public B

{

public:

virtual~C(){}

virtual void foo(){std :: cout<< &QUOT;Ç:: 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