虚拟重载函数和基类函数调用... [英] virtual overloaded functions and base class function call...

查看:60
本文介绍了虚拟重载函数和基类函数调用...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

首先抱歉我的英语不好,我是法国人;-)

我对其中一个项目中发生的事情有一个理解问题

我正在努力。


基本上我有一个gs_object课程而不是VIRTUAL函数

createList()。这个createList()函数在另一个类中重载了名为ct_server的
,它继承了gs_object。


在我的代码中,它看起来像这样:


class gs_object {

...

virtual void createList();

...

};


class ct_server:public gs_object {

...

virtual void createList();


void initInstance();

...

};


这是问题:


函数ct_server :: initInstance,我的团队中的一个男孩想要

调用gs_object :: createList()基函数,而不是过载的

one(ct_server :: createList())。但是,根据我的说法他写错了

,他写道:


(static_cast< GS_object *>(this)) - > createList() ;


而不是


gs_object :: createList();

据我所知,as createList()是虚拟的,这行代码应该是

调用ct_server :: createList而不是gs_object :: createList()


但它不是:当它在在调试模式下运行,我可以看到它调用

gs_object :: createList();


我不明白为什么。你能解释一下吗?

FYI,我用的是Visual C ++ 7.1.3; Qt 3.3.4

Hello,
First sorry for my poor English, I am French ;-)
I''ve got a comprehension problem of what happend in one of the project
i''m working on.

Basically I''ve got a class gs_object than has got a VIRTUAL function
createList(). This createList() function is overloaded in another class
named ct_server that inherits gs_object.

in my code, it looks something like that :

class gs_object {
...
virtual void createList();
...
};

class ct_server : public gs_object {
...
virtual void createList();

void initInstance();
...
};

Here is the problem :

in the function ct_server::initInstance, one boy of my team wanted to
call the gs_object::createList() base function, and not the overloaded
one (ct_server::createList() ). But, according to me he made a mistake
as he wrote :

(static_cast<GS_object*>(this))->createList();

instead of

gs_object::createList();
According to me, as createList() is virtual, this line of code should
call ct_server::createList and not gs_object::createList()

But it doesn''t : when in run in debug mode, i can see it calls
gs_object::createList();

I can''t understand why. Could you explain me ?
FYI, i''m using Visual C++ 7.1.3 ; Qt 3.3.4

推荐答案

2006年3月10日06:13:00 -0800, al **************** @ gmail.com 写道:
On 10 Mar 2006 06:13:00 -0800, al****************@gmail.com wrote:
你好,
首先抱歉我的英语不好,我是法国人;-)
我对其中一个项目中发生的事情有一个理解问题<我正在努力。

基本上我有一个类gs_object而不是一个VIRTUAL函数
createList()。这个createList()函数在另一个名为ct_server的类中重载,该类继承了gs_object。

在我的代码中,它看起来像这样:

class gs_object {
...
virtual void createList();
...
};

类ct_server:public gs_object {
...
virtual void createList();

void initInstance();
...
};

这是问题:<在函数ct_server :: initInstance中,我的团队中的一个男孩想要调用gs_object :: createList()基函数,而不是重载
一个(ct_server ::) createList())。但是,根据他的说法,他写了一个错误
,他写道:

(static_cast< GS_object *>(this)) - > createList();
而不是

gs_object :: createList();

据我说,因为createList()是虚拟的,这行代码应该调用ct_server :: createList而不是gs_object :: createList()

但它没有:当在调试模式下运行时,我可以看到它调用
gs_object :: createList();

我不明白为什么。你能解释一下吗?
我也在使用Visual C ++ 7.1.3; Qt 3.3.4
Hello,
First sorry for my poor English, I am French ;-)
I''ve got a comprehension problem of what happend in one of the project
i''m working on.

Basically I''ve got a class gs_object than has got a VIRTUAL function
createList(). This createList() function is overloaded in another class
named ct_server that inherits gs_object.

in my code, it looks something like that :

class gs_object {
...
virtual void createList();
...
};

class ct_server : public gs_object {
...
virtual void createList();

void initInstance();
...
};

Here is the problem :

in the function ct_server::initInstance, one boy of my team wanted to
call the gs_object::createList() base function, and not the overloaded
one (ct_server::createList() ). But, according to me he made a mistake
as he wrote :

(static_cast<GS_object*>(this))->createList();

instead of

