class design question

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 638
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

class design question

 
0
  #1
Oct 30th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: class design question

 
0
  #2
Oct 30th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 638
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: class design question

 
0
  #3
Oct 30th, 2008
ah great - i see.

I'll get this OOP thing eventually.. haha
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 638
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: class design question

 
0
  #4
Oct 30th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: class design question

 
1
  #5
Oct 30th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC