Link to home
Start Free TrialLog in
Avatar of jochemspek
jochemspekFlag for Netherlands

asked on

'triangular inheritance' of a templated class - is it legal ?

Hi there,

I'm having some issues with a construction in my class-hierarchy. At least, I think that's where the problem lies, and I'm wondering if the following construction is legal:

//-----------------------------begin code--------------------------
#include <iostream>
#include <string>

using namespace std;

class AbstractProperty {
public:
	virtual string getValue( void ) = 0;
	virtual void setValue( string ) = 0;
};

template< class T > 
class Property : public AbstractProperty {
public:
	Property( T value ):
		m_value( value ){
	}

	virtual string	getValue( void ){
		char ret[128];
		sprintf_s( ret, "%d", m_value ); 
		return( string( ret ) );
	}

	virtual void	setValue( string value ){
		m_value = atoi( value.c_str() );
	}

private:
	T m_value;
};

class AbstractUniform {
public:
	virtual void	bind( void ) = 0;
};

template< class T >
class Uniform : public AbstractUniform, public Property< T > {
public:
	Uniform( T value ):
		Property< T >( value ){
	}

	virtual void bind( void ){
		cout << "bind called with value: " << getValue() << endl;
	}
};


void main( void ){
	
	Uniform< int > * unifPtr = new Uniform< int >( 2 );

	AbstractUniform * abstrUnifPtr = ( AbstractUniform * )unifPtr;
	AbstractProperty * abstrPropPtr = ( AbstractProperty * )unifPtr;

	abstrPropPtr->setValue( "3" );
	abstrUnifPtr->bind();

}

//----------------end code--------------------------------

Open in new window


analyzing  the vtableswith visual studio, everything seems fine,
and this little testcase poses no problems, however, the real
version of my code which has the same construction, just more
member functions and -variables crashes when I use a pointer
that is cast from AbstractUniform * to AbstractProperty *. Lies
the reason for it in said construction is now my question.

Thanks,

Jonathan

Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

The construct you propose is perfectly legal. The fact there is a template class as a base makes no difference.
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jochemspek

ASKER

Ok, thanks. yes, the AbstractUniform * started as a Uniform< whatever > *, just like in the example.
I agree with you that this suggests a flaw in my design. Maybe you have a suggestion. what I'm doing is the following:

I have a scenegraph (for OpenGL-rendering), in which there are many classes that are either descendants of 'Property<T>' or 'PropertyContainer<T>', which are subclasses of AbstractProperty and AbstractPropertyContainer respectively.

The reason for this is obvious, I want to have access to the data in the scenegraph in a typesafe manner, without knowing what the graph looks like, so i can give the information to, say, a QtTreeView, or for serialization purposes. Normal subclasses of Property(Container)s are a Camera class, a TransformNode class, a Texture class, etc.

but there's one subclass of Property that's different, in that it also needs an absract interface
which differs significantly from that of Property, and that is 'Uniform', which holds a value that can be passed to an OpenGL shader. for this, it needs to be bound to a program, and again, i need to do that
without knowing the type of uniform ( be it a float, int, vec2,3 etc. )

While I'm writing this (ahaa) I wonder if I should let the program class take care of the binding instead of the uniform class. that way, Uniform can just be another property and the Propgram class can analyze what type of binding to make ( float, int, etc. )

does this make sense to you ? do you have other ideas ?

thanks,

J