Advertisement

virtual variables

Started by September 30, 2000 09:42 AM
4 comments, last by SkatMan 24 years, 3 months ago
Hi. I''d like to know if there''s a way to declare a virtual variable whose type can be overriden in the subclasses. What I want to do is a class of a double linked list that inherits a class of a regular linked list. So the double linked list item inherits a regular linked list item (I can do that), but then the double linked list class should inherit the regular linked list''s class and change the type of ''head''. How can I do that? Here is an example. class _LinkedListItem { int* next; int data; ... } class _DoubleLinkedListItem : public _LinkedListItem { int* prev; ... } class _LinkedList { _LinkedListItem* Head; ... } class _DoubleLinkedList { how should i define the head here? _DoubleLinkedListItem* tail; }
Afaik, no.

Just add a new variable in the derived class for that case. It wouldn''t be possible to code a function in your base class which takes a variable whose type isn''t clear anyway.

But you here''s another solution:
    class CValue {  virtual void Get(void *var) = NULL;  virtual void Set(void *var) = NULL;};//class CRegistry {  CValue m_CValue;  CValue GetValue(char *pszName);  void PutValue(char *pszName, CValue Value);};//class CInt {  int m_iValue;  inline void Get(void *var) {    *var = m_iValue;  }  inline void Set(void *var) {    m_iValue = *var;  }};//class CFloat {  int m_fValue;  inline void Get(void *var) {    *var = m_fValue;  }  inline void Set(void *var) {    m_fValue = *var;  }};    


Just an idea.
But there should be plenty of other ways.

-Markus-
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
Advertisement
I recommened you look up a wonderful little thing called a Template. You can make a class who has members who''s types are known until the class is instantiated. They''re great.

Morbo
Cygon, Morbo, yes, but I want the double linked list class to inherit the regular linked list class because if I have a certain algorithm that works with a regular linked list, I want it to be able to work with a double linked list too.
(I want to be able to pass the double linked list as an argument in a function that takes a regular linked list).

If you have another solution for this please tell me about it.

Thanks,

Oren.
        class _LinkedListItem{  int* next;  int data;  ...}class _DoubleLinkedListItem : public _LinkedListItem{  int* prev;  ...}class _LinkedList{  _LinkedListItem* Head;  virtual somefunc1();  virtual somefunc2();  ...}class _DoubleLinkedList : public _LinkedList{  _DoubleLinkedListItem* tail;  virtual somefunc1(); // redefine the appropriate functions as needed...}     



Good ole' polymorphism...


Regards,
Jumpster

Semper Fi

Edited by - Jumpster on October 2, 2000 9:01:45 PM
Regards,JumpsterSemper Fi
ok

but now my head is a regular linked list pointer.

let''s say i want to define a ''Current Item Pointer''.

What type will it be?

ah i know, i''ll make it a double linked list pointer.

the head will be a pointer to a pointer and everything will be good ya shipud.

This topic is closed to new replies.

Advertisement