|
-
March 7th, 2002, 02:08 PM
#1
A quick question on VTBL
Is there a way where i can call a function in
a class without using the name of the function.
class A
{
public :
int x
void print(){cout<<"Hello";}
};
A *a;
then using this pointer call print function
without using the name. I dont want to use
a->print();
I am in this situation beacuse i have the name of the function in database. using that name and the name of the class i want to call the function.
Please rate the article if it is of any use to you. This encourages me to reply more and more and more.
-
March 7th, 2002, 05:02 PM
#2
Re: A quick question on VTBL
That isn't a question of v-tables because print is not virtual in A.
However if you want you could do this:
class IPrint // interface implements printing
{
public:
virtual void print() = 0;
virtual ~IPrint();
};
Now you can have different instances of classes that derive from IPrint and override printing (in their own way).
However your "database" can store an array of IPrint pointers (possible shared-pointers), and not have to know the exact class that they point to.
The best things come to those who rate
-
March 8th, 2002, 12:23 AM
#3
Re: A quick question on VTBL
If the function is virtual then you can call it via table. Here is a small program to show this.
// call virtual function without using its name
#include <iostream>
using namespace std;
class Base {
public:
virtual void Fun1() {
cout << "Base::Fun1" << endl;
}
virtual void Fun2() {
cout << "Base::Fun2" << endl;
}
};
typedef void(*Fun)();
int main() {
Base obj;
// declare function pointer
Fun pFun = NULL;
// call first virtual function
pFun = (Fun)*((int*)*(int*)((int*)&obj+0)+0);
pFun();
// call second virtual function
pFun = (Fun)*((int*)*(int*)((int*)&obj+0)+1);
pFun();
return 0;
}
Hope it helps.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|