C 可以通过使用要求其为某个类别的对象来声明对象
C++, Is is possible to declare an object with using the requiring it to be of a certain class?
首先,我是C 的新手,尤其是以OOP方式使用C 。我有一个带有多个子类的课程,我想知道我是否可以模棱两可地声明一个变量以接受对象而不限制可以存储哪些对象。我之所以问,是因为其中一个孩子最终会一次被使用。因此,如果我不能模棱两可声明一个变量,则可以确定正在使用的众多变量中的哪些方法。
的线 obj randomObj = new className;
而不是
className randomObj = new className
您说您是C 的新手,以及用来描述想要的内容的语法表明您对Java或C#(例如Java或c#)的其他语言更熟悉。您显示的语法在这些语言中正常工作:
Foo myFoo = new DerivedFoo;
这起来是因为,在Java和C#myFoo
中的幕后实际上是指向Foo的指针,而不是作为能够存储Foo的固定内存区域的名称。在C 中,语法Foo myFoo
创建了这样的固定内存区域。即使您尝试通过这样做将一些派生类型放在那里:
DerivedFoo myDerivedFoo;
Foo myFoo = myDerivedFoo;
myFoo
仍然只能容纳一个foo对象。在myFoo
初始化期间,所有不是FOO的东西都被"切成",因为它不适合该固定内存区域。
因此,在C 中,您必须通过使用C 的指针语法明确地做Java和C#在幕后做的事情:
Foo *myFoo = new DerivedFoo;
现在myFoo
是指向FOO的指针,该指针可以指代任何Foo
对象,包括衍生品的FOO部分,而无需任何切片或任何发生的任何东西。new DerivedFoo
创建一个内存区域,其中可能存在衍生品,然后将myFoo
设置为指向创建DerivedFoo
的Foo
部分。
由于所有类均来自公共基类,因此您可以将变量声明为基类类型,但仍然能够为其分配任何派生类,例如:
class BaseClass
{
...
};
class DerivedClass : public BaseClass
{
...
};
BaseClass *randomObj = new DerivedClass;
容器只能由同一类型的对象组成。如果您想要一个异质收藏,则需要将"句柄"类型粘贴到容器中,并间接访问对象。标准手柄类型为A Pointer ,标准结构为std::vector<std::unique_ptr<Base>>
,如下:
#include <memory>
#include <utility>
#include <vector>
template <typename T, typename ...Args>
std::unique_ptr<T> make_unique(Args &&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); // or "::new"
}
struct Base
{
virtual ~Base() { }
};
struct Derived1 : Base { /* ... */ };
struct Derived2 : Base { /* ... */ };
struct Derived3 : Base { /* ... */ };
std::vector<std::unique_ptr<Base>> v;
用法:
v.push_back(make_unique<Derived1>(true, 'a'));
v.push_back(make_unique<Derived2>(11, 22, 33));
v.push_back(make_unique<Derived3>("hello", "world"));
- std::unordered_map 类型对象声明期间出现"field has incomplete type"错误
- 对象声明中的模板"error: expected expression"
- 为什么我的对象声明不调用默认构造函数?
- C++文件中.cpp静态对象声明
- 使用自定义对象声明shared_ptr数组时出现错误 C2664
- 具有多态性的对象声明中的逗号
- 如果我的班级是字面的班级,那么将我的类的对象声明为constexpr是多余的
- 何时应在Qt中将子对象声明为其父类的成员变量
- 在将对象声明为挥发性时,如何在C 中超载运算符
- 将一个类的对象声明为另一个类(C++)的成员
- 将对象声明为其自身的成员变量
- C 全局对象声明
- 尝试和捕获中的对象声明未在范围内定义
- 向量对象声明后的括号是什么意思
- C++对象声明被称为抽象
- 将派生对象声明为类中的私有成员时发生编译错误
- C++中的优化(对象声明)
- C++ 中类之间的对象声明
- 成员对象声明的差异
- 如果将类的对象声明为另一个类的实例变量,则调用该类的构造函数