English 中文(简体)
C++ 某类或另一类点?
原标题:C++ pointer of one type or another?

我有一个方案解决在C区工作的大型电网中的项目,但希望将其赶到C++学习面向目标的节目。

我的问题是,我有两个数据结构,请打电话Class1Class2。 简单地说,Class1包含两倍(a>/em>和b)和Class2包含四倍(a,b,cd)。

现在,从现在起,我想产生一种动态的阵列,其中将包含一个等级:<>OR>>>>/指另一个等级,取决于菲律宾人民民主党的条件。

Something like:

if (PDEtype === (1) 点子=新类别1[n]

else              pointer = new Class2[n]

Then after that I will need to access pointer with something like: pointer[2].a = 1.0 or pointer[4].d = 10.0 etc.

工会不会发挥作用,因为我需要储存最低的记忆。 (我将处理可能有数百万点的重大问题)

Is there a way to do this in C++?

Thanks in advance!!!

问题回答

If your Class2 is actually derived from Class1, you can have an array of Class1* s. If not, I d base both Class1 and Class2 off a common base class, and had an array of BaseClass* s.

当然,你需要某种方式了解该条目的实际内容(如果适用的话,在C++中可以更好地做到)。

what you have is pretty close actually. the problem is that you will need to create the pointer beforehand and THEN call new Class1[n]. your problem seems to be needing to figure out how to declare the pointer beforehand.

有两个选择,即:

(1) 不担心宣布两种不同的点类型。 宣布“一点”类型(在这种情况下,将二类定为“一等”,因为第1类可被视为第1类的“子类”,然后才宣布第2类中未使用的数值为某种特定价值表示未使用。

2) i m 甚至不能确定这种技术是否正确,但毫无疑问,按照这些思路可以发挥作用:宣布点为Vid*。 你们还需要确定粉碎1和粉碎2,以分别标明1类和2类。 您将请Void* 点名实际上指你所定义的类别,然后形成一种功能,将这一空白点推到Ptr1(一类点)或Ptr(第二类点)。

这与造成一种真空*(一种真空点)非常相似,如果你的记忆位置实际上包含一些内乱,就把它当作一种愤怒。 然后,你可以把这一 cast子作为愤怒的指点。

最后,你在你的例子中说“第2点......a = 1.0”和“第4点]d = 10.0点”。 你们正在创建一系列课程? 如果你想要你的点子指出一个等级,那么你最终将使用“点子”和“点子”。





相关问题
Having many stacks with different types

I m making a C program that needs to use two stacks. One needs to hold chars, the other needs to hold doubles. I have two structs, node and stack: struct node { double value; struct node *...

Creating (boxed) primitive instance when the class is known

I need a method that returns an instance of the supplied class type. Let s assume that the supplied types are limited to such that an "empty" instance of them can be created. For instance, supplying ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

How to generate an instance of an unknown type at runtime?

i ve got the following in C#: string typename = "System.Int32"; string value = "4"; theses two strings should be taken to generate an object of the specified type with the specified value... result ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...