使用虚拟功能 [英] Using virtual functions

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

问题描述

我遇到的问题是我有一个SDK,其中包含一些我想使用的类,其中包含要使用的纯虚函数 .我该怎么做呢?利用基类的功能似乎找不到任何东西.如果我知道从SDK使用的是什么代码,我将覆盖虚拟函数,但我不能这样做.

我制作了一个测试程序来尝试向您展示我真正想做的事

The problem I am having is I have a SDK with a few classes that I would like to use which contain pure virtual functions which I want to use. How do I go about doing this? Can''t seem to find anything on utilizing the functions from the base class. If I knew what code I was using from the SDK, I''d just override the virtual functions, but I can''t do that.

I made a test program to try and show you what I really want to do

class A {
  public:
  virtual int f() = 0 ;
};
class B : public A {
  public:
  B() ;
  int f() ;
};
//would like to use this
int A::f() {
     int x = 1 ;
     x = 1 ;
     return x ;
}
B::B() {}
int B::f() {
    int x = 1 ;
    x = 2 ;
    return x ;
}
int main() {
A* aPtr ;
B* bPtr = new B();
int retVal = 0 ;
retVal = bPtr->f() ; //uses B::f() which is not what I want to use
retVal = aPtr->f() ; //access violation (since f() = 0 right?)
return 0 ;
}

推荐答案

<br />
<pre lang="midl">retVal = bPtr->f() ; //uses B::f() which is not what I want to use<br />
retVal = aPtr->f() ; //access violation (since f() = 0 right?)</pre><br />



因为aPtr没有指向任何有效的对象,所以生成了访问冲突.

要点是,您不能创建包含纯虚函数的类的实例.

谢谢



Access violation is generated because aPtr is not pointing any valid object.

The main point is that you cant create an instance of a class that contain a pure virtual function.

Thanks


任何包含纯虚函数的类都是抽象类,无法实例化.它仅用作可以建立的基类.任何从抽象派生的类都必须实现纯虚函数,否则它也将成为抽象类.在class A中,函数f()不包含任何实现,因此任何派生类都必须使用适当的代码来实现该函数.如果要使用两个版本的f(),一个在A中,一个在B中,则不要使其在A中成为纯虚拟版本.
Any class that contains a pure virtual function is an abstract class and cannot be instantiated. It is only used as a base class that can be built upon. Any class derived from an abstract will have to implement the pure virtuals or it becomes an abstract class also. In your class A the function f() contains no implementation so any derived class must implement that function using whatever code is appropriate. If you want two versions of f(), one in A and one in B then don''t make it pure virtual in A.


问题是我不知道我什至不知道A :: f()的实现方式(在我的测试程序中,但是在SDK中,我不知道.).

由于A :: f()是纯虚拟的,没有办法使用它吗?我以为我无法以任何方式修改A类.



根据定义,不存在纯虚函数.那里没有代码.其空白.这样的类不能从不实例化.现有的编译器都不允许这样做.如果需要,您必须推出自己的产品.记住,像"A"这样的类仅是任何要充当"A"的东西的布局,还记得吗?

想象一下.高高的房间里有一堵墙,上面有一扇叫 exit 的门.另一边什么也没有.如果您尝试以任何方式使用它,那么您将成为一名摔跤手.您应该提供退出途径;无论是消防梯,楼梯,电梯,火箭-符合您目的的任何东西.

因此,不必担心不知道该功能是否实现以及如何实现.不是.什么都没有,零,密码,儿子,鸭,爱...
The problem is that I don''t know how A::f() is even implemented (in my test program I do, but in the SDK I don''t.).

Since A::f() is pure virtual, is there no way to use it? I''m assuming that I cannot modify class A in any way.



By definition, pure virtual functions don''t exist. Theres no code there. Its blank. And such a class can never be instantiated. None of the existing compilers allow that. You got to roll out your own if you need to. Such classes as ''A'' are supposed to be just a layout for anything that wants to act as ''A'', remember?

Imagine this. Theres a wall on a high raise room with a door called exit. Theres nothing on the other side. If you attempt to use it any way then you''ll be a splat. You are supposed to provide a means of exit; be it a fire ladder, a staircase, a lift, a rocket - anything that suits your purpose.

So, don''t worry about not knowing if and how that function is implemented ''cause its not. NOT. Theres nothing, null, cipher, sonne, duck, love...


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

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