Accessing Baseclass virtual function from derived

N

NewToCPP

I am trying to access a base class's virtual function from the drived
class function. It is complaining that the function is not static. Can
anyone tell me what is the problem in the below given program.

class Base
{
private:
unsigned short abc;

public:
Base ();
virtual ~Base ();
virtual int myVirt (char *, const EnumMode);
};

int Base::myVirt (char *buffer, const EnumMode mode)
{
int offset = 0;


Base* pFrame = (Base *)(void *)(buffer + offset);



return offset + 2;

}

class Derived
{
private:
unsigned long dAbc;

public:
Derived ();
virtual ~Derived ();
virtual int myVirt (char *, const EnumMode);

};



int Derived::myVirt (char *buffer, const EnumMode mode)
{
int offset = 0;
offset = Base::myVirt (buffer, mode);


return offset + 4;

}










c:\testFile.h(64) : error C2352: 'Base::myVirt' : illegal call of
non-static member function
c:\testFile.h(31) : see declaration of 'myVirt'
 
M

Marcus Kwok

NewToCPP said:
I am trying to access a base class's virtual function from the drived
class function. It is complaining that the function is not static. Can
anyone tell me what is the problem in the below given program.

class Base
{
private:
unsigned short abc;

public:
Base ();
virtual ~Base ();
virtual int myVirt (char *, const EnumMode);
};

int Base::myVirt (char *buffer, const EnumMode mode)
{
int offset = 0;


Base* pFrame = (Base *)(void *)(buffer + offset);



return offset + 2;

}

class Derived

You need to actually derive Derived from Base:

class Derived : public Base
 
T

Thomas Tutone

NewToCPP said:
I am trying to access a base class's virtual function from the drived
class function. It is complaining that the function is not static. Can
anyone tell me what is the problem in the below given program.

class Base
{
private:
unsigned short abc;

public:
Base ();
virtual ~Base ();
virtual int myVirt (char *, const EnumMode);
};

int Base::myVirt (char *buffer, const EnumMode mode)
{
int offset = 0;


Base* pFrame = (Base *)(void *)(buffer + offset);



return offset + 2;

}

class Derived

Change the above line to:

class Derived : public Base
{
private:
unsigned long dAbc;

public:
Derived ();
virtual ~Derived ();
virtual int myVirt (char *, const EnumMode);

};



int Derived::myVirt (char *buffer, const EnumMode mode)
{
int offset = 0;
offset = Base::myVirt (buffer, mode);


return offset + 4;

}


Best regards,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top