【问题标题】:Dynamic allocated class object动态分配的类对象
【发布时间】:2012-03-07 21:49:05
【问题描述】:

我有一个简单的类,你可以在下面看到:

问题

  1. 我可以在我的Playground 类构造函数中有这样的Playground(int aRow, int aColumn); 如果YES 在这种情况下我必须如何为行和列的元素分配内存?如果为什么?
  2. 为什么我不能写mPlayground[0][0] = 0,我该怎么做?如果可以的话。

Playboard.h

#pragma once
#ifndef PLAYBOARD_H
#define PLAYBOARD_H

class Playground
{
public:
    Playground()
    {       
    }
};

class Playboard
{
public:
    Playboard();
    ~Playboard();

private:
    Playground** mPlayground;
    int mRows;
    int mColumns;

public:
    void Initialize(int aRow, int aColumn);
};

#endif /** PLAYBOARD_H */

Playboard.cpp

#include "Playboard.h"
Playboard::Playboard()
{
    mPlayground = 0;
}

void Playboard::Initialize(int aRow, int aColumn)
{
    // Set rows and columns in order to use them  in future.
    mRows = aRow;
    mColumns = aColumn;

    // Memory allocated for elements of rows.
    mPlayground = new Playground*[aRow];
    // Memory allocated for elements of each column.
    for (int i=0; i<aRow; i++)
        mPlayground[i] = new Playground[aColumn];
}

Playboard::~Playboard()
{
    // Free the allocated memory
    for (int i=0; i<mRows; i++)
        delete[] mPlayground[i];
    delete[] mPlayground;
}

【问题讨论】:

  • 您是否在编译器中尝试过您的代码?我看不出它有什么问题。
  • 你可以,但你不应该使用new。如果这样做,如果其中一个分配失败,您将泄漏内存。我们通常在 C++ 中将std::vector 用于数组。
  • @BenRussell 你读过问题了吗?
  • @LuchianGrigore 我认为他没有
  • @avakar 我知道std::vector,但我不想使用它。我想在不使用 stl 库的情况下编写我的课程。

标签: c++ class memory-management dynamic


【解决方案1】:

1a 我可以在我的 Playground 类构造函数中有这样的 Playground(int aRow, int aColumn);

是的,很简单:

class Playground {
    int x, y;
    Playgrown(int aX, int xY) : x(aX), y(aY) {}
};

1b 如果是,我必须如何为行和列的元素分配内存?如果没有,为什么?

您根本不必分配内存。 Playground 不包含指针,因此不需要分配。

2 为什么我不能写mPlayground[0][0] = 0,我该怎么做?如果可以的话当然可以。

因为您没有为Playground 重载赋值运算符。例如,

class Playground {
    …
    // Sample operator=. You'll need to implement its semantics
    void operator=(int) {}
};


您不能使用new 初始化数组的成员。你也许可以这样做:
{
    mPlayground[i] = new Playground[aColumn];
    for(int x = 0; x < i; x++)
      mPlayground[i][x] = Playground(3,4);
}

【讨论】:

  • 运算符 = 不返回 void。
  • 我想写这样的东西mPlayground = new Playground(2, 4)*[aRow];
  • @LuchianGrigore - 是的,operator= 完全能够返回 void。有关示例,请参见 here
  • @ViToBrothersApoyan - 假设你可以写这个,你想表达什么意思?
  • @Rob 你是对的,对不起。我知道你可以,但不认为这是标准的。 +1
【解决方案2】:

1) 是的:

Playground(int aRow, int aColumn)
{
}

2) 编辑:抱歉,我认为这是一个更复杂的问题。我将在此处留下以下答案以供将来参考。为了能够写mPlayground[0][0] = 0,你需要重载

Playground& Playground::operator = ( int x );

旧答案:

为了能够从Playboard 类访问Playground 对象,您可以重载() 运算符并调用:

Playground Playboard::operator()(int r, int c)
{
    return mPlayground[r][c];
}

//...
Playboard p;
p(x,y);

[] 运算符:

Playground* Playboard::operator[] (int r)
{
    return mPlayground[r];
}

 //...
 Playboard p;
 p[x][y];

【讨论】:

  • OP 的问题不清楚,但他的Playground 没有成员mPlayground。似乎Playboard 包含Playground 数组,而不是Playground 包含额外的Playground 数组。
  • 但是我可以从Playboard::Initialize Playground 构造函数中调用吗?
  • @ViToBrothersApoyan 我编辑了第二个问题的答案。
  • @ViToBrothersApoyan 好的,我以为您在问一些不同的问题。现已更正。
  • @LuchianGrigore 感谢您的第二个回答,但我仍然无法理解冷杉,因为我的 Playground 课程中没有 mPlayground
猜你喜欢
  • 2021-03-17
  • 1970-01-01
  • 2014-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-18
  • 2010-09-20
相关资源
最近更新 更多