虚拟方法的有趣问题 [英] Funny problem with virtual method

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

问题描述




我在虚拟方法中遇到了一个奇怪的问题。原始方法代码

在运行时引发访问冲突。问题的解决方案

是在虚方法中声明一个虚拟整数。然后不再发生

访问冲突!


以下类(从NetCDF C ++界面压缩):


class NcVar:public NcTypedComponent

{

public:

//此方法在NcTypedComponent中实现抽象方法

虚拟NcType类型()const;

私有:

int dim_to_index(NcDim * rdim);

int the_id;

};


类型()的原始实现:


NcType NcVar :: type()const

{

// nc_inq_vartype是一个正常工作的库函数

//在此方法之外

nc_type typ;

nc_inq_vartype(the_file-> id(),the_id,& typ);

return(NcType)typ;

}


运行此代码会引发访问冲突。在方法之外调用完全相同的

代码可以很好地工作。


类型()的修改实现:


NcType NcVar :: type()const

{

int x; //这是正确的,添加它使它工作


nc_type typ = 0;

nc_inq_vartype(the_file-> id(),the_id, & typ);

return(NcType)typ;

}


添加int x使代码运行,没有任何访问冲突!

其他类中的一些其他虚拟方法也有类似的问题。


这里发生了什么?


仅供参考,此代码来自NetCDF C ++界面。我正在尝试使用它与Borland C ++ Builder 6一起使用
。我没有编写NetCDF C ++界面,

但是我试图按原样使用它,并且访问违规问题已经

让我疯了。


任何帮助都将不胜感激。


问候,

Markus。

Hi,

I have a weird problem in a virtual method. The original method code
raises an access violation when it is run. The solution to the problem
is to declare a dummy integer inside the virtual method. Then the
access violation no longer occurs!

The following class (condensed from the NetCDF C++ interface):

class NcVar : public NcTypedComponent
{
public:
// This method implements an abstract method in NcTypedComponent
virtual NcType type() const;
private:
int dim_to_index(NcDim* rdim);
int the_id;
};

Original implementation of type():

NcType NcVar::type() const
{
// nc_inq_vartype is a library function that works normally
// outside of this method
nc_type typ;
nc_inq_vartype(the_file->id(), the_id, &typ);
return (NcType)typ;
}

Running this code raises an access violation. Calling the exact same
code outside of the method works perfectly.

Modified implementation of type():

NcType NcVar::type() const
{
int x; // That''s right, adding this makes it work

nc_type typ = 0;
nc_inq_vartype(the_file->id(), the_id, &typ);
return (NcType)typ;
}

Adding the "int x" makes the code run, without any access violations!
A few other of the virtual methods in other classes have a similar
problem.

What''s going on here?

FYI, this code is from the NetCDF C++ interface. I am trying to use it
with Borland C++ Builder 6. I did not write the NetCDF C++ interface,
but I am trying to use it as-is, and the access violation problem has
been driving me nuts.

Any help would be appreciated.

Regards,
Markus.

推荐答案



Markus Svilans napsal:

Markus Svilans napsal:




虚拟方法中有一个奇怪的问题。原始方法代码

在运行时引发访问冲突。问题的解决方案

是在虚方法中声明一个虚拟整数。然后不再发生

访问冲突!


以下类(从NetCDF C ++界面压缩):


class NcVar:public NcTypedComponent

{

public:

//此方法在NcTypedComponent中实现抽象方法

虚拟NcType类型()const;

私有:

int dim_to_index(NcDim * rdim);

int the_id;

};


类型()的原始实现:


NcType NcVar :: type()const

{

// nc_inq_vartype是一个正常工作的库函数

//在此方法之外

nc_type typ;

nc_inq_vartype(the_file-> id(),the_id,& typ);

return(NcType)typ;

}


运行此代码会引发访问冲突。在方法之外调用完全相同的

代码可以很好地工作。


类型()的修改实现:


NcType NcVar :: type()const

{

int x; //这是正确的,添加它使它工作


nc_type typ = 0;

nc_inq_vartype(the_file-> id(),the_id, & typ);

return(NcType)typ;

}


添加int x使代码运行,没有任何访问冲突!

其他类中的一些其他虚拟方法也有类似的问题。


这里发生了什么?


