i want to know how to access virtual table for a class this is my attempt to do so ........

#include<iostream>
using namespace std;
class sample
{
    public:
        virtual void  fun(int q)
        {
            cout<<"fun"<<endl;
            }
        
        void g()
        {
            int *p=(int *)this;
            p=(int *)*p;
            p=(int *)*p;
            void ( sample::*pfun)(int);
            pfun=(void(sample::*)(int))p;
           // pfun=(void (sample::*)(int))p;
            (*pfun)(25);
                
         }
         void abc()
       {
        cout<<"abc"<<endl;
        }
         
        
    };
    int main()
{
       sample s;
        s.g();
}

here i am getting error while casting the in the line
pfun=(void(sample::*)(int))p;

Recommended Answers

All 6 Replies

the scope resolution operator have become smiley !!!!!

commented: The scope operator would be the scope operator if you fsking learnt how to use CODE TAGS!!!!!!! 28 post, and you're still dumber than a box of rocks -7

If you haven't noticed, this is the C forum. You should post your question in the C++ forum. Perhaps it's better if you wait for a moderator to move your question for you to the right forum.

sorry for posting in the wrong place...
anyways i posted same thing on c++ forum so pls delete this thread from here

Function definition of g() should be:

void g(){
 int *p=(int *)this;
 typedef void (*pfun)(int);
 pfun p1=(pfun)*(int*)*(int*)p; // value of vptr.
 (p1)(25);
}

can same thing be achieved without using "typedef"

Yes,

((void (*)(int))*(int*)*(int*)p)(20);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.