0

So I have a bunch of static const arrays of unsigned type. I declare them in the header and initialize them in a cpp file. The arrays are quite large (They are carrier indices of an OFDM based Transmitter). However I have the values from a document. So it is just a matter of copy paste for initialization. So far this works fine.

However I need a second set of arrays which are nothing but the original set + a constant value.

in header

class C
{
static const uint32_t A[288];
static const uint32_t A_ext[288];
}

in cpp

const uint32_t C::A[288] = {1,2,3......};

I want A_ext to be

A_ext[i] = A[i] + 5;

I want these also to be defined as static const because all these arrays are meant to be read only everywhere else in the project. They are like standard tables which can be accessed everywhere else in the project.

How do I go about doing this?

6
  • 2
    not an answer, but if it is just +5 then you dont need to keep it in memory, you could use a function to access the same array and add 5 Commented Jun 30, 2020 at 9:47
  • since is static const, take those value and add manually 5 no?.. Commented Jun 30, 2020 at 9:51
  • 2
    If for some reason you really need to use "array index syntax" for accessing A_ext, why not create a simple class which overloads operator[] to basically just do return A[i] + 5? Commented Jun 30, 2020 at 9:52
  • @idclev 463035818 Yes this is what I was about to do because the actual operation is indeed a simple addition. I just thought it would be a good idea to have these extended values also as an array. I don't really have a reason. I just thought that would be nice. Commented Jun 30, 2020 at 9:52
  • 1
    Or if you go with the plain function (as suggested by @idclev463035818) then you could create a simple inline function to get the value from A as well, so you have a consistent interface for both A and A_ext. Commented Jun 30, 2020 at 9:54

1 Answer 1

1

I would avoid static initialization depending on each other. You could use a

struct carrier_indices_t {
    uint32_t A[288];
    uint32_t A_ext[288];
};
class C {
    static const carrier_indices_t carrier_indices;        
}

And then

const carrier_indices_t C::carrier_indices = foo();

Where foo is a constexpr function that returns a carrier_indices_t.

Alternatively, store only one of the arrays and provide two static methods to acces it (one with an offset of +5).

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for that. This "constexpr function" sounds interesting. Going by this idea may be I have to change a little too much for now. I will think about it, Thanks.
@Shreyes I cannot really tell you which one to pick. I guess the least changes you have to do if you go with 2 functions to access the values, but that wont work anymore as soon as the difference is not just an offset (though you could still use that two functions to access two different arrays)
Or a single function which in turn calls the method of Class C to access appropriate array depending on certain parameter. In that case I can keep same interface for both. like suggested by a comment on the question. Certainly that is quicker to do now. But for future I feel constexpr function is a good thing to do. I can immediately think of more uses to it. I have some more tables with complex values which require some calculations for real and imaginary parts. This helps for that.
@Shreyes I think that comment did suggest to use two functions instead of one function and accessing the array directly, because that would be inconsistent. Anyhow you can of course also use one function and select between the two with a parameter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.