I have a base class called "ModelFile" which I derive several types of 3D model file classes from.

Many of these types of files are just a set of points, so in the base class I have a vector<Point> member. Some of the derived file classes also have vector<Triangle> members. The problem is, in a class with triangles, if I move the points, I need to update the triangles (even if the triangles have their 3 points stored as the address of the points, I still need to recalculate normals). I thought of two solutions:

1) put the vector<Triangle> in the base class so I can call an UpdateTriangles() function every time a MovePoints() function is called (which is in the base class). The vector<Triangle> would just be empty for derived classes without triangles and therefore the update triangle function would have nothing to do in those cases.

2) is there a way to somehow flag/trigger something to happen in particular derived classes? ie when MovePoints() is called, at the end of the function say "if the current class is a derived class with triangles, do soemthing"?

Any suggestions are appreciated!

Thanks,

Dave

Don't use the first option. The whole point of a Base class is capturing functionality that is common to all derived classes.

As to the second option, there are many ways. Such as....

Provide a protected virtual function named UpdatePoints() in your base class. Implement it to do nothing (i.e. a body with nothing in it). Call it from your MovePoints() function, preferably after updating the vector<Point> member.

Override it in the derived class as appropriate (eg in your class with triangles, use it to update the vector<Triangle> based on the updated points).

Making it protected simply ensures it can't be called outside the class hierarchy (as there's probably no point in that). That's optional, depending on whether you want to allow it to be called by arbitrary code, obviously.

ah great - i see.

I'll get this OOP thing eventually.. haha

actually one more thought

If there are multiple derived classes which need to update the triangles, then the function has be to implemented (copied and pasted) into each of them. This sounds like a bad thing to do, no?

If there are common things that all classes which handle triangles have to do, put those functions into their own class (eg TriangleModelFile derived from ModelFile). Derive the specific/multiple classes (eg SpecificTriangleModelFile) from that generic triangle-handling class, and give it specific behaviour it requires.

It's often a good idea to make base classes are abstract (i.e. you can't instantiate them) and derived classes are concrete (i.e. they can be instantiated). That way you reduce the temptation to move things to a base class that are only needed by some derived classes.

commented: Great advice =) +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.