C ++:可以从派生类构造函数中调用ABSRACT CLASS构造函数吗? [英] C++: Can ABSRACT CLASS constructor be called from its derived class constructor?

查看:84
本文介绍了C ++:可以从派生类构造函数中调用ABSRACT CLASS构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class  ISample  //  界面 
{
public

ISample()
{
cout<< ISample default constructor !!!;
}

~ISample()
{
cout<< ISample default destructor !!!;
}
虚拟 void add ()= 0 ;
virtual void diff()= 0 < /跨度>;
};

class 基数:ISample // 仍然是抽象类
{
int n;

public

Base()
{
cout<< 基本默认构造函数!!!;
n = 0 ;
}

基数( int val):ISample() // 调用接口构造函数
{
cout<< 基础参数构造函数!!!;
}

虚拟 void 添加()
{

}
虚拟 void diff()= 0 ;
};

class Der:Base // 派生类
{
int a;

public
Der()
{
cout<< 默认构造函数!!!;
a = 0 ;
}

Der( int val):Base(val) /// /调用抽象类构造函数
{
cout<< Der param构造函数!!!;
}

void add ()
{
cout<< 派生添加功能!!!;
}

虚拟 void diff()
{
cout<< 派生差异函数!!!;
}
};

int main()
{
Der obj2( 10 < /跨度>); // 创建派生类的实例
}

注意:在上面的场景中, interface 和abstarct class 的构造 class 构造函数中
调用。这是否意味着创建Interface / abstract class 的实例? 其中不可能!但这是编译。

解决方案

一个 C ++ 抽象类不是,默认为接口,即它也可能包含功能 implementation

As 文档 [ ^ ]声明:





包含至少一个纯虚函数的类被视为抽象类。







它也可能包含方法实现(它也显示在链接文档示例代码中)。


构造函数从调用开始构造函数为其基础,然后为其成员构造,然后执行其代码(如果有的话)。



是否y ou class有虚拟成员函数,纯粹或其他,与此无关。但有一点需要注意的是,在构造或销毁过程中调用派生类的虚函数是不可能的。



在C ++中我们认为那些对象是不存在,因为他们的构造函数没有被调用,或者,在析构函数的情况下,它们不再存在 - 即使它们占用的内存仍然被分配。



最好的问候

Espen Harlinn


//这是不可能的!

:)



//但这是编译

:)



//这不是说创建一个实例接口/抽象类??

实例将隐式创建,作为 Der -object的一部分。

因此 ISample / Base 的任何成员都将计入 sizeof(Der)



但是,是的,你是对的,

创建无关紧要的东西是不可能的(没有 diff())一世nstance直接:)



PS:关于接口的东西:

抽象接口也将用于掩盖对象:

  //   client.h  
void test(ISample * pISample)
{
// 请注意:
// 用户功能无法看到界面背后的真实对象
if (pISample){
pISample-> HowAreYou();
}
}

// Sample.h
class ISample
{
public
virtual void HowAreYou()= 0 ;
};

// MaskedObject.h
#include Sample.h
class MaskedObj: public ISample
{
// 这里是自己屏蔽信息的兆字节......
// ...

// 测试
void SomeFunction();

// 示例功能:
public
virtual void HowAreYou(){MyMessageBox ( 好的,谢谢!); }
};

// MaskedObj.cpp
#include client.h
void MaskedObj :: SomeFunction()
{
:: test(); // 测试函数只知道我是一个接口而不是MaskedObj
}


class ISample //Interface
{
public:

    ISample()
    {
        cout<<"ISample default constructor!!!";
    }

    ~ISample()
    {
        cout<<"ISample default destructor!!!";
    }
    virtual void add() = 0;
    virtual void diff() = 0;
};

class Base : ISample //still an Abstract class
{
    int n;

    public:

        Base()
        {
            cout<<"Base default constructor!!!";
            n=0;
        }

        Base(int val) : ISample() //calling Interface constructor
        {
            cout<<"Base param constructor!!!";
        }

    virtual void add()
    {

    }
    virtual void diff() = 0;
};

class Der:Base //Derived class
{
    int a;

public:
    Der()
    {
        cout<<"Der default constructor!!!";
        a=0;
    }

    Der(int val) : Base(val) ////calling Abstract class constructor
    {
        cout<<"Der param constructor!!!";
    }

     void add()
    {
        cout<<"Derived add function!!!";
    }

    virtual void diff()
    {
        cout<<"Derived diff function!!!";
    }
};

int main()
{
    Der obj2(10); //creating an instance of Derived class
}

Note: In the above scenario the construcor of interface and abstarct class is getting called from the derived class constructor. Does it NOT mean that creating an instance of Interface / abstract class?? which is inturn NOT possible! But THIS IS COMPILING.

解决方案

A C++ abstract class is not, by default an interface, i.e. it may contain functionality implementation as well.
As documentation[^] states:


A class that contains at least one pure virtual function is considered an abstract class.



that is it might contain method implementations as well (it is also shown in the linked documentation sample code).


A constructor starts by invoking constructors for its bases, then constructors for its members, and then executing its code (if any)".

Whether you class has virtual member functions, pure or otherwise, has nothing to do with this. One thing to be aware of though, is that it''s impossible to call virtual functions of derived classes during construction or destruction.

In C++ we consider those objects to not exist because either their constructor has not been called, or, in the case of destructors, they no longer exist - even if the memory they occupied is still allocated.

Best regards
Espen Harlinn


// which is inturn NOT possible!
:)

// But THIS IS COMPILING
:)

// Does it NOT mean that creating an instance of Interface / abstract class??
The instance will be created implicitly, as a part of a Der-object.
Hence any member of ISample/Base would be counted to sizeof(Der).

But yes, you are right,
it is inpossible to create an indifferent (without diff()) instance directly :)

PS: Something about interfaces:
An abstract interface will be used also to mask an object out:

// client.h
void test(ISample* pISample)
{
  // Please note:
  // the user-function can not see the real object behind the interface
  if (pISample) {
    pISample->HowAreYou();
  }
}

// Sample.h
class ISample
{
public:
  virtual void HowAreYou() = 0;
};

// MaskedObject.h
#include "Sample.h"
class MaskedObj : public ISample
{
// here are "megabytes" of own masked Info...
//...

// Test
  void SomeFunction();

// Sample functionality:
public:
  virtual void HowAreYou() { MyMessageBox("Fine, thank you!"); }
};

// MaskedObj.cpp
#include "client.h"
void MaskedObj::SomeFunction()
{
  ::test(this); // the test-function will know me as an Interface only and not as a MaskedObj
}


这篇关于C ++:可以从派生类构造函数中调用ABSRACT CLASS构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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