c++ - 就像一个结构数组而不声明数组

标签 c++ arrays class struct output

我开始编写这段代码只是为了好玩,我很惊讶它能编译并运行。 (我正在使用 Dev C++ 进行编译。)我想创建一个结构数组,但在我创建数组之前,它存储和读取值就像它已经是一个数组一样。为什么会这样,我如何创建结构数组以便跟踪数组的总大小?

#include <iostream>
using namespace std;

struct Shape
{
 public:
 int x,y,z;
};

struct Triangle: public Shape
{
 public:
 int tNum;
};

struct Rectangle: public Shape
{
 public:
 int rNum;
 float differentNum;
};

struct Circle: public Shape
{
 public:
 int cNum;
}; 

int main(void)
{ int i;
 Triangle tri;
 Rectangle rec; 
 Circle cir; 
 Shape *arr[3]={&tri, &rec, &cir};

 for(i=1;i<5;i++){ arr[1][i].x=i*2; cout<<"set  tri["<<i<<"] "<<i*2<<endl;} cout<<endl; 
 for(i=1;i<5;i++){ arr[2][i].x=i*9; cout<<"set  rec["<<i<<"] "<<i*9<<endl;} cout<<"-----------------"<<endl;

 for(i=1;i<5;i++){ cout<<"read tri["<<i<<"] "<<arr[1][i].x<<endl;} cout<<endl;
 for(i=1;i<5;i++){ cout<<"read rec["<<i<<"] "<<arr[2][i].x<<endl;}

 system("pause");
 return(0);
}

/*output   
set  tri[1] 2   
set  tri[2] 4  
set  tri[3] 6  
set  tri[4] 8  

set  rec[1] 9  
set  rec[2] 18  
set  rec[3] 27  
set  rec[4] 36  
-----------------`  
read tri[1] 2  
read tri[2] 4  
read tri[3] 6  
read tri[4] 8

read rec[1] 9  
read rec[2] 18  
read rec[3] 27  
read rec[4] 36*/

最佳答案

您有一个包含 3 个 Shape* 变量的数组,每个变量都指向堆栈中的一个有效 Shape 实例:

  • arr[0] 指向 tri 实例,所以 arr[0][0]tri 实例。
  • arr[1] 指向 rec 实例,所以 arr[1][0]rec 实例。
  • arr[2] 指向 cir 实例,所以 arr[2][0]cir 实例。

arr 的任何其他使用基本上都是非法的(即使它可能有效)。

尽管不推荐,rec 实例和 cir 实例紧跟在 tri 实例之后出现在堆栈中,允许您以其他几种方式使用 arr 并“保持活力”:

  • arr[0][1]rec 实例,它出现在堆栈中 tri 实例之后。
  • arr[0][2]cir 实例,它出现在堆栈中 rec 实例之后。
  • arr[1][1]cir 实例,它出现在堆栈中 rec 实例之后。

正如下面评论之一所指出的,这实际上取决于堆栈的实现。

任何其他(不同的)尝试通过 arr 访问内存都是潜在的内存访问冲突。

以下是您应该如何操作:

#include <string>
using namespace std;

class Shape
{
protected:
    Shape() {}
    virtual ~Shape() {}
    string type;
    int x,y,z;
public:
    string GetType() const {return type;}
    void SetX(int val) {x = val;}
    void SetY(int val) {y = val;}
    void SetZ(int val) {z = val;}
};

class Triangle : public Shape
{
public:
    Triangle():type("Triangle") {}
    void SetNum(int val) {tNum = val;}
private:
    int tNum;
};

class Rectangle : public Shape
{
public:
    Rectangle():type("Rectangle") {}
    void SetNum(int val) {rNum = val;}
    void SetDifferentNum(float val) {differentNum = val;}
private:
    int   rNum;
    float differentNum;
};

class Circle : public Shape
{
public:
    Circle():type("Circle") {}
    void SetNum(int val) {cNum = val;}
private:
    int cNum;
};

...

Triangle  tri;
Rectangle rec;
Circle    cir;
Shape*    arr[3] = {&tri, &rec, &cir};
int       size = sizeof(arr)/sizeof(*arr);
for (i=0; i<size; i++)
{
    arr[i]->SetX(i*2);
    cout << "set " << arr[i]->GetType() << "[" << i << "] = " << i*2 << endl;
}

关于c++ - 就像一个结构数组而不声明数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22131389/

相关文章:

c++ - OpenCV 编译问题

java - 在 C++ 与 Java 中利用对象的巨大 vector (或数组?)的最快方法

c++ - 使用 Code::Blocks 在 Windows 上构建 FLTK

javascript - 访问对象数组的元素

c# - 什么时候在 C# 中使用类?

c++ - 创建多个txt文件以在c++中存储数据

java - 在三维空间中找到彼此最近的两个点

javascript - 递归函数数组到字符串javascript

python - 我什么时候真正使用类

java - 使用构造函数扩展类