zoukankan      html  css  js  c++  java
  • 一个纯虚函数导致的问题

    WIN7系统,VC2010下。

    程序A静态链接B.dll动态库。

    B.dll中导出3个类:

    (1) 基类

    class AFX_EXT_CLASS base

    {

    public:

      base(){};

      virtual ~base(){};

      virtual int getX() = 0;

    protected:

      int x;

      ......

    }

    (2)子类1

    class AFX_EXT_CLASS derived_1

    {

    public:

      derived_1(){};

      virtual ~derived_1(){};

      int getX() {return x;};

    protected:

      //int x; x是继承的

      ......

    }

    (2)子类2

    class AFX_EXT_CLASS derived_2

    {

    public:

      derived_2(){};

      virtual ~derived_2(){};

      int getX() {return x;};

    protected:

      //int x; x是继承的

      ......

    }

    函数getX()是为了防止父类实例化而设为纯虚函数。

    在程序A中引用该DLL,一切都是按规矩来的(头文件包含,引入lib文件)

    A中引用 dll中的类:

    class A 

    {  

      base* pbase;

      void OnCreateObject(int type)

      {

        if(type == 0)

          pbase = new derived_1();

        else

          pbase = new derived_2();

      }

      。。。。。。

    } 

    A程序将OnCreateObject与一个菜单命令连接起来,即此函数不是在系统初始化时调用。

    编译通过。但在运行中,出现了问题,系统在加载DLL过程中意外退出。

    通过思考,将base函数中的纯虚函数改变为:

    int getX() {return x;};

    而将两个子类中的同名函数删除,运行正常。

    但问题在哪里? 为什么 ?

    可能的原因:

       该DLL在另一个DLL中也被引用,是base的指针形式,引用是通过值传入,并没有new实例,但可能是因为这个才导致程序不能进入吧?

      在WIN10的VC2010中,程序编译就通不过,将纯虚函数改变为虚函数时,才能通过。

    各位高手,你们认为呢?

     

      

  • 相关阅读:
    POJ2965(The Pilots Brothers' refrigerator)
    POJ1753(Flip Game)
    POJ3253(Fence Repair)
    山东理工大学的训练计划
    loutsScript 常用代码
    《大道至简》读后感
    2019暑第三周
    2019暑第二周
    2019暑第一周
    关于13组作品《TD tree》的使用感想
  • 原文地址:https://www.cnblogs.com/myboat/p/9949009.html
Copyright © 2011-2022 走看看