2

I am trying to figure out how to initialize a const class member variable, (in this case a vector), to some arbitrary values in the class's constructor.

Basically, in the class definition, I have:

const vector < pair <float, float> > myVector;

In the class's constructor however, I would like to get the size of myVector to something arbitrary, and also populate it with arbitrary values. However, my compiler is saying that since it is a const, it cannot be changed, which makes sense, but I am unsure of how to then accomplish what I am trying to do here.

Spacey
  • 2,941
  • 10
  • 47
  • 63
  • You'll have to define "arbitrary". Do you really mean "arbitrary"? If not, what values? How many? Where do they come from? If _you_ don't know what the vector should contain then neither does the compiler. – Lightness Races in Orbit May 26 '15 at 01:39
  • @LightnessRacesinOrbit The size will come from the user, but the contents are specific but known ahead of hand. For example, if the user picks the size of "2", the pattern is (-9, 7) and (-4,2), but if the user picks a size of "3", then the pattern is (-32,1), (-4,1), (2.1,1). Thanks. – Spacey May 26 '15 at 01:43

1 Answers1

1

Assign it in an initializer list:

class MyClass {
public:
    MyClass()
    : myVector(createVector())
    { }

private:
    vector<pair<float,float>> createVector() {
        vector<pair<float,float>> v;
        // populate with whatever
        return v;
    }

    const vector<pair<float,float>> myVector;
};
Barry
  • 286,269
  • 29
  • 621
  • 977
  • Why do you create a function for it? Can't you do it directly using `{}`? `MyClass() : myVector{...} {}` BTW you forgot to actually define the member – Lightness Races in Orbit May 26 '15 at 01:40
  • @LightnessRacesinOrbit maybe you need a for loop to define it, or some other complicated expressions. Added the member in. – Barry May 26 '15 at 01:41
  • Maybe.​​​​​​​​​ Then dupe of either http://stackoverflow.com/q/14495536/560648 or http://stackoverflow.com/q/11575637/560648, depending on the OP's requirements. – Lightness Races in Orbit May 26 '15 at 01:42
  • Thanks Barry, but where is the `const` here? – Spacey May 26 '15 at 01:45
  • @LightnessRacesinOrbit There's gotta be a better dupe with initializing something more complicated than a numeric type? – Barry May 26 '15 at 01:46
  • @Learnaholic Omitted by mistake, added. – Barry May 26 '15 at 01:46
  • @Barry: I don't see why the type is relevant. One of those I linked to is spot on here. It's the fact that the value comes from procedural computations that's relevant. – Lightness Races in Orbit May 26 '15 at 01:51
  • Fantastic, thanks Barry. One question though, can you please explain a little what is happening under the hood here? I particular, what is going on in `myVector(createVector())`? I get that this will be called during the constructor, how I do not get how myVector can accept a function within its constructor, but also even if it could, how is it that it's const values now correspond to the non-const values that were returned? Many thanks! – Spacey May 26 '15 at 01:52
  • @LightnessRacesinOrbit ::shrug:: You're welcome to close it then, I have no objections. – Barry May 26 '15 at 01:55
  • @Learnaholic It's not constructed with a function, we're calling the function - which returns a `vector`. It's constructed from that temporary. Although, with RVO, technically the object returns from `createVector()` will be constructed in-place in `myVector`. – Barry May 26 '15 at 01:56