C++ composition (now: scene graph woes)

Started by
17 comments, last by tolaris 18 years, 6 months ago
[EDIT] Slide down to this post for an explanation of what it is I'm trying to do. I've been a little irritated today with how composition works...or rather, doesn't work, in C++. Yes, I can compose an object from other objects fairly easily, just by making instances as member variables. But I can't declare a function parameter that takes any type T that composes a type X, the way I can with inheritance. Nor can I declare a container of such types, or in fact do much of anything, as far as I can tell. The only way to do it is to forget composition and use MI instead, which seems to me a rather poor sort of substitute, especially given all the caveats that MI brings with it. Is there some clever C++/STL/loki/boost hack to get composed objects to be usable in a more useful and syntactically rich manner, or am I stuck with what I have? [Edited by - Promit on October 19, 2005 8:26:18 PM]
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Advertisement
I'm not sure exactly what you are asking, but if you're wondering about how to, say, create a container class that will store any type, then look up templates.

EDIT: here

EDIT2: Just looked at who I was replying to. Nevermind =)

EDIT3: Oh nevermind. If templates are what you want, then check them out if you don't know how to use them already.
my siteGenius is 1% inspiration and 99% perspiration
Use templates they are your best friends when you want genericity.

Edit: Have you noted how difficult is to reply to topics? there's always a chance somebody else will answer after you started typing and before you click the submit button and make you look stupid=)
------ XYE - A new edition of the classic Kye
Templates work ok for functions, but they don't do jack for containers. Suppose I want to store a container of pointers to objects that composes type X. How can I do that? I can't think of any way myself...I can store a container of pointers to actual X instances, but not anything that composes X.

Let me be a bit more verbose here. Suppose I want to create some kind of set of objects where a given object can have positional data, rotational data, or both.
struct RotationalData{    Matrix3x3 Rotation;    float ax, ay, az; //euler angles};struct PositionalData{    float x, y, z;};struct PositionObject{    PositionalData Position;};struct RotationObject{    RotationalData Rotation;};struct TransformObject{    PositionalData Position;    RotationalData Rotation;};

Now, how do I create a container that can hold both RotationObject and TransformObject instances?
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
well


enum PType={PT_OBJA, PT_OTHEROBJECT , ... }struct container{    void *Pointer;    PType pointertype;}




I think that last time when I was browsing Boost's documentation there was a container class.

Edit: Since they are structs you can also use an union instead of pointers

enum SType={PT_OBJA, PT_OTHEROBJECT , ... }struct container{    union    {         structA stA;         structB stB;...    };    SType structype;}

------ XYE - A new edition of the classic Kye
C++ encourages you to think about composition in terms of iteration semantics. A templated iterator pair defining a range works fairly well, and is the solution used by the standard library algorithms. Of course, it only really works out if there is a reasonably "natural" ordering that can be applied.
Quote:Original post by Sneftel
C++ encourages you to think about composition in terms of iteration semantics. A templated iterator pair defining a range works fairly well, and is the solution used by the standard library algorithms. Of course, it only really works out if there is a reasonably "natural" ordering that can be applied.


I don't quite follow. What do you mean by a "templated iterator pair"?
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Quote:Original post by Promit
Is there some clever C++/STL/loki/boost hack to get composed objects to be usable in a more useful and syntactically rich manner, or am I stuck with what I have?

Templates are pretty clever, but not a hack:
template< typename composite1, typename composite2 >class implementer {public:   explicit implementer( int some, int values ): c1( some ), c2( values ) {}   void some_function( int more ) { c1.enforced_interface( more ); return; }   void another_function( int values ) { c2.enforced_interface( values ); return; }private:   composite1 c1;   composite2 c2;};

Look into compile-time polymorphism.
Well, take a look at how, say, std::sort is defined. It takes two paramters which define an iterator range. A minor advantage, and the one that everyone assumes is the primary reason to do this, is that you can sort only a given slice of the array. A bigger advantage is that sorting can occur without knowledge of the underlying container type; only the iterator type is used.
Quote:Original post by Promit
Quote:Original post by Sneftel
C++ encourages you to think about composition in terms of iteration semantics. A templated iterator pair defining a range works fairly well, and is the solution used by the standard library algorithms. Of course, it only really works out if there is a reasonably "natural" ordering that can be applied.


I don't quite follow. What do you mean by a "templated iterator pair"?

a certain range is dictated by: iterator_begin through iterator_end. "templated iterator pair" is not some phrase you need to know; don't let it confuse you.

This topic is closed to new replies.

Advertisement