仅供参考,此代码来自NetCDF C ++界面。我正在尝试使用它与Borland C ++ Builder 6一起使用
。我没有编写NetCDF C ++界面,

但是我试图按原样使用它,并且访问违规问题已经

让我疯了。


任何帮助都将不胜感激。


问候,

马库斯。
Hi,

I have a weird problem in a virtual method. The original method code
raises an access violation when it is run. The solution to the problem
is to declare a dummy integer inside the virtual method. Then the
access violation no longer occurs!

The following class (condensed from the NetCDF C++ interface):

class NcVar : public NcTypedComponent
{
public:
// This method implements an abstract method in NcTypedComponent
virtual NcType type() const;
private:
int dim_to_index(NcDim* rdim);
int the_id;
};

Original implementation of type():

NcType NcVar::type() const
{
// nc_inq_vartype is a library function that works normally
// outside of this method
nc_type typ;
nc_inq_vartype(the_file->id(), the_id, &typ);
return (NcType)typ;
}

Running this code raises an access violation. Calling the exact same
code outside of the method works perfectly.

Modified implementation of type():

NcType NcVar::type() const
{
int x; // That''s right, adding this makes it work

nc_type typ = 0;
nc_inq_vartype(the_file->id(), the_id, &typ);
return (NcType)typ;
}

Adding the "int x" makes the code run, without any access violations!
A few other of the virtual methods in other classes have a similar
problem.

What''s going on here?

FYI, this code is from the NetCDF C++ interface. I am trying to use it
with Borland C++ Builder 6. I did not write the NetCDF C++ interface,
but I am trying to use it as-is, and the access violation problem has
been driving me nuts.

Any help would be appreciated.

Regards,
Markus.



我认为你在某处覆盖记忆。

I think you are somewhere overwriting memory.


" Markus Svilans" < ms ****** @ gmail.com写信息

新闻:11 ********************** @ l39g2000cwd .googlegr oups.com ...
"Markus Svilans" <ms******@gmail.comwrote in message
news:11**********************@l39g2000cwd.googlegr oups.com...




我在虚拟方法中有一个奇怪的问题。原始方法代码

在运行时引发访问冲突。问题的解决方案

是在虚方法中声明一个虚拟整数。然后不再发生

访问冲突!


以下类(从NetCDF C ++界面压缩):


class NcVar:public NcTypedComponent

{

public:

//此方法在NcTypedComponent中实现抽象方法

虚拟NcType类型()const;

私有:

int dim_to_index(NcDim * rdim);

int the_id;

};


类型()的原始实现:


NcType NcVar :: type()const

{

// nc_inq_vartype是一个正常工作的库函数

//在此方法之外

nc_type typ;

nc_inq_vartype(the_file-> id(),the_id,& typ);

return(NcType)typ;

}


运行此代码会引发访问冲突。在方法之外调用完全相同的

代码可以很好地工作。


类型()的修改实现:


NcType NcVar :: type()const

{

int x; //这是正确的,添加它使它工作


nc_type typ = 0;

nc_inq_vartype(the_file-> id(),the_id, & typ);

return(NcType)typ;

}


添加int x使代码运行,没有任何访问冲突!

其他类中的一些其他虚拟方法也有类似的问题。


这里发生了什么?


仅供参考,此代码来自NetCDF C ++界面。我正在尝试使用它与Borland C ++ Builder 6一起使用
。我没有编写NetCDF C ++界面,

但是我试图按原样使用它,并且访问违规问题已经

让我疯了。


任何帮助都将不胜感激。


问候,

马库斯。
Hi,

I have a weird problem in a virtual method. The original method code
raises an access violation when it is run. The solution to the problem
is to declare a dummy integer inside the virtual method. Then the
access violation no longer occurs!

The following class (condensed from the NetCDF C++ interface):

class NcVar : public NcTypedComponent
{
public:
// This method implements an abstract method in NcTypedComponent
virtual NcType type() const;
private:
int dim_to_index(NcDim* rdim);
int the_id;
};

Original implementation of type():

NcType NcVar::type() const
{
// nc_inq_vartype is a library function that works normally
// outside of this method
nc_type typ;
nc_inq_vartype(the_file->id(), the_id, &typ);
return (NcType)typ;
}