gs_object::createList();
According to me, as createList() is virtual, this line of code should
call ct_server::createList and not gs_object::createList()

But it doesn''t : when in run in debug mode, i can see it calls
gs_object::createList();

I can''t understand why. Could you explain me ?
FYI, i''m using Visual C++ 7.1.3 ; Qt 3.3.4




ct_server :: initInstance()在哪里调用?如果在ct_server对象的

构造函数中调用它,则完全有可能调用

基类版本,因为该对象尚未完成

建设。但是,我不确定这是否可以保证按照C ++标准发生?b $ b;它可能是实现定义的。


-

Bob Hairgrove
没有********** @ Home.com


感谢您的回答,但我已经考虑过这个问题:没有

initInstance没有在类的构造函数中调用...: - /


还有其他想法吗?

PS:我做了一个基本的项目,在Visual Studio中只有2个类在

中再次检查它,现在我的结果完全没有相同:它

在我的''基本'项目中调用了ct_server :: createList()....


我变得疯狂了; - )

thanks for your answer, but I already thought to this issue : NO the
initInstance is NOT called in the constructor of the class... :-/

Any other idea ?
PS: i made a basic project with only 2 classes under Visual Studio in
order to check it again, and now I''ve not the same result at all : It
calls well ct_server::createList()... in my ''basic'' project.

I''m becoming crazy ;-)




Bob Hairgrove写道:

Bob Hairgrove wrote:
2006年3月10日06:13:00 -0800,< a href =mailto:al **************** @ gmail.com> al **************** @ gmail.com 写道:
On 10 Mar 2006 06:13:00 -0800, al****************@gmail.com wrote:
你好,
首先抱歉我的英语不好,我是法国人;-)
我是我对我正在进行的项目中发生的事情有一个理解问题。

基本上我有一个类gs_object而不是具有VIRTUAL函数 createList()。这个createList()函数在另一个名为ct_server的类中重载,该类继承了gs_object。

在我的代码中,它看起来像这样:

class gs_object {
...
virtual void createList();
...
};

类ct_server:public gs_object {
...
virtual void createList();

void initInstance();
...
};

这是问题:<在函数ct_server :: initInstance中,我的团队中的一个男孩想要调用gs_object :: createList()基函数,而不是重载
一个(ct_server ::) createList())。但是,根据他的说法,他写了一个错误
,他写道:

(static_cast< GS_object *>(this)) - > createList();
而不是

gs_object :: createList();

据我说,因为createList()是虚拟的,这行代码应该调用ct_server :: createList而不是gs_object :: createList()

但它没有:当在调试模式下运行时,我可以看到它调用
gs_object :: createList();

我不明白为什么。你能解释一下吗?
我也在使用Visual C ++ 7.1.3; Qt 3.3.4
Hello,
First sorry for my poor English, I am French ;-)
I''ve got a comprehension problem of what happend in one of the project
i''m working on.

Basically I''ve got a class gs_object than has got a VIRTUAL function
createList(). This createList() function is overloaded in another class
named ct_server that inherits gs_object.

in my code, it looks something like that :

class gs_object {
...
virtual void createList();
...
};

class ct_server : public gs_object {
...
virtual void createList();

void initInstance();
...
};

Here is the problem :

in the function ct_server::initInstance, one boy of my team wanted to
call the gs_object::createList() base function, and not the overloaded
one (ct_server::createList() ). But, according to me he made a mistake
as he wrote :

(static_cast<GS_object*>(this))->createList();

instead of

gs_object::createList();
According to me, as createList() is virtual, this line of code should
call ct_server::createList and not gs_object::createList()

But it doesn''t : when in run in debug mode, i can see it calls
gs_object::createList();

I can''t understand why. Could you explain me ?
FYI, i''m using Visual C++ 7.1.3 ; Qt 3.3.4



ct_server :: initInstance()在哪里调用?如果在ct_server对象的
构造函数中调用它,则完全有可能调用
基类版本,因为该对象尚未完成构造。但是,我不确定这是否可以保证由C ++标准发生;它可能是实现定义的。

-
Bob Hairgrove
没有********** @ Home.com




我想在构造函数中调用虚函数是未定义的

行为。那是因为那时vtbl还没有完全由

构建。



I guess calling a virtual function in the constructor is undefined
behaviour. That is because the vtbl is still not fully constructed by
that time.


这篇关于虚拟重载函数和基类函数调用...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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