class inheritence

Started by
6 comments, last by Zahlman 16 years, 2 months ago
So, I have a base class and a derived class..

class CBase_Arrow
{
protected:
	unsigned char * Sprite;
	int Type;
	void Destroy();
};

class CArrow : public CBase_Arrow
{
public:
	CArrow();
};
Now I'm trying to define the Destroy() function for the derived class like so..
/*void CArrow::Destroy()
{
	delete ArrowNorm;
}*/
(commented out because it isn't working..) ...aaand it's not liking it for some reason. error C2509: 'Destroy' : member function not declared in 'CArrow' I'm not having problems with the Type variable, which is also derived, so I don't understand this at all.
Advertisement
Quote:
class CArrow : public CBase_Arrow
{
public:
CArrow();
};


I don't see any function called Destroy declared in CArrow, just like the compiler said.
You should implement your destructor in CBase_Arrow, not in CArrow. In your code it is declared in Arrow.

Probably you want to make destuctor virtual. If so, use the following:
class CBase_Arrow
{
protected:
super_arrow_class *ArrowNorm;
unsigned char * Sprite;
int Type;
virtual void Destroy();

};

class CArrow : public CBase_Arrow

{

public:

CArrow();
void Destroy();
};

void Arrow::Destroy()

{
delete ArrowNorm;
}

void CArrow::Destroy()

{
CBase_Arrow::Destroy();
}
A function called Destroy is not a destructor, but maybe you meant to write a destructor instead.
You must declare the Destroy function in CArrow also. This is a requirement in the derived class when you are overriding a base class function. You need:

class CArrow : public CBase_Arrow
{
public:
CArrow();
protected:
void Destroy();
};
Quote:Original post by RDragon1
A function called Destroy is not a destructor, but maybe you meant to write a destructor instead.


No, i'm not writing a destructor.

Quote:You must declare the Destroy function in CArrow also. This is a requirement in the derived class when you are overriding a base class function. You need:


ah, thanks, news to me.
Quote:Original post by Oni Sephiroth
Quote:Original post by RDragon1
A function called Destroy is not a destructor, but maybe you meant to write a destructor instead.


No, i'm not writing a destructor.

Quote:You must declare the Destroy function in CArrow also. This is a requirement in the derived class when you are overriding a base class function. You need:


ah, thanks, news to me.


Obligatory C++ FAQ link
NextWar: The Quest for Earth available now for Windows Phone 7.
Quote:Original post by Oni Sephiroth
Quote:Original post by RDragon1
A function called Destroy is not a destructor, but maybe you meant to write a destructor instead.


No, i'm not writing a destructor.


Why are you writing a function called "Destroy" instead of a destructor? What will cause it to be called? What will you do with the object after "Destroy"ing it? (Keep in mind that it still exists somewhere. The destructor is code that is automatically called, immediately before the object ceases to exist.)

This topic is closed to new replies.

Advertisement