简体   繁体   English

使用C ++从另一个类访问一个类中的数组

[英]Accessing an array in one class from another class with C++

I'm having trouble trying to access an array used in my main class from another class. 我在尝试从另一个类访问主类中使用的数组时遇到麻烦。 My application is an editor for making a 2d platform game - it basically allows you to place down 2D assets (segments) and build up a level. 我的应用程序是用于制作2D平台游戏的编辑器-它基本上允许您放下2D资产(段)并建立一个关卡。

My main class handles an array of map segment classes (each segment class in the array holds information such as position, scale and rotation of the segment on the map) and draws them to screen. 我的主类处理一个地图分段类的数组(数组中的每个分段类均保存该分段在地图上的位置,比例和旋转等信息)并将它们绘制到屏幕上。

I have a separate class which is basically a panel (dragabble, and resizable like you would find in something like Photoshop) that is initialised in the main class and is used to draw a grid of available segments from a file. 我有一个单独的类,该类基本上是一个面板(在象Photoshop这样的应用程序中可以找到,并且是可调整的),该面板在主类中进行了初始化,用于从文件中绘制可用段的网格。 What I need is the ability to click on one of the segments which then adds information to the array that is referenced in the main class. 我需要的是能够单击这些段之一,然后将信息添加到主类中引用的数组中。

I have my main class "Map" which declares an array: 我的主类“ Map”声明了一个数组:

map.h (simplified) map.h(简体)

class Map
{
public:
  MapSegment* mapSeg[512];
};

I'm then trying to send a reference of that array when I create the panel to display the available segments, like so: 然后,我在创建面板以显示可用段时尝试发送该数组的引用,如下所示:

Panel* segmentPane = new SegmentPanel(sf::Rect<float>(200,200,250,200), mapSeg);

Segment Panel header is formed as follows: 段面板标题的格式如下:

class SegmentPanel : public Panel
{
public:
    SegmentPanel(sf::Rect<float> _position, MapSegment* mapSeg[512];);
    void Update();
    void Draw(sf::RenderWindow & renderWindow);
    void ReadSegments();
private:
    std::vector<SegmentDefinition *> segDef;
    MapSegment* mapSeg[512];
};

And SegmentPanel cpp: 和SegmentPanel cpp:

SegmentPanel::SegmentPanel(sf::Rect<float> _position, MapSegment* mapSeg[512])
    : Panel(_position)
{
    panelTitle = "Segment Selection";
}

void SegmentPanel::Update()
{
    // Update segments
}

void SegmentPanel::Draw(sf::RenderWindow & renderWindow)
{
    // Draw default panel items
    Panel::Draw(renderWindow);

// Draw segments
}

However, add elements to the array from SegmentPanel.cpp class doesn't seem to be reflected in my main Main class - it seems to create a new array in memory. 但是,从SegmentPanel.cpp类向数组添加元素似乎没有反映在我的主Main类中-它似乎在内存中创建了一个新数组。

I'm still fairly new to C++ after working with C#! 在使用C#之后,我对C ++还是相当陌生!

First, there's no such thing as an array parameter type in C++. 首先,在C ++中没有数组参数类型之类的东西。 In your SegmentPanel constructor, the MapSegment* mapSeg[512] parameter is actually equivalent to MapSegment** mapSeg ; 在您的SegmentPanel构造函数中, MapSegment* mapSeg[512]参数实际上等效于MapSegment** mapSeg it's just a pointer to a pointer! 它只是一个指向指针的指针!

Panel* segmentPane = new SegmentPanel(sf::Rect<float>(200,200,250,200), mapSeg);

Here, you attempt to pass the array mapSeg . 在这里,您尝试传递数组mapSeg This undergoes array-to-pointer conversion which turns it into a pointer to its first element (a MapSegment** ) and then passes that pointer. 这将进行数组到指针的转换,该转换将其转换为指向其第一个元素( MapSegment** )的指针,然后传递该指针。

This is all fine, but you do nothing with the mapSeg argument in your constructor. 一切都很好,但是您对mapSeg函数中的mapSeg参数不执行任何操作。 If you want access to the array, you'll need to store that pointer somewhere. 如果要访问数组,则需要将该指针存储在某个地方。 You can do that by changing the member of SegmentPanel called mapSeg to: 您可以通过将SegmentPanel名为mapSeg成员更改为:

MapSegment** mapSeg;

Then change your constructor to: 然后将您的构造函数更改为:

SegmentPanel::SegmentPanel(sf::Rect<float> _position, MapSegment** mapSeg)
    : Panel(_position), mapSeg(mapSeg)
{
    panelTitle = "Segment Selection";
}

Note the initialisation of mapSeg in the member initialization list. 需要注意的初始化mapSeg在成员初始化列表。


Another way you can do this is to take a reference to array type argument instead. 您可以执行此操作的另一种方法是改为引用数组类型参数。 Your constructor would now look like: 您的构造函数现在如下所示:

SegmentPanel::SegmentPanel(sf::Rect<float> _position, MapSegment* (&mapSeg)[512])
    : Panel(_position), mapSeg(mapSeg)
{
    panelTitle = "Segment Selection";
}

The type of the mapSeg argument is a "reference to array of 512 pointers to MapSegment ". mapSeg参数的类型是“对指向MapSegment的512个指针的数组的引用”。 You'll then need to make the member mapSeg the same type. 然后,您需要做的成员mapSeg同一类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM