'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 {
	virtual string getValue( void ) = 0;
	virtual void setValue( string ) = 0;

template< class T > 
class Property : public AbstractProperty {
	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() );

	T m_value;

class AbstractUniform {
	virtual void	bind( void ) = 0;

template< class T >
class Uniform : public AbstractUniform, public Property< T > {
	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" );


//----------------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.



Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
The construct you propose is perfectly legal. The fact there is a template class as a base makes no difference.
evilrixSenior Software Engineer (Avast)Commented:
>> when I use a pointer that is cast from AbstractUniform * to AbstractProperty *.

I presume that  AbstractUniform * started life as a Uniform *? If not then this won't work. If so then the cross cast should be ok (although the fact you have to do this suggests a flaw in your design!).

Try using dynamic cast. Since these classes are polymorphic you can use this to test if the cross-cast is safe.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jochemspekAuthor Commented:
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 ?




It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.