Variant and void*

Hi Jules,

I was wondering if there is a reason why var do not include the generic pointer type void * ?
I was thinking of storing a pointer inside the properties of a component but didn’t found a way to do it.


I prefer to avoid unstructured data if possible these days. It’s unsafe, hacky, old fashioned… Though you could derive a class from DynamicObject and put that inside a var to hold your object.

But when you start trying to attach void* pointers to generic components, that definitely sets off “bad design” alarm bells in my head! I’m sure there are situations where it might be the only efficient way to do something, but it definitely sounds like something to be ashamed of!

Well i would just use it to tag a component in order to check if it references the right object.
As the object is in my audio engine I don’t want to inherit it from a juce class.

It’s never dynamic casted so I don’t see it as a bad design.
Just like a quick MD5 on the object.

your call !

I’m fine with var with int64 otherwise :smiley:

Is this for your table cells components? Surely you know what the class of these components is, so can’t you just cast them to that class and ask them whether they reference the object or not?

Except that I have like 20 columns with a different component for each of those so it would be a pain to have a cast for each of those to their respected class in refreshComponentForCell :slight_smile:

So make them all implement a common base class, e.g.

class ComponentThatRepresentsAnObject { virtual bool representsObject (MyObject* o) = 0; };

Except that maybe you don’t want to have 30 custom class for the sake of readability and that some are created trough a factory.
void * is not always dirty, It’s only dirty when you try to use for type safe stuff :slight_smile:

It wouldn’t require 30 custom classes, would it? Assuming that these components are already custom classes, you’d only have to add a base class.

Anyway, why not avoid pointers altogether, and just store the row and column numbers of the cell in the component’s properties?

Or if you really want a dirty hack, turn the pointer into a hex string and store that?

Fair enough.

Still I think the addition of int64 might be useful if you want to be pedantic.


Yes, I do agree with the int64 suggestion!