Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / C++ operators & inheritance Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
skeb

April 15, 2005, 04:41 AM

Hi,
I've got a question. I have something like the following:


----------------------------------------------------------------------------------------------------------------

/**
* a base class, from which three childs inherit everything
*/
template
class ctBase
{
public:
tContent aX;
tContent aY;
tContent aZ;


/* Method to add the components of two objects (aX + aX and aY + aY and aZ + aZ) and return the result */
template
friend ctBase operator + (const ctBase&, const ctBase&);
}


/**
* Child A class
*/
template
class ctChildA : public ctBase
{
}

/**
* Child B class
*/
template
class ctChildB : public ctBase
{
}

/**
* Child C class
*/
template
class ctChildC : public ctBase
{
}

/**
* Main function
*/
void main()
{
ctChildA oChildA(10, 30, 80);
ctChildB oChildB(1, 3, 7);


ctChildC oChildC = oChildA + oChildB;
}


---------------------------------------------------------------------------------------------------------------------------


So I want the operator of the base class to do something with the child classes their attributes, which they inherited from the base class themself. But this doesn't work like I want it to work(like it doesn't compile!) :P
If I do something like this:

(ctBase)ctChildC oChildC = oChildA + oChildB;


then it compiles, but when it runs, the attributes of ctChildC are 0 (they should be (11, 33, 87) ).
If I do this:
ctBase oTest = oChildA + oChildB;

it works perfectly, but then I have to 'manually' copy the attributes to an ctChildC, like this:

ctBase oTest = oChildA + oChildB;
ctChildC oChildC;
oChildC.aX = oTest.aX;
oChildC.aY = oTest.aY;
oChildC.aZ = oTest.aZ;

this makes my code very unreadable and I think there must be a better way(I hope there is).
Any help would be appreciated!

Thanks!
Robbert

 
Bramz

April 15, 2005, 07:58 AM

I'm not sure what you're trying to do is a good idea in the first place. But one way to solve it is to have a constructor in ctChildC that accepts a ctBaseA


template
class ctChildC : public ctBase
{
public:
ctChildC(const ctBase& base): ctBase(base) {}
};


Mind you, depending on what you actually are doing with these childs, this will most likely cause other problems! I'd expect the result of oChildA + oChildB to be so not easily defined ...

Bramz

 
skeb

April 15, 2005, 09:00 AM

Actually ChildA + ChildB works perfectly. I think this is very logical, because ChildA and ChildB are both of the type ctBase. But the return value is also of the type ctBase.

I think the problem is that a child is also of the type of the parent, but the parent isn't of the type of the child. But maybe your idea is a good one to solve the problem of copying the components at every place you want without rewriting the same code to do this.

The reason I want to do this is that if it works, I can easily add/subtract/multiply vectors, coordinates, rotations etc, which are all childs of a tuple. This would have been great :)

Thanks!

 
Bramz

April 15, 2005, 10:15 AM

skeb wrote: But the return value is also of the type ctBase.


And that's exactly your problem ... My solution to it is only a hack, it doesn't really solve the problem. If you add two vectors, you get a vector and not a rotation. By using your scheme + conversion constructor, you'd be able to say roation = vector + vector, vector = coordinate + rotation. This simply is nonsense.

My suggestion is that you go from a IS-A relationship to an USES-A relationship. A coordinate (or point) is not necessarely a tuple, because it should not inheret all properties of a tuple. For instance, if your tuple allows addition of two tuples, this is a property that shouldn't belong to a point because you can't add two points. However, your point could contain a tuple to represent its position. Then you can define a totally new interface that possible simply reroutes the operations to the contained tuple.

Greetz,
Bramz

 
This thread contains 4 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.