Inheritance question
I have a class called Scanner which casts rays into a 3d scene and records the resulting intersections. It has the following type of properties:
Location (a 3d point)
Forward (a 3d vector)
Up (a 3d vector)
min/max angle (the bounds of where to cast rays)
theta/phi step (the directions of the rays)
I've been saving the results in a class called Scan. But I'd like for a Scan object to know everything about the Scanner that created it. So this doesn't fit the "is a" paradigm of OOP inheritance - so these classes are not related in my current setup. How would you structure these? It doesn't really make sense for a Scan to exist without having come from a Scanner, so my first thought was just to make
class Scanner
{
Scan TheScan;
};
But then for some functions it doesn't make sense to pass only TheScan to them because it needs some info from the Scanner, but it also doesn't make intuitive sense to pass the whole Scanner object:
double ComputeAverageDistance(const Scan &TheScan)
{
//for each intersection, find the distance to the scanner
}
In that example - the location of the scanner would need to be known to compute the distance. Passing the whole scanner seems weird: ComputeAverageDistance(const Scanner &TheScanner) .
Have I explained my concern well at all?
Thanks,
Dave
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
So you would think
Scan "was made by" Scanner?
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
Thanks Tom Gunn. Can anyone else comment if they would do this the same way Tom recommended?
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
daviddoria,
Think about nested class,
class Scanner
{
private:
class Scan
{
};
public:
Scan sc;
....
};
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
Ok, it's something to keep in mind - but I don't see how that helps in this situation over just having a Scan member variable inside Scanner?
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
You can declare any of those 2 classes into the other one, as long as you have the respective function returning and using the right data. To make a reference to the thread name, I would declare the scanner class as a base class for scan class, so the scan class can use the necessary data from the scanner class.
losh177
Junior Poster in Training
54 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
Yea that's kinda what I was thinking, but it doesn't follow the "is a" OOP idea, but maybe sometimes you don't have to follow that?
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
Well, to my understanding being able to create a base/child relation between classes forms part of the main idea of object oriented programming. If I got the wrong idea, by all means, let me know.
losh177
Junior Poster in Training
54 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
You are exactly correct - Scanner is doing stuff and Scan is a passive "record". The dilemma is in that some of the properties are common to both, e.g. number of points, the location the scan was taken from (which is currently the Location of the Scanner and the ScannerLocation of the Scan). There are about 10 of this type of "shared" property that I have been quite annoyingly copying into the Scan object each time one is created.
Does that clear things up?
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204