在另一个文件中使用来自另一个 class 的变量

Using variables from another class in another file

我的问题是关于在不同文件中的另一个 class 中使用一个 class 的变量。

我在 Mabbs Input.h 中有一个 class,它看起来像这样:

class fileParameters{
public:
     static int numImageRows;
     static int imageRowLength;

private:
    int numRows=0;    
};

int numImageRows = 640;
int imageRowLength = 480;

我想在名为 Image Centering.cpp 的单独文件中使用变量 numImageRows 和 imageRowLength。我知道我需要将它放在 header 图片 Centering.h 中,我已经这样做了。

这是我在图片中的代码 Centering.h:

class imageCenteringParameters{

public:
   static int numImageRows;
   static int imageRowLength;


private:
    int imageArray[numImageRows][imageRowLength];

};

我有两个问题:

a.) 这是确保我可以在任何其他文件中使用 Mabbs Input.h 中 class fileParameters 中的变量的正确方法吗?如果是这样,是否有更好/更有效的方法?如果没有,我将如何解决这个问题,有什么好的网站可以学习这个?

b.) 它表示 imageArray 中的字段必须具有恒定大小。我认为它们会,因为它们是在 Mabbs Input.h 中声明的。我将如何解决这个问题,但更重要的是,这是什么意思?

    C++ 中的
  1. 类 可以扩展,创建新的 classes 保留基础 class 的特征。这个过程称为继承,涉及基 class 和派生 class:派生 class 继承基 class 的成员,它可以在其上添加它自己的成员。

两个class的继承关系在派生class中声明。派生的 classes 定义使用以下语法:

class derived_class_name: public base_class_name
{ /*...*/ };
  1. const 限定符将数据对象显式声明为不可更改的对象。它的值在初始化时设置。您不能在需要可修改左值的表达式中使用 const 数据对象。例如,const 数据对象不能出现在赋值语句的左侧。

使用 const 类型限定符的变量定义使用以下语法:

const type variable_name = initial_and_only_value;

进一步阅读 making a constant array in C++.