Archived

This topic is now archived and is closed to further replies.

EvilCrap

is-a ?

Recommended Posts

i was messing around with inheritance, and i found that is-a is false! (to some extent) if your base object has functions that work with the this pointer, then those functions only account for the sizeof and address of the base, not the child classes that may use base for concrete inheritance. does that really seem like an is-a thing? i discovered it in this example, where i try to have a base class with a binary copy routine.
  
int fsize = 0;
char* fdata = 0;
#define safedeletea(x) if(x) delete[] x; x = NULL;

class A{
	 virtual void getBinData(int& size, void* &begin) { size = sizeof(*this); begin = this;}
	int _a, _b;
public:
	void save(){
		int size; void* begin;
		getBinData(size, begin);
		safedeletea(fdata);
		fdata = new char[size];
		memcpy(fdata,begin, size);
	}
	void load()
	{
		int size; void* begin;
		getBinData(size, begin);
		memcpy(begin, fdata, size);
	}
	virtual void print() = 0;
};

class C: public A{
	//void getBinData(int& size, void* &begin) { size = sizeof(*this); begin = this;}

	int _c;
public:
	void print(){ cout << _c << endl;}
	void set(int c) { _c = c;}
};
void main()
{
	C c2; c2.set(5);
	C c; c.set(10);

	c2.print ();
	c.print ();

	c.save();	

	c2.load();
	c2.print();	
}
  
this code only works ( c2 == c) if C overloads A::getBinData!! i realize that that is probably proper functioning(?), but, to me, it doesnt seem very consistant with other is-a behavior. what do you think? if your wondering, i got the idea here.

Share this post


Link to post
Share on other sites
I have no idea what you''re trying to say here, but it would appear you''re missing the point. The ''is-a'' relationship is something you have to implement. It''s not something that just happens. If you don''t create the necessary virtual functions, then of course it won''t hold true. It can''t. Sizeof() is done at compile-time. And binary copying of C++ objects is just plain wrong.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
i realize how retarded binary copying is, it was just an exercise.

im not sure what i was thinking last night. but, i think i forgot that base classes have to allow polymorphism. otherwise, the compiler could create a special class concantenating the base and child (and therefore making sizeof & this proper).

Share this post


Link to post
Share on other sites