473,517 Members | 2,837 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Runtime overriding of virtual functions

Say you have a library with a header file that defines two classes:

class A
{
protected:
virtual void draw();
};

class B
{
public:
A *getWidget() { return m_a; }
private:
A *m_a;
};

Is there any way of creating an instance of class B such that when it
calls the virtual draw function of m_a it will call a user derived
version of draw? The two ways I can think of are making the m_a
variable public and setting it equal to a class derived from A or
somehow modifying the virtual function table of the pointer returned
by getWidget(). I assume these are both hacks that are dependent on
the behaviour of the compiler. Has anyone got this to work using
Visual C++?

Thanks
Jul 22 '05 #1
4 1536
Dim St Thomas wrote:
Say you have a library with a header file that defines two classes:

class A
{
protected:
virtual void draw();
};

class B
{
public:
A *getWidget() { return m_a; }
private:
A *m_a;
};

Is there any way of creating an instance of class B such that when it
calls the virtual draw function of m_a it will call a user derived
version of draw? The two ways I can think of are making the m_a
variable public and setting it equal to a class derived from A or
somehow modifying the virtual function table of the pointer returned
by getWidget(). I assume these are both hacks that are dependent on
the behaviour of the compiler. Has anyone got this to work using
Visual C++?

Thanks


If you had code like

B b;

...

b.getWidget()->draw();

the last line will effectively translate to b.m_a->draw(). Since draw() is
virtual, it will invoke whichever method is the right one for the object
that b.m_a points to. This object can be of any type derived from A.

However your class definitions above seem not to allow for assigning *any*
value to b.m_a. How do you intend to initialize this pointer anyway? You
certainly want this pointer to point somewhere meaningfull? Once it does, I
do not really see a problem with the statement:

b.getWidget()->draw();

What is it that I am missing?
Best

Kai-Uwe Bux
Jul 22 '05 #2
I am replying to my own message as the reply below has not yet
appeared on Google, so I copied from my free newsfeed.
Kai-Uwe Bux wrote:
Dim St Thomas wrote:

Say you have a library with a header file that defines two classes:

class A
{
protected:
virtual void draw();
};

class B
{
public:
A *getWidget() { return m_a; }
private:
A *m_a;
};

Is there any way of creating an instance of class B such that when itcalls the virtual draw function of m_a it will call a user derived
version of draw? The two ways I can think of are making the m_a
variable public and setting it equal to a class derived from A or
somehow modifying the virtual function table of the pointer returned
by getWidget(). I assume these are both hacks that are dependent on
the behaviour of the compiler. Has anyone got this to work using
Visual C++?

Thanks

If you had code like

B b;

...

b.getWidget()->draw();

the last line will effectively translate to b.m_a->draw(). Since draw() is
virtual, it will invoke whichever method is the right one for the object
that b.m_a points to. This object can be of any type derived from A.

However your class definitions above seem not to allow for assigning *any*
value to b.m_a. How do you intend to initialize this pointer anyway? You
certainly want this pointer to point somewhere meaningfull? Once it does, I
do not really see a problem with the statement:

b.getWidget()->draw();

What is it that I am missing?


The header files are from a library that I don't have access to the
source code. The B class creates an instance of A internally (probably
during the constructor). Ideally there would be a setWidget function,
but as there isn't I have to find some way of hacking it to do what I
want.
Jul 22 '05 #3
Dim St Thomas wrote:
I am replying to my own message as the reply below has not yet
appeared on Google, so I copied from my free newsfeed.
Kai-Uwe Bux wrote:
Dim St Thomas wrote:

Say you have a library with a header file that defines two classes:

class A
{
protected:
virtual void draw();
};

class B
{
public:
A *getWidget() { return m_a; }
private:
A *m_a;
};

Is there any way of creating an instance of class B such that when itcalls the virtual draw function of m_a it will call a user derived
version of draw? The two ways I can think of are making the m_a
variable public and setting it equal to a class derived from A or
somehow modifying the virtual function table of the pointer returned
by getWidget(). I assume these are both hacks that are dependent on
the behaviour of the compiler. Has anyone got this to work using
Visual C++?

Thanks

If you had code like

B b;

...

b.getWidget()->draw();

the last line will effectively translate to b.m_a->draw(). Since draw()
is virtual, it will invoke whichever method is the right one for the
object that b.m_a points to. This object can be of any type derived from
A.

However your class definitions above seem not to allow for assigning
*any* value to b.m_a. How do you intend to initialize this pointer
anyway? You certainly want this pointer to point somewhere meaningfull?
Once it does, I do not really see a problem with the statement:

b.getWidget()->draw();

What is it that I am missing?


The header files are from a library that I don't have access to the
source code. The B class creates an instance of A internally (probably
during the constructor). Ideally there would be a setWidget function,
but as there isn't I have to find some way of hacking it to do what I
want.


