[问]奇怪的dynamic_cast问题 [英] [Q] Strange dynamic_cast problem

查看:75
本文介绍了[问]奇怪的dynamic_cast问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的......这似乎正在进入一些非常深奥的c ++领域。

我会尽力解释这个问题,但我还不完全明白

我自己发生了什么。我希望问题归结为

标准c ++的东西,并不是特定于Mac OS X编译器和链接器。


我把一个简单的组合在一起测试项目可在以下网址找到:

http:// ericgorr。 net / LibraryLoading.zip


表示问题。


在Shared.h中,有两个纯粹的定义虚拟课程 -

A& B. B是A的子类。


在LibraryA中,有一个名为BImp的B类实现。

是一个名为GetA的函数,它返回一个指向

BImp实例的指针并返回一个A *。


In LibraryB,有一个叫test的函数。这个函数需要一个

void *,它最终会成为一个函数指针,指向LibraryA的GetA

函数。


测试函数出现问题:


B * myB = dynamic_cast< B *(myA);


dynamic_cast失败并且myB被指定为NULL。这个_should not

失败,因为myA是B类的一个实例。


但是,我可以使这个dynamic_cast成功,如果在main.cp中,这是

a应用程序目标的一部分,我设置:


#define CASE_A 1


允许


A * myA = functionPtr();

B * myB = dynamic_cast< B *(myA);


to在调用LibraryB的测试函数之前执行。


任何想法为什么它允许它工作?

任何想法为什么它首先失败?


在main.cp中,有两个#define'。


#define CASE_A 0

#define CASE_B 0

如果两者都设置为零,它将崩溃。这是最简单的崩溃

案例。

如果CASE_A为1,它将起作用。知道为什么吗?

如果CASE_B为1,它会崩溃,这种情况最接近我正在使用的真实

案例。

自从我开始撰写这条消息以来,我一直在研究

问题,并认为如果可以制作

destructors纯粹的虚函数,然后就可以解决

问题。


Google搜索纯虚拟析构函数c ++,我遇到了:

http:// www .linuxtopia.org / online_boo ... ter15_024.html


虽然看起来很奇怪,显然这在C ++中是允许的。


所以,如果我将Shared.h更改为:


*****

*****

#define VISIBLE __attribute __((visibility(" default")))


class VISIBLE A

{

public:

virtual~A(void)= 0;

virtual void Func(void)= 0;

};

A ::〜A(){}

class VISIBLE B:public A

{

public:

虚拟~B(无效)= 0;

虚拟空虚Func1(无效)= 0;

};

B ::〜B(){}

externC VISIBLE A * GetA(无效);

*****

*****


一切顺利三种情况。


对此解决方案有何评论?任何理由都不能完全有效吗?


任何想法或评论都会受到赞赏。

Ok...this seems to be treading into some really esoteric areas of c++.
I will do my best to explain this issue, but I don''t fully understand
what is happening myself. I am hoping that the problem comes down to
standard c++ stuff and is not specific to Mac OS X compiler&linker.

I have put together a simple test project which can be found at:

http://ericgorr.net/LibraryLoading.zip

which demonstrates the problem.

In Shared.h, there are the definitions of two purely virtual classes -
A & B. B is a subclass of A.

In LibraryA, there is a implementation of class B called BImp. There
is a function called GetA which returns a pointer to an instance of
BImp and returns an A*.

In LibraryB, there is a function called test. This function takes a
void *, which will end up being a function pointer to the GetA
function from LibraryA.

The problem comes in the test function with the line:

B* myB = dynamic_cast<B*(myA);

The dynamic_cast fails and myB is assigned NULL. This _should not_
fail because myA is an instance of class B.

However, I can make this dynamic_cast succeed, if in main.cp, which is
a part of the Application target, I set:

#define CASE_A 1

which allows

A *myA = functionPtr();
B *myB = dynamic_cast<B*(myA);

to be executed before the test function from LibraryB is called.

Any idea why this allows it to work?
Any idea why it is failing in the first place?

In main.cp, there are two #define''s.

#define CASE_A 0
#define CASE_B 0

If both are set to zero, it will crash. This is simplest crashing
case.
If CASE_A is 1, it will work. Any idea why?
If CASE_B is 1, it will crash and this is the case closest to the real
case I am working with.

Since I started composing this message, I''ve been looking into the
issue a bit more and thought that if it were possible to make the
destructors pure virtual functions as well, then that would solve the
problem.

Googling for "pure virtual destructors c++", I came across:

http://www.linuxtopia.org/online_boo...ter15_024.html

While it seems strange, apparently this is allowed in C++.