Running this code raises an access violation. Calling the exact same
code outside of the method works perfectly.

Modified implementation of type():

NcType NcVar::type() const
{
int x; // That''s right, adding this makes it work

nc_type typ = 0;
nc_inq_vartype(the_file->id(), the_id, &typ);
return (NcType)typ;
}

Adding the "int x" makes the code run, without any access violations!
A few other of the virtual methods in other classes have a similar
problem.

What''s going on here?

FYI, this code is from the NetCDF C++ interface. I am trying to use it
with Borland C++ Builder 6. I did not write the NetCDF C++ interface,
but I am trying to use it as-is, and the access violation problem has
been driving me nuts.

Any help would be appreciated.

Regards,
Markus.



这种类型的错误,你得到一个访问violatoin,但你做了一些事情

完全不相关它消失了,意味着你有一个缓冲区溢出

某处。添加新变量可以将变量稍微改变一下,

它只是意味着你要覆盖一个不会立即导致访问冲突的变量,但是你的问题仍然存在。


显示导致问题的完整可编译程序。我敢打赌

你甚至不会把它发送到这个新闻组,但是,这样做的行为

会告诉你问题所在。如果没有,请发送代码。

This type of error, you get an access violatoin, but you do something
totally unrelated and it goes away, means you have a buffer overflow
somewhere. Adding that new variable shuffled the variables around a bit,
and it just means you''re overwriting a variable that isn''t immediately
causing an access violation, but your problem is still there.

Show a complete compilable program that causes the problem. I''ll bet that
you won''t even send it to this newsgroup, however, as the act of doing this
will show you where the problem is. If not, send the code.




Markus Svilans在留言中写道

< 11 **** ******************@l39g2000cwd.googlegroups .com> ...

Markus Svilans wrote in message
<11**********************@l39g2000cwd.googlegroups .com>...

>在运行时引发访问冲突。解决问题的方法是在虚方法中声明一个虚拟整数。然后不再发生
访问冲突!
以下类(从NetCDF C ++界面浓缩):

类NcVar:public NcTypedComponent {public:

//此方法在NcTypedComponent中实现抽象方法

虚拟NcType类型()const;

private:

int dim_to_index(NcDim * rdim);

int the_id;

};

类型()的原始实现:

NcType NcVar: :type()const {

// nc_inq_vartype是一个正常工作的库函数

//在此方法之外

nc_type typ;
>Hi,
I have a weird problem in a virtual method. The original method code
raises an access violation when it is run. The solution to the problem
is to declare a dummy integer inside the virtual method. Then the
access violation no longer occurs!
The following class (condensed from the NetCDF C++ interface):

class NcVar : public NcTypedComponent { public:
// This method implements an abstract method in NcTypedComponent
virtual NcType type() const;
private:
int dim_to_index( NcDim* rdim );
int the_id;
};

Original implementation of type():

NcType NcVar::type() const {
// nc_inq_vartype is a library function that works normally
// outside of this method
nc_type typ;



什么是''nc_type''?

What is ''nc_type''?


nc_inq_vartype(the_file-> id(), the_id,& typ);
nc_inq_vartype( the_file->id(), the_id, &typ );



什么是''the_file''指向?

//返回(NcType)典型值;


什么是''NcType''?


选择一个(全部尝试)。

返回NcType(典型值);

返回static_cast< NcType>(典型值);

返回dynamic_cast< NcType>(典型值);

返回reinterpret_cast< NcType>(典型值);


删除强制转换,看看你的编译器正在尝试做什么!

C演员可能会掩盖错误。

返回typ;

What is ''the_file'' pointing to?
// return (NcType)typ;

What is ''NcType''?

Pick one (try all).
return NcType( typ );
return static_cast<NcType>( typ );
return dynamic_cast<NcType>( typ );
return reinterpret_cast<NcType>( typ );

Remove the cast and see what your compiler is trying to do!
The ''C'' cast may be covering up an error.

return typ;


>}
运行此代码会引发访问冲突。在方法之外调用完全相同的代码可以很好地工作。
>}

Running this code raises an access violation. Calling the exact same
code outside of the method works perfectly.



显示有效的电话。


-

Bob R

POVrookie

Show the call that works.

--
Bob R
POVrookie


这篇关于虚拟方法的有趣问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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