调用C ++中的Abstract Base类的构造方法 [英] Calling the Constructor for the Abstract Base class in C++

查看:93
本文介绍了调用C ++中的Abstract Base类的构造方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如果我有一个抽象类,那么我将无法创建抽象类类型的对象。但是,假设基是基类,而派生是派生类。
在基类中,我有一个成员变量名称。

  Base.h 
Base(字符串名称= );

Base.cpp
Base(字符串theName){name = theName);

这不是在创建对象吗?



在派生类中,我有成员变量年龄。
现在在派生类的默认构造函数中

  Derived.h 
Derived(string name =, int theAge = 0);

Derived.cpp
Derived(字符串theName,int theAge):Base(theName){age = theAge}

不是也调用Base的默认构造函数吗?
所以,我可以调用参数化的构造函数,但不能调用默认的构造函数吗?



还有一点,如果我在Base类中有另一个函数而不是纯函数,如果不允许我为基类创建对象,该如何调用该函数?



预先感谢!

解决方案


我知道,如果我有一个抽象类,那么我将无法创建抽象类类型的对象。


有一个例外:可以将抽象类的对象创建为派生类对象的子对象。 / p>

实际上,在这种情况下必须 创建它,因为这就是继承在C ++中的工作方式。派生类的对象包含基类的对象(或者通常是基类的对象,因为我们不要忘记C ++也支持多重继承)。



标准在§10.4/ 1中非常清楚地说明了这一点:


抽象类是只能用作基类的类其他阶级的除了作为从其派生的类的子对象之外,不能创建抽象类的对象。


有您有:抽象类的实例可能在此限制内。因此,


  Base(string theName){name = theName); 

这不是在创建对象吗?


它是一个与其他构造函数一样的构造函数;它用于创建对象。鉴于上述规则,该类是抽象的事实无关紧要。


  Derived( string theName,int theAge):Base(theName){age = theAge} 

调用Base的默认构造函数




是的。


所以,我可以调用参数化的构造函数,但不能调用默认的构造函数吗?


所有参数均默认为的构造函数是默认的构造函数。


还有一点,如果我在基类中有除纯函数以外的另一个函数,如果不允许我为基类创建对象,该如何调用该函数?


如果它是 public 函数,那么拥有派生类对象的任何人都可以调用它(包括派生类本身)。此外,任何通过指针或对基类的引用访问派生类对象的人都可以调用它。当然,基类本身也可以调用它。



如果它是受保护的函数,则



如果它是 private ,则基类本身可以调用



正如我们在上面建立的那样,从技术上讲,您确实为基类创建了一个对象,作为派生类对象内部的子对象。因此,原则上来说,情况并不仅仅因为基类是抽象的。






这里要记住的是定义了语言的技术性,以便所有基本的面向对象功能都能按预期工作。您不必在日常编程业务中担心这些事情。


I know that the if I have an abstract class then I cannot create an object of abstract class type. But Suppose "Base" is a base class and "Derived" is a derived class. In base class I have one member variable name.

Base.h
Base(string name = "");

Base.cpp
Base(string theName){name = theName);

Isn't this creating an object ??

In the Derived class I have member variable age. Now in the derived class default constructor

Derived.h
Derived(string name = "", int theAge = 0);

Derived.cpp
Derived(string theName, int theAge):Base(theName) { age = theAge }

Isn't that also calling the default constructor for the Base ?? So, am I allowed to call a parametrized constructor but not the default one ?

One more thing, if I have another function in the Base class other than the pure function, how can I call that function if I am not allowed to create an object for the Base class ?

Thanks in advance!

解决方案

I know that the if I have an abstract class then I cannot create an object of abstract class type.

With one exception: An object of an abstract class can be created as a subobject of a derived-class object.

In fact, it must be created in that situation, because that's just how inheritance works in C++. An object of a derived class contains an object of the base class (or generally, objects of the base classes, because let us not forget that C++ also supports multiple inheritance).

The standard says this very clearly in §10.4/1:

An abstract class is a class that can be used only as a base class of some other class; no objects of an abstract class can be created except as subobjects of a class derived from it.

There you have it: instances of abstract classes may exist within this restriction. Therefore,

Base(string theName){name = theName);

Isn't this creating an object ??

It is a constructor like any other; it is used to create an object. The fact that the class is abstract doesn't matter, given the rule above.

Derived(string theName, int theAge):Base(theName) { age = theAge }

Isn't that also calling the default constructor for the Base ??

Yes, it is.

So, am I allowed to call a parametrized constructor but not the default one ?

A constructor in which all arguments are defaulted is a default constructor.

One more thing, if I have another function in the Base class other than the pure function, how can I call that function if I am not allowed to create an object for the Base class ?

If it's a public function, then anyone who has a derived-class object can call it (this includes the derived class itself). Additionally, anyone who accesses the derived-class object via a pointer or reference to the base class can call it. And of course, the base class itself can call it, too.

If it's a protected function, then the derived class and potential further derived classes can call it.

If it's private, then the base class itself can call it.

As we have established above, you do technically create an object for the base class, as a subobject inside of the derived-class object. So principally, the situation is not special just because the base class is abstract.


The thing to keep in mind here is that the technicalities of the language are defined so that all basic object-oriented features just work as expected. You don't have to worry about these things in your daily programming business.

这篇关于调用C ++中的Abstract Base类的构造方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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