Class initialization problem

Hi,
this is sort of a follow up on a previous inquiry I had (http://cplusplus.com/forum/beginner/68176/). In it I have been wondering how to get data into a class and have it available for all member without them being able to change them. The solution was the following:

1
2
3
4
5
6
struct SampleClass 
{
    SampleClass( const InputData& data ) : data_( data ) {}
    // ...
    const InputData data_ ;
};


Which has worked well for me so far.

Now, however, I have a new problem and can't think of anything how to solve it. In the Program I am working on I need to know the location of certain elements within a vector. For most of them it is quite trivial and I have done the following:

1
2
3
4
5
6
7
8
struct SampleClass 
{
    SampleClass( const InputData& data ) : data_( data ),
                                           nodeA_(data_.node.size()-1) {}
    // ...
    const InputData data_ ;
    const int nodeA_
};


Now as long as I know the position I have no problem. The thing is that I am lacking that information for one element. The information is however stored in data_ and can be extracted as follows:

1
2
3
for (int i=0; i<data_.node.size(); i++){
   if (data_.node[i].type== 'P') nodeP_=i;
}


This however does not work as part of the initialization (see below) of the constructor (as I had expected).

1
2
3
4
5
6
7
8
9
10
11
12
struct SampleClass 
{
    SampleClass( const InputData& data ) : data_( data ),
                                           nodeA_(data_.node.size()-1),
                                           for (int i=0; i<data_.node.size(); i++){
                                              if (data_.node[i].type== 'P') nodeP_=i;
                                           } {}
    // ...
    const InputData data_ ;
    const int nodeA_;
    const int nodeP_;
};


Is there any way around this?
Thanks,
Ben
Last edited on

Code to be executed lives in the constructor body, not the initialisation list.

1
2
3
4
5
6
7
 SampleClass( const InputData& data ) : data_( data ),
                                       nodeA_(data_.node.size()-1)
                                          
{ 
  for (int i=0; i<data_.node.size(); i++)
  {if (data_.node[i].type== 'P') nodeP_=i}
};
Last edited on
But that means that I can not have a (to the class) global constant if I don't know where in the input data I can find the value I would like to write into it. Right?
I have no idea what you just said. I think you might be asking the following:

"How can I have a member variable in the class that is the same for all instances of that class (i.e. shared between them) and that cannot change after being set once, which will be the first time I create an instance of the class?"

Is that what you're asking?

Last edited on
yes! that is what I would like to do. And that works for data_ as I am just using data. but how is that done if I am not sure what element of data.node I wan't to store?
Topic archived. No new replies allowed.