2

I have this:

struct myClass{
    multiset<string,binPred<string> > values ;

    myClass(const char param1, const char param2) : values(less<string>())
    { }
} ;

I need to initialize the values member with a different functor depending on the values of param1 and param2. Unfortunately, the logic to decide which functor to use is not so simple and once values is constructed I cant change its associated comparison functor.

So... I would need to put all that decision logic in the member initializaion part, but I dont know how aside using the ?: operator.
Is it possible to put more complex statements in there?? (like switch staments)

If not, is there a way to delay the construction of values so I can initialize it in the constructor's body??

Thanks.

GetFree
  • 40,278
  • 18
  • 77
  • 104

3 Answers3

9

Call a function:

myClass(const char param1, const char param2) 
             : values( MakeComplicatedDecision( xxx ) ) {
}

and put your logic in the function.

4

You can use a static member function that will accept the parameters you have and return a necessary value. This solves the problem completely and allows for clean easily debuggable code.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
0

You could use a pointer to a multiset for values, then use new to create it in the constructor. This would delay construction, but it does mean a small amount of overhead.

Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102