Thread: Virtual variables or constants?

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Virtual variables or constants?

    Hi! Is it possible to have virtual variables in a class (they would be stored in the v-table)? Or virtual constants? I know that I could just have a virtual function that returned the variable, but it feels bad to have a function to return the variable when I could just have a variable (if the language would support it!). The reason I'm asking is because I want to index my classes, so that I can easily use the class type as one of two entry keys to a two-dimensional array.
    Come on, you can do it! b( ~_')

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, there is no such feature. But if you want to map keys to values, then why not use a map?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I don't think it makes any sense to refer to virtual variables unless they are going to be of different types. Which if they are going to be of different types, it's hard to see how you are going to incorporate that in a meaningful way -- eg, only if you had a function in the base class which operated on the variable which is of one class and you want to change that type in the derived class, which I doubt this will work anyway. Certainly this seems like a non-sequitor:

    The reason I'm asking is because I want to index my classes, so that I can easily use the class type as one of two entry keys to a two-dimensional array.
    Or maybe I misunderstand you?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by Elysia View Post
    No, there is no such feature. But if you want to map keys to values, then why not use a map?
    Because I can use a virtual function to do the job, which I believe is faster. But I don't know. Using a map was not a bad idea, though.

    Quote Originally Posted by MK27 View Post
    Certainly this seems like a non-sequitor:
    Quote Originally Posted by MK27 View Post
    Or maybe I misunderstand you?
    I can give you an example of what I mean:

    A container class, cntr, contains a pointer to one of five subclasses of the class sup, which are called c1, c2, c3, c4 and c5. For these five classes c1-c5, the plus operator is defined and returns a cntr for all 25 combinations of them (two arguments, 5^2 = 25); for example is the function operator+(const c1&, const c2&) defined. When two objects a and b, each of the cntr class, are added to each other with the + operator, the result should be the sum of the two objects that are pointed to by the pointers in a and b respectively. Hence, it must be possible to determine which plus operator to use, since there are 25 of them. Letting the v-table choose for you, by using a virtual plus operator, is not sufficient since there are only 5 v-tables which means that only 5 plus operators could be addressed using that method.

    However, if the v-table for each subclass c1-c5 contained an index between 0 and 4, corresponding to the class, the index for both of the objects' classes could be looked up and a key to an array containing pointers to all the plus operators could be calculated. The key could then be used to acquire the appropriate function pointer and call the plus operator.
    Last edited by TriKri; 06-07-2010 at 09:16 AM.
    Come on, you can do it! b( ~_')

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hmmm. Okay. You might as well use method calls -- gettype() or something then. I'm always uncomfortable with one line functions that just return a static value too, but realistically it's probably no big deal.

    I totally understand why you would want to ask if something else is possible.

    BUT, just reading thu that again, why not just have one variable inherited from the base class:
    Code:
    int type;
    which would be set to 0-4 in the constructor for each derived class? Then in your + operator function you check the value of "type" for each submitted object and deal with it from there (probably by passing them on to a more specific/appropriate function and returning that).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Thumbs up

    Quote Originally Posted by MK27 View Post
    BUT, just reading thu that again, why not just have one variable inherited from the base class which would be set to 0-4 in the constructor for each derived class?
    Hm, that's actually pretty smart. It would make each object 4 bytes bigger, but then since the type of the object would be known thanks to the variable, I won't need to have any v-table and the objects won't need to have any pointers to the v-tables, so it evens out. Simple and clean solution. I guess this is how you can do it in C if you want to have something close to virtual functions, isn't it?

    Quote Originally Posted by Elysia View Post
    No, there is no such feature.
    Good to know at least

    Thanks to both of you!
    Come on, you can do it! b( ~_')

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by TriKri View Post
    Simple and clean solution. I guess this is how you can do it in C if you want to have something close to virtual functions, isn't it?
    I suppose that's essentially true. I don't really like the function pointer method of simulating OO in C, I'd rather just write separate functions dedicated to specific typedefs/structs (which C++ classes are a means of organizing code and exploiting the possibilities which arise from that). But you could include a (sub)type indicator and a void pointer containing anything* (sorted by the type indicator). I've never done anything like that, but I think I've seen it done. I suppose it would simulate inheritance, but thinking in terms of "simulation" and OOP terms is not really necessary (it still makes sense).

    * lack of a useful void pointer is the absolute worst thing about C++, that was the single dumbest idea in all programming history IMO. I cannot see how the added "type safety" is worth all the work-arounds and templating needed to make up for that. If I could lock Bjlarney S. up in a room, I would not let him out until he agreed to take it back
    Last edited by MK27; 06-07-2010 at 11:29 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    * lack of a useful void pointer is the absolute worst thing about C++, that was the single dumbest idea in all programming history IMO. I cannot see how the added "type safety" is worth all the work-arounds and templating needed to make up for that. If I could lock Bjlarney S. up in a room, I would not let him out until he agreed to take it back
    Good joke
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by laserlight View Post
    Good joke
    I reaaaaaaally hope that it was a joke.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    TriKri, what you want is commonly referred to as "multimethods" and "multiple dispatch" (or just "binary dispatch" in the two-argument case) in the literature. Searching for those terms might give you some implementation ideas.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    I read the Wikipedia article about multiple dispatch; it mentioned the use of visitor patterns, which actually has crossed my mind before. However, I think that the code easily would get messy if I used this kind of help functions, since every operator in every class would need to have an extra help function. So I'd rather use the method with a lookup table containing to get a function pointer, which I also believe is the quickest method. The function would then have to reinterpret_cast the arguments (since all functions would take arguments from the base class).
    Come on, you can do it! b( ~_')

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    If you have to reinterpret_cast, you've done something wrong. Nothing less safe than a static_cast should ever be necessary in a multimethod implementation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    I meant that I was going to use static_cast. Thank you.
    Come on, you can do it! b( ~_')

  14. #14
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Using an int type is fastest, but the problem is that it is more easy to mess up. If you want to go on that path make an enumeration with all classes so you wont have to remember 2 = apple.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-28-2009, 09:25 AM
  2. Platform game gravity problem
    By Akkernight in forum Game Programming
    Replies: 33
    Last Post: 02-22-2009, 01:10 PM
  3. Virtual Box
    By ssharish2005 in forum Tech Board
    Replies: 3
    Last Post: 02-12-2009, 05:08 AM
  4. Declaring instances of different implementations
    By dwks in forum C++ Programming
    Replies: 8
    Last Post: 07-16-2008, 11:43 AM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

Tags for this Thread