Static 2nd Array

I am trying to define and use a 2 dimensional array in C ++

const float twitterCounts[][5] = {
        {1,0,0,0,0},
        {0,2,0,0,0},
        {0,0,3,0,0}
};

returns it as follows:

const float ??? TwitterLiveData::data() {
    return twitterCounts;
}

and using it that way

float x = TwitterLiveData::data()[1][1];

What is the correct signature for a static accessor?

+3
source share
3 answers

You cannot return an array, but you can return a pointer to its first element or array reference. You just need to enter the correct type in the signature:

const float (*TwitterLiveData::data())[5] {

Or maybe,

const float (&TwitterLiveData::data())[3][5] {
+5
source

See: fooobar.com/questions/671236 / ...

Summary:

#include <array>

const std::array<std::array<float,5>,3>
twitterCounts = {
        {1,0,0,0,0},
        {0,2,0,0,0},
        {0,0,3,0,0}
};

const std::array<std::array<float,5>,3>
TwitterLiveData::data() {
    return twitterCounts;
}

You might not want to return the array by value, as this may be too expensive. Instead, you can return an array reference:

const std::array<std::array<float,5>,3> &TwitterLiveData::data();

Anyway, your desired syntax float x = TwitterLiveData::data()[1][1];works.

+3

, . , twitterCounts float **.

0

All Articles