Hi ,
         I encountered an issue like ,  I have a base class with one virtual 
function  and one derived class for it. I have overridden the virtual method. I 
have a function which will accept an array of base class objects. and I am 
printing the virtual method in this function. I have passed address of derived 
class array objects. I could access non - virtual functions of base class but 
couldn't access the virtual methods. I thought like there could be an issue 
with size like derived could be larger than base and while incrementing the 
base pointer , I am getting issue like. But after some r&d I figured out that 
is not the issue. I am copy pasting the sample code snippet . Could any one of 
you please assist.



#include "stdafx.h"
typedef void (*myfun)();

class polybase
{
public:
    void disp()
    {
        cout<<"I am in disp of base"<<endl;
    }
    virtual void show()
    {
        cout<<"I $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$sam in base"<<endl;
    }
};
class polyder:public polybase
{
   
public:
    void show()
    {
        cout<<"=======================================I am in der"<<endl;
    }
};

void disp(polybase **x)
{
    myfun objfun;
objfun = (myfun )*(int *)*(int *)(&x[0]); //(Just to check whether I could 
access the 
                                                       //(virtual methods and I 
am done.)
    objfun();
x[0]->disp();
x[1]->disp(); // I could access disp of second object in array .. 
x[0]->show();  // I am getting access violation error here.
x[1]->show();
cout<<endl;
cout<<&x[0]<<endl;
cout<<"size is"<<sizeof(x[0])<<endl;

}
void func(polybase *x)
{
    x->show();
    x->disp();
}
int main(int argc, char* argv[])
{
    polyder obj[2];
    //     polybase obj[2];
    cout<<"The size is "<<sizeof(obj[0])<<endl;
    obj[0].show();
    myfun objfun;
        
    objfun = (myfun )*(int *)*(int *)(&obj[0]);
    objfun();
    
    disp((polybase **)&obj);

    func((polybase *)&obj1);  // If I pass a single object instead of array , I 
could 
                                         //access virtual functions properly. )
    polybase *x = &obj1;
    x->show();
    
    
    return 0;
}


Thanks & Regards,
Gopi.k
Software Engg.
PAY PAL,
Chennai.
9884609596

       
---------------------------------
Building a website is a piece of cake. 
Yahoo! Small Business gives you all the tools to get online.

[Non-text portions of this message have been removed]

Reply via email to