last day (14 days later) » 

12:20 PM
0
Q: A C++ idiom for per-class data, accessible without needing virtual getter methods

einpoklum(This question is related to reflection, but isn't really about reflection) I have this hierarchy of classes (say class A and class B : public A), and in addition to instance-specific data, I'd like to have class-specific data shared by all instances. For example, suppose I want to have a FunnyC...

 
@Simple: The getters for class-specific data should be static, yes. Corrected.
@KarolyHorvath: The problem is that a static method of the base class needs to have different behavior based on what the calling instance' actual class is.
@GermánDiago: The getter functions should exist at compile-time.
@Walter: You're right, see edit.
@anatolyg: IIANM, if we have A* a1 = new B(), wouldn't typeid(*A).name() give me the value for A rather than B?
 
Why the following simple solution doesn't satisfy your conditions?
 
@Constructor: Here's why...
 
@einpoklum So you want polymorphic solution working at run-time without virtual functions, don't you?
 
@Constructor: No, I didn't say you can't use virtual functions. The getters need to be static, that's all.
 
12:20 PM
@einpoklum Why do you want them to be static?
 
@Constructor: You know what? If they're not static, but do not use any data members of the instance (i.e. they only use the vtable), then maybe I can tolerate that. If you have something in mind.
 
@einpoklum Of course I have. What conditions are violated here?
 
@Constructor: In your sample code, you're not accessing a and b via A*s.
 
@einpoklum What? Both a and b are A*!
 
@Constructor: Sorry! I thought you linked to the same place as before.
 
12:22 PM
Are you sure my solution is what you want?
 
Ummm... let's say it's what I might end up having to live with.
 
@einpoklum What place do you mean?
 
You posted a link to some code before, then again further below
 
@einpoklum But it is an obvious solution!
 
Which is why it's not really what I want...
I want to implement the getter just once (even if it's not static)
and allow subclasses to just have a static field.
If you make your suggestion an answer I promise an upvote...
 
12:25 PM
@einpoklum Why do you want static fields?
 
Oh, because the behavior is the getter behavior is the same for all subclasses: It's "return a certain class-specific constant value".
It shouldn't really be a virtual function
The only thing that's subclass-specific is the constant value
I want the code to reflect that fact.
 
@einpoklum If you want run-time polymorphism it should.
 
it should what?
 
@einpoklum It should be a virtual function if you want run-time polymorphism.
 
And no, I don't really want that kind of run-time polymorphism - I want the same logic, but with class-specifc data.
So certainly not a virtual function...
but like I said,
I might just have to live with your suggestion...
 
12:30 PM
@einpoklum You want polymorphic behavior at a run-time. A usual way to get it in C++ is to use virtual functions.
 
Yes, I know that :-)
 
@einpoklum So?...
 
So, I was hoping to do something that's not the usual way, since the kind of polymorphic behavior I'm interested in is very limited.
Think of typeid(obj).name()
it's polymorphic,
but nobody's implementing any virtual methods (that I know of).
 
@einpoklum typeid is a bad example.
 
Well, what can I say.... that's the kind of thing I want to achieve. Duff's device is ugly too, and still it's valid code :-)
 

last day (14 days later) »