指向对象数组的指针作为成员覆盖内存

0 c++ arrays class object

这是交易.我们有2个不同的类F和O类

class F {
    private:
        int x;
        int y; 
    public:
        int getXf(){ return x; }
        int getYf(){ return y; }
        f(int ,int);
};

class O {
    private:
        int n;
        int k;
        int x;
        int y;
        char type;
        int id;
        int t;
    public:
        O(int ,int ,int ,int ,int);
        int getX(){ return x; }
        int getY(){ return y; }
};
Run Code Online (Sandbox Code Playgroud)

我们有一个第三类P,我们在其中初始化值.在类中,我们创建了两个对象数组.

class Prog {                                                                   
    public:
        int count;
        int fcount;
        O *o[]; //here we are declaring the arrays of objects
        F *f[];           
    public :
        //void init(); Here is the function where we initializing the values
};
Run Code Online (Sandbox Code Playgroud)

现在我们正在创建对象的2个语句.

for(int i=0;i<10;i++){
        randx = rand() % 10;
        randy = rand() % 20;

        o[i] = new O(100,50,i,randx,randy);
    }


    for(int i=0;i<3;i++){
        randx = rand() % 10;
        randy = rand() % 10;

        f[i] = new F(randx, randy);
    }
Run Code Online (Sandbox Code Playgroud)

当我们打印所有对象都在这里但是第一个类的前3个被秒的对象替换.正好分别是10050(第一个)randxrandy(第二个).

Mik*_*our 6

O *o[];
Run Code Online (Sandbox Code Playgroud)

这声明了一个未知大小的数组,这是一个不完整的类型.C++不允许将其用作类成员,尽管某些编译器会将其作为扩展,将其解释为零大小的数组.在任何一种情况下,它都不是你想要的.

如果您知道在编译时绑定的数组,那么您应该指定它:

O *o[10];
Run Code Online (Sandbox Code Playgroud)

否则,您需要在运行时动态分配数组:

std::vector<O*> o;

for(int i=0;i<10;i++){
    randx = rand() % 10;
    randy = rand() % 20;

    o.push_back(new O(100,50,i,randx,randy));
}
Run Code Online (Sandbox Code Playgroud)

我还建议存储对象,或者可能是智能指针,而不是数组中的原始指针.如果由于某种原因你确实想要原始指针,那么记住在完成它们之后删除这些对象,因为这不会自动发生,并且不要忘记三条规则.


pmr*_*pmr 5

您正在声明数组,但您永远不会为它们分配内存.您所看到的就是您的代码遍布整个堆栈的方式.

更合适的东西:

struct X {}; struct Y {};

class P {
public:
  P() : xs(new X*[10]), ys(new Y*[10]) { init(); }

  ~P() {
    // delete all objects
    for(std::size_t i = 0; i < 10; ++i)
      delete xs[i];
    for(std::size_t i = 0; i < 10; ++i)
      delete ys[i];

    delete[] xs;
    delete[] ys;
  }
private:
  void init() {
    // initialize
    for(std::size_t i = 0; i < 10; ++i)
      xs[i] = new X();
    for(std::size_t i = 0; i < 10; ++i)
      ys[i] = new Y();
  }

  // prevent assignment and copy
  P& operator=(const P& other);
  P(const P&);

  X** xs;
  Y** ys;
};
Run Code Online (Sandbox Code Playgroud)

当然,如果您只是std::vector用来存储数据,那么所有这些魔法都变得不必要了 .