Author Topic: modifying vtable virtual function pointers?  (Read 13154 times)

0 Members and 1 Guest are viewing this topic.

Offline drlava

  • Sr. Member
  • ****
  • Posts: 314
  • Milliwatts: 18
    • View Profile
modifying vtable virtual function pointers?
« on: April 10, 2009, 11:48:39 pm »
I'm new to the world of virtual functions, and the web tutorials I have seen lay out how to create child classes of a main class whose functions over-ride the parent classes virtual functions.  These examples are simple in that the sub-class is created with a 'new' from outside the parent class code.  What I'd like to be able to do is have the parent class handle loading and unloading of its own subclasses which replace its own virtual functions that it publicizes. 

Edit: I figured out a way around this, but you can still answer if you like :)
« Last Edit: April 11, 2009, 12:28:18 am by drlava »

Offline James

  • Administrator
  • Hero Member
  • *****
  • Posts: 2130
  • Milliwatts: 47
  • Gender: Male
    • View Profile
    • LaserBoy !!!
Re: modifying vtable virtual function pointers?
« Reply #1 on: April 11, 2009, 01:22:56 am »
I'm not sure what you are asking here. I looks like you might be talking about parent to child as a container to object relationship. That is not really what a virtual function is about.

Can you show me the example you found and the issue you had in code?

There are some examples of virtual functions in LaserBoy, when the ez_uail_frame class calls ez_uail_segment functions and then calls the validate() function.

James.  :)
« Last Edit: April 11, 2009, 01:26:51 am by James »
LaserBoy is Sofa King Cool!
But it will never be Alpha King Done!

Offline drlava

  • Sr. Member
  • ****
  • Posts: 314
  • Milliwatts: 18
    • View Profile
Re: modifying vtable virtual function pointers?
« Reply #2 on: April 11, 2009, 11:22:34 am »
Originally I was thinking something like this:

Code: [Select]
class parentclass {
public:
     parentclass(){};
     virtual ~parentclass(){};
     int changeparentclass(int mode){
          switch (mode){
              case 1:
                   -something to load child1 and destroy previous child if necessary-
                   break;
              case 2:
                   -something to load child2 and destroy previous child if necessary-
                   break;
          }
           if (itworked)
               return 0;
          return 1;
    }

     virtual int func(){return -1;}
};


class childclass1 : parentclass {
public:
    childclass1(){};
    ~childclass1(){};
    int func(){printf("this is func from child1"); return 1;}
};

class childclass2 : parentclass {
public:
    childclass2(){};
    ~childclass2(){};
    int func(){printf("this is func from child2"); return 2;}
};


void main(void)
{
    parentclass test;
    test.func(); //<-returns -1
    test.changeparentclass(1);
    test.func(); //<-returns 1 and prints 'this is func from child1'
    test.changeparentclass(2);
    test.func(); //<-returns 2 and prints 'this is func from child2'
}

 :-\

« Last Edit: April 11, 2009, 11:24:20 am by drlava »

Agent C

  • Guest
Re: modifying vtable virtual function pointers?
« Reply #3 on: April 11, 2009, 12:54:40 pm »
Would this be for compiling new classes on the fly and loading them into the running for text input of functions, or maybe loading an already compiled class as a plugin?

Offline drlava

  • Sr. Member
  • ****
  • Posts: 314
  • Milliwatts: 18
    • View Profile
Re: modifying vtable virtual function pointers?
« Reply #4 on: April 11, 2009, 01:40:02 pm »
yes it was for handling 'plugins'.  What I instead decided to do was to have the 'parent' class return a pointer to a new class that it creates with what used to be called 'changeparentclass' in the example.

Offline James

  • Administrator
  • Hero Member
  • *****
  • Posts: 2130
  • Milliwatts: 47
  • Gender: Male
    • View Profile
    • LaserBoy !!!
Re: modifying vtable virtual function pointers?
« Reply #5 on: April 11, 2009, 03:34:06 pm »
I think you might be confusing the ideas of inheritance and containment.

The difference is sometimes called is-a vs. has-a.

When you inherit a class into a new class, the new class is-a class of it's ancestoral type.

If you declair objects of type class_2 within the structure of a class_1, that class_1 has-a class_2. That is containment.

An example of inheritance in LaserBoy is the relationship that exists between a ez_uail_segment and an ez_uail_frame. Both of these concepts have the same core information. They both contain a list of ordered points that make up a laser drawing. But a segment is more primative and therefore can be used with greater flexibility. A frame is a much more defined object.

A frame is-a segment with additional features.

There are a few functions that can be called on the segment type that effect information that is only found in the frame. So if the function is called on a segment, that's fine. But if the same function gets called on a frame object, the frame needs to know. So a virtual function is defined within the frame class that calls the segment function of the same name and does some other stuff the frame needs to do because of the change in data that happened to the segment that is actually its own core. That is an example of a virtual function.

One of the cool tricks that I use in LaserBoy is "containment via inheritance"!

It looks so simple and eligant, but there is a lot going on here!

If I first create a class that is to be an element of an array of things; like an ez_ual_point. I can then use the STL vector class template to make a container of them. But, I can also inherit that container class of vector<ez_uail_point> into a class with a better name and all kinds of added custom functionality; ez_uail_segment..... in one line of code!

Code: [Select]
//############################################################################
class ez_uail_segment : public vector<ez_uail_point>
{
public: .....



The type vector<ez_uail_point> never gets an object or reference name of its own. It is simply inherited directly into a new class type. So ez_ual_segment is-a vector<ez_uail_point> with added features. This makes it possible to call all of the functions that are part of the STL vector class upon objects of this type.



I also use multiple inheritance which you can only do in C++!





Code: [Select]
//############################################################################
class ez_uail_point : public ez_3D_short, public ez_color
{
public: .....




James.  :)
« Last Edit: April 11, 2009, 04:11:14 pm by James »
LaserBoy is Sofa King Cool!
But it will never be Alpha King Done!

Offline drlava

  • Sr. Member
  • ****
  • Posts: 314
  • Milliwatts: 18
    • View Profile
Re: modifying vtable virtual function pointers?
« Reply #6 on: April 13, 2009, 03:56:33 pm »
bingo.  there is no reason to try to re-define virtual functions if instead I use a class that has (not is) the member class that I'm interested in changing. 
thanks :)

 

SMF spam blocked by CleanTalk
SimplePortal 2.3.7 © 2008-2024, SimplePortal