简体   繁体   中英

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.

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. 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.h (simplified)

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::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.

I'm still fairly new to C++ after working with C#!

First, there's no such thing as an array parameter type in C++. In your SegmentPanel constructor, the MapSegment* mapSeg[512] parameter is actually equivalent to 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 . This undergoes array-to-pointer conversion which turns it into a pointer to its first element (a MapSegment** ) and then passes that pointer.

This is all fine, but you do nothing with the mapSeg argument in your constructor. 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:

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.


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 ". You'll then need to make the member mapSeg the same type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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