Question

I am having hopefully a simple issue that I could not find a specific answer to in regards to the 'variables' being initialized. Here is the initializer list in question: Triangle.cc

Triangle::Triangle(Vector _a, Vector _b, Vector s, Point o)
    : a(_a), b(_b), Shape(o, new RectilinearAnimationStrategy(s, bBoxMaxX(), bBoxMinX(), bBoxMaxY(), bBoxMinY())){}

I know that you must initialize the variables in the same order as they are defined in the class but what about this case when you are initializing an object as well? I need a and b initialized explicitly before the call to Shape in order for the correct values to be returned to RectilinearAnimationStrategy. Everything I try gives me

Triangle.h: In constructor Triangle::Triangle(Vector, Vector, Vector, Point):
Triangle.h:11: warning: Triangle::b will be initialized after
Triangle.cc:9: warning: base Shape
Triangle.cc:9: warning: when initialized here

Any help is greatly appreciated, thank you.

I do not get that error with the current setup nor with the first answer given, but the output does not take a and b into account (not initialized when the Shape constructor was called)

Was it helpful?

Solution

I understand that initialization of Shape depends on function bBoxMaxX() which depends on initialization of member b. Since base class Shape is initialized before member b the code will not work correctly.

The solution is to have a bBoxMaxX() function that uses the initializing variables _b. Or a initializer for Shape that uses _b directly. For example:

Triangle::Triangle(Vector _a, Vector _b, Vector s, Point o) : Shape(o, new RectilinearAnimationStrategy(s, bBoxMaxX(_b), bBoxMinX(_b), bBoxMaxY(_b), bBoxMinY(_b))),  a(_a), b(_b) {}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top