3

Is it possible to instead of have a virtual function have a virtual variable?

class B { virtual int fn(); /*virtual int val;*/ };
class D1: public B { virtual int fn() {return 0;}; 
/*int D1::val = 0;*/

class D2: public B { virtual int fn() {return 3;}; 
/*int D2::val = 3;*/

Right now i'm writing b->fn() because I have no idea how to have a virtual variable which I think would be more efficient (b->val). Is it possible in C++? Basically I want to have a const variable instead of a const function pointer in the vtable.

  • 2
    possible duplicate of [Why doesn't C++ have virtual variables?](http://stackoverflow.com/questions/3248255/why-doesnt-c-have-virtual-variables) – juanchopanza Apr 22 '14 at 20:21
  • 3
    If it's a variable what's point in making it virtual? just set it in child ctor – RiaD Apr 22 '14 at 20:22
  • 2
    you can add `val` as protected. I'm not sure what exactly you are up to though. It's a bad code smell. – Alexander Oh Apr 22 '14 at 20:22
  • @RiaD: Because I need it to be STATIC const and not an instance. –  Apr 22 '14 at 20:23
  • also, can you post an example of a code which compiles, and achieves the same *behaviour*? – Karoly Horvath Apr 22 '14 at 20:27
  • @Mat: I'm trying to do exactly whats shown. Access a const value based on the class type. In this case I use a virtual function which is a static function pointer. I want a static variable –  Apr 22 '14 at 20:27
  • 1
    @acidzombie24: "I'm trying to do exactly whats shown." - it doesn't compile, the code doesn't have meaningful semantics. Consequently, what you're saying doesn't make sense. – Karoly Horvath Apr 22 '14 at 20:28
  • @acidzombie24: I think it's fair to expect a question which can be decoded by further visitors without going through dozens of comments (and yes, I agree, so far that's the most clear description). just because we expect a clear description doesn't mean we are stupid ;) – Karoly Horvath Apr 22 '14 at 20:33
  • 1
    @KarolyHorvath: The comments are trying to find out the why. It doesn't matter WHY I am doing it. I'm saying EXACTLY WHAT I want to do. Put a const variable in the vtable ie virtual variable. –  Apr 22 '14 at 20:36
  • So, you want a type-specific value. If just uniqueness is enough you could use typeid, but `virtual int f(void)` overhead is going to be down around nothing in a modern architecture, particularly with the devirtualization work that's going on. – jthill Apr 22 '14 at 20:43
  • @acidzombie24: i'm pragmatic on this issue. clear questions rarely get comments asking for clarification (note: yes, it's better now). – Karoly Horvath Apr 22 '14 at 20:45
  • @KarolyHorvath: IS it more clear because I commented out the variable? Is that all I needed? Is the extra sentence important `Basically I want to have a const variable instead of a const function pointer in the vtable.` –  Apr 22 '14 at 20:47
  • both. and I think devirtualization isn't relevant here. – Karoly Horvath Apr 22 '14 at 20:59
  • @jthill Any idea if MS VC does devirtualization? I use MSVC and LLVM. I forgot about devirtualization but I like having clear and concise ways to state what I want –  Apr 22 '14 at 22:08
  • Look at the generated code. If anybody cares and there's no workaround, it'll at least eventually happen. – jthill Apr 22 '14 at 22:31

3 Answers3

8

NO, you cannot.

But instead, just make a virtual get/set.

Top-Master
  • 7,611
  • 5
  • 39
  • 71
elcuco
  • 8,948
  • 9
  • 47
  • 69
5

Complier will make an indirection/lookup for virtual methods. This only applies to methods (aka. member functions). This indirection/lookup is not applied in C++ to data members (what you called variables).

See following picture which may give a better graphical representation: http://www.yaldex.com/games-programming/FILES/05fig07.gif

So, provide access through [virtual] getter/setter.

okigan
  • 1,559
  • 2
  • 18
  • 33
  • 1
    The VTable pointer has function pointers (with various signatures). I was hoping instead of a function pointer I could use a function variable. Then I could give it a const value based on the type. I guess C++ doesn't support that which is too bad –  Apr 22 '14 at 20:30
  • Correct, C++ does not support that. Of top of my head, i think C# could be closest, but still because it has `properties` which looks like variable access, but (automatically) mapped to function access (so still not what are asking for). – okigan Apr 22 '14 at 20:36
0

Maybe this is something what you want:

#include <iostream>

class AbstractSomething
{
public:
    void add(int diff){ data() += diff; };
    int get() const { return data(); }
protected:
    virtual int & data() = 0;
    virtual int const & data() const = 0;
};

class ConcreteSomething
  : public AbstractSomething
{
public:
    ConcreteSomething() { m_data = 0; }
protected:
    virtual int & data() { return m_data; }
    virtual int const & data() const { return m_data; }
private:
    int m_data;
};

int main()
{
    ConcreteSomething c;
    c.add(7);
    c.add(3);
    std::cout << c.get() << std::endl;
    return 0;
}
Notinlist
  • 16,144
  • 10
  • 57
  • 99
  • Not really basically I want a static const value which is tied to the type –  Apr 22 '14 at 20:31
  • @user34537 that's funny I came here for exactly the same thing. I guess we've found something that can't be done in C++. – glades Jul 15 '21 at 08:08