C++ template member function question...

Started by
11 comments, last by BeerNutts 12 years, 4 months ago

[quote name='kindjie' timestamp='1322863474' post='4889929']
This smells like a design problem, so I'm not recommending you actually do this, but you could just make mData a void*.

Right now, it looks like you're just writing a clumsy wrapper for the C++ type system.


The class above is just an example, it's actually an Event system, where the client sends an event to a register listener. I was looking at adding a data component to the event.

I currently am using a void*, but I thought a template solution would've been more elegant solution, so I was trying to add it in with out redefining the class.
[/quote]

At no point during the invocation of an event system should you NOT know the exact type of the parameters.

A button click event handler will take exactly one type. The instant you introduce the magical void* into things you lose all type safety and introduce significant chance of bugs.

You invoke your event with a PointF, I catch the event and cast the void* I got to a PointI, and blamo bugo. Now I've got to waste MY time debugging and trying to figure out why my PointI has such wonky values, walking the stack trace, only to find out you're using a PointF... "Gee," I say, "it sure would have been nice if this event had just had the signature for a PointF instead of a void*."

Use boost signals/slots, or any of the other existing and very useful signals and slots implementations. That way you can have end to end type safety.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Advertisement

[quote name='BeerNutts' timestamp='1322864310' post='4889936']
[quote name='kindjie' timestamp='1322863474' post='4889929']
This smells like a design problem, so I'm not recommending you actually do this, but you could just make mData a void*.

Right now, it looks like you're just writing a clumsy wrapper for the C++ type system.


The class above is just an example, it's actually an Event system, where the client sends an event to a register listener. I was looking at adding a data component to the event.

I currently am using a void*, but I thought a template solution would've been more elegant solution, so I was trying to add it in with out redefining the class.
[/quote]

At no point during the invocation of an event system should you NOT know the exact type of the parameters.

A button click event handler will take exactly one type. The instant you introduce the magical void* into things you lose all type safety and introduce significant chance of bugs.

You invoke your event with a PointF, I catch the event and cast the void* I got to a PointI, and blamo bugo. Now I've got to waste MY time debugging and trying to figure out why my PointI has such wonky values, walking the stack trace, only to find out you're using a PointF... "Gee," I say, "it sure would have been nice if this event had just had the signature for a PointF instead of a void*."

Use boost signals/slots, or any of the other existing and very useful signals and slots implementations. That way you can have end to end type safety.
[/quote]

I agree and disagree. Sometimes its more simple to have a universal eventinterface but at the same time typesafety. i managed to get something like that by using an empty eventclass that is subclassed by the correct event and passed to the event manager. the eventmanager then disides whic of it handlers matches the signature of the event and only passes it to those.

[quote name='BeerNutts' timestamp='1322864310' post='4889936']
[quote name='kindjie' timestamp='1322863474' post='4889929']
This smells like a design problem, so I'm not recommending you actually do this, but you could just make mData a void*.

Right now, it looks like you're just writing a clumsy wrapper for the C++ type system.


The class above is just an example, it's actually an Event system, where the client sends an event to a register listener. I was looking at adding a data component to the event.

I currently am using a void*, but I thought a template solution would've been more elegant solution, so I was trying to add it in with out redefining the class.
[/quote]

At no point during the invocation of an event system should you NOT know the exact type of the parameters.

A button click event handler will take exactly one type. The instant you introduce the magical void* into things you lose all type safety and introduce significant chance of bugs.

You invoke your event with a PointF, I catch the event and cast the void* I got to a PointI, and blamo bugo. Now I've got to waste MY time debugging and trying to figure out why my PointI has such wonky values, walking the stack trace, only to find out you're using a PointF... "Gee," I say, "it sure would have been nice if this event had just had the signature for a PointF instead of a void*."

Use boost signals/slots, or any of the other existing and very useful signals and slots implementations. That way you can have end to end type safety.
[/quote]

I'd never suggest to anyone to use void * as I do; but, fortunately, this isn't my 1st rodeo.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement