UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C++
  • » clearing virt.func.table?

#1 2007-02-05 10:25 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: clearing virt.func.table?

#include <iostream>
#include <string.h>

class ITest
{
public:
	virtual int Get()=0;
	virtual void Set(int nLen)=0;
};


class Child:Public ITest
{
	int m_nParam;
public:
	int Get()
	{
		cout << "get " << sizeof(*this) <<endl;
		return m_nParam;
	}
	void Set(int nParam)
	{
		m_nParam	= nParam;
	}
};

int main()
{
	Child c;
	//bzero(&c,sizeof(c));			clearing virtual functions table?
	Child & rC=c;
	Child * pC;
	pC	= &c;
	cout << "object" << endl;
	c.Get();
	cout << "reference" << endl;
	rC.Get();
	cout << "pointer" << endl;
	pC->Get();
	return 0;
}


if bzero is not commented I get segmentation fault.

Offline

#2 2007-02-05 05:17 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: clearing virt.func.table?

The first problem is that the C++ standard does not specify how the v-table is implemented... so there is no standard way for you to 'zero' it out...

The second problem is that the C++ standard does not specify how the v-table is implemented... ( lol ) so there is no standard way for you to 'set' / assign a new function to v-table once you zero it out...

Last...

What in the world are you trying to do - lol ?!


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#3 2007-02-06 07:57 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: clearing virt.func.table?

Nothing,but I spent almost half an hour to find out the problem.Of course the code was much more complex.In the end I decided,that when using bzero, I clear vf table,so when I call from a pointer or reference a method of zeroed object(that has vf table), I get segmentation fault.However if i call a method directly from object,all is ok.So Idecided to ask somebody to explain me that mexanizm.

Offline

  • Index
  • » C++
  • » clearing virt.func.table?

Board footer

Powered by FluxBB