Sounds like you want to hack a setWidget() method from the outside. One
idea would be to do something like

void setWidget( B& b, A* a ) {
char * b_data = (char*) &b;
// and now do some manipulations to b_data[...] to
// set the field b.m_a
}

For instance, on my machine the following program
#include <iostream>

class B{
private:

unsigned int l;

public:

unsigned int get( void ) {
return( l );
}

}; // B

void set ( B& b, unsigned int k ) {
char* b_data = (char*) &b;
char* k_data = (char*) &k;
for ( int i = 0; i < 4; ++i ) {
b_data[i] = k_data[i];
}
}

int main ( void ) {
B b;
std::cout << b.get() << std::endl;
set( b, 6 );
std::cout << b.get() << std::endl;
}

prints

3221224484
6

I would think, however, that the setWidget() method was left out for a
reason and that you are very likely to break all sorts of stuff inside the
object b.
Hope this helps

Kai-Uwe Bux
Jul 22 '05 #4
Kai-Uwe Bux <jk********@gmx.net> wrote in message news:<ca**********@news01.cit.cornell.edu>...
Dim St Thomas wrote:
Kai-Uwe Bux wrote:
Dim St Thomas wrote:
Say you have a library with a header file that defines two classes:

class A
{
protected:
virtual void draw();
};

class B
{
public:
A *getWidget() { return m_a; }
private:
A *m_a;
};

Is there any way of creating an instance of class B such that when itcalls the virtual draw function of m_a it will call a user derived
version of draw? The two ways I can think of are making the m_a
variable public and setting it equal to a class derived from A or
somehow modifying the virtual function table of the pointer returned
by getWidget(). I assume these are both hacks that are dependent on
the behaviour of the compiler. Has anyone got this to work using
Visual C++? [snip]
However your class definitions above seem not to allow for assigning
*any* value to b.m_a. How do you intend to initialize this pointer
anyway? You certainly want this pointer to point somewhere meaningfull?
Once it does, I do not really see a problem with the statement:

b.getWidget()->draw();

What is it that I am missing?
The header files are from a library that I don't have access to the
source code. The B class creates an instance of A internally (probably
during the constructor). Ideally there would be a setWidget function,
but as there isn't I have to find some way of hacking it to do what I
want.


Sounds like you want to hack a setWidget() method from the outside. One
idea would be to do something like

void setWidget( B& b, A* a ) {
char * b_data = (char*) &b;
// and now do some manipulations to b_data[...] to
// set the field b.m_a
}


Thanks for your replies.
This is the same as just changing the header file to make the variable
public, which seems to work ok in a test program.
I would think, however, that the setWidget() method was left out for a
reason and that you are very likely to break all sorts of stuff inside the
object b.


This is why I would rather modify the virtual function table of the
pointer returned by getWidget(). I found this post which seems to be
what I want:

<http://groups.google.com/groups?selm=O4qMx5M5BHA.1660%40tkmsftngp05>
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2711
by: Hongzheng Wang | last post by:
Hi, I have a problem about the overriding of private methods of base class. That is, if a method f() of base class is private, can the derived class overriding f() be overriding? For example, class base {
3
2172
by: CoolPint | last post by:
I read that the return type has to be exactly same for a virtual function to be overriden. While testing something, I discovered something I cannot understand. I cannot understand why the code below compiles. I see that it won't compile if I have "virtual char & vtest() { }" in Base class and have "virtual const char & vtest() { }" in...
1
2361
by: Xiangliang Meng | last post by:
Hi, all. When reading C++ books, I'm alway confused by those terms "redefining functions", "overloading functions" and "overriding functions". Please give me some comments on those terms. Thanks. If giving more strategy hehind them, more helpful. Best Regards,
2
2429
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine. i would forget overriding it but i have to do it because its a coursework. here is a simple version of the class #include <iostream> #include...
11
8889
by: iceColdFire | last post by:
Hi, What is the Diff btwn Function overloading and overriding thanks, a.a.cpp
15
23986
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and overriding? 2. When is one preferable to use as opposed to the other? 3. How are virtual functions related to this topic (overloading/overriding) - a...
3
8632
by: David Scarlett | last post by:
Hi all, I've got a question regarding overriding const member functions with non-const functions. Let's say I've got a base class defined as follows: /*******************/ class Foo { public:
8
3208
by: yashwant pinge | last post by:
#include<iostream> using namespace std; class base { public: void display() { } };
3
2011
by: Rick | last post by:
One of the rules I have recommended for our new coding standard is as follows: "When overriding a base class virtual function, all overloads of that base class virtual function should also be overridden. Otherwise, the overloads of the overridden function in the base class will not be visible from the derived class." In other words...
0
7295
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7197
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7556
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5120
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4786
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3280
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1641
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
504
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.