views:

102

answers:

3

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.

A: 

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
+7  A: 

Call a function:

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

and put your logic in the function.

anon
Which should have no requirement on the attributes of the class as they are yet to be initialized.
David Rodríguez - dribeas
IMHO, it should not be possible to call non-static member functions here, but unfortunately it is.
anon
+4  A: 

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