So, if I changed Shared.h to look like:

*****
*****
#define VISIBLE __attribute__ ((visibility("default")))

class VISIBLE A
{
public:
virtual ~A( void ) = 0;
virtual void Func( void ) = 0;
};
A::~A() {}
class VISIBLE B : public A
{
public:
virtual ~B( void ) = 0;
virtual void Func1( void ) = 0;
};
B::~B() {}
extern "C" VISIBLE A *GetA( void );
*****
*****

everything worked in all three cases.

Any comments on this solution? Any reason why this wouldn''t be
perfectly valid?

Any thoughts or comments would be appreciated.

推荐答案



你更有可能得到一个好的回复

如果你发布一个简短的,可编译的例子

表示问题。


让我们很容易为您提供帮助!


Sean


11月4日,2:18 * pm,sean_in_rale ... @ yahoo.com写道:
On Nov 4, 2:18*pm, sean_in_rale...@yahoo.com wrote:

你更有可能得到一个好的回复

如果你发布一个简短的,可编辑的例子

来证明这个问题。


让我们很容易为您提供帮助!
You''re more likely to get a good response
if you post a short, compilable example
that exhibits the problem.

Make it easy for us to help you!



这是:

http://ericgorr.net/LibraryLoading.zip


应该是。我意识到这是Mac特定的,这个

将限制能够提供帮助的人数,但是那里有
真的没有多少代码在那里所有如果你想看看。


在另一个论坛上,有人指出:


-----

所有C ++规范指定的是一组C ++

源模块中发生的事情,它们都链接在一起形成一个应用程序。无处

它讨论了如果你有DLL',共享库或

关于语言的其他事情会发生什么。

-----


确实有帮助。在''真实''的情况下,这在Visual Studio中只能工作



如果这是如何工作的基本上是未定义的,那么就不会有一个''正确''

解决方案 - 或''正确''解决方案将是特定于平台的。其中

的情况下,我将需要专门与Apple及其论坛打交道。

This is what:

http://ericgorr.net/LibraryLoading.zip

is supposed to be. I realize that it is Mac specific and that this
will limit the number of people who will be able to help, but there
really isn''t much code there at all if you wanted to take a look.

In a different forum, someone pointed out that:

-----
All the C++ specification specifies is what happens in a set of C++
source modules, all linked together to form one application. Nowhere
is it discussed what happens if you have DLL''s, shared libraries, or
anything else with respect to the language.
-----

which does actually help. In the ''real'' case, this is working just
fine within Visual Studio.

If how this works is basically undefined, there will not be a ''right''
solution - or the ''right'' solution will be platform specific. In which
case, I will need to deal exclusively with Apple and their forums.


11月4日,2:18 * pm,sean_in_rale ... @ yahoo.com写道:
On Nov 4, 2:18*pm, sean_in_rale...@yahoo.com wrote:

你更有可能得到一个好的回复

如果你发短信,可编辑的例子

表示问题。


让我们很容易为您提供帮助!
You''re more likely to get a good response
if you post a short, compilable example
that exhibits the problem.

Make it easy for us to help you!



这是:

http://ericgorr.net/LibraryLoading.zip


应该是。我意识到这是Mac特定的,这个

将限制能够提供帮助的人数,但是那里有
真的没有多少代码在那里所有如果你想看看。


在另一个论坛上,有人指出:


-----

所有C ++规范指定的是一组C ++

源模块中发生的事情,它们都链接在一起形成一个应用程序。无处

它讨论了如果你有DLL',共享库或

关于语言的其他事情会发生什么。

-----


确实有帮助。在''真实''的情况下,这在Visual Studio中只能工作



如果这是如何工作的基本上是未定义的,那么就不会有一个''正确''

解决方案 - 或''正确''解决方案将是特定于平台的。其中

的情况下,我将需要专门处理Apple及其论坛。

This is what:

http://ericgorr.net/LibraryLoading.zip

is supposed to be. I realize that it is Mac specific and that this
will limit the number of people who will be able to help, but there
really isn''t much code there at all if you wanted to take a look.

In a different forum, someone pointed out that:

-----
All the C++ specification specifies is what happens in a set of C++
source modules, all linked together to form one application. Nowhere
is it discussed what happens if you have DLL''s, shared libraries, or
anything else with respect to the language.
-----

which does actually help. In the ''real'' case, this is working just
fine within Visual Studio.

If how this works is basically undefined, there will not be a ''right''
solution - or the ''right'' solution will be platform specific. In which
case, I will need to deal exclusively with Apple and their forums.


这篇关于[问]奇怪的dynamic_cast问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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