The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Absract Class

When I compile the following with VC++2005 I get the "cannot instantiate abstract class" for COther. To my knowledge I have implemented all of the required methods "Finalize" and "AnotherMethod". CBaseImpl::Finalize and COther::AnotherMethod.

I suspect I am getting into "virtual" base class territory but cannot figure it out. I want to have a base class implement the "Finalize" so that I don't have to repeast code for all of my other classes.

Thoughts?

>>begin example source<<
struct IObject {
    virtual void Finalize() = 0;
};

struct IOther : public IObject {
    virtual void AnotherMethod() = 0;
};

class CBaseImpl : public IObject {
public:
    void Finalize() { delete this; }
};

class COther : public CBaseImpl, public IOther {
public:
    void AnotherMethod() { /* does nothing */ }
};

int _tmain(int argc, _TCHAR* argv[])
{
    COther * o = new COther(); // error C2259 'COther'
    return 0;
}
>>end example source<<
Ken Brittain Send private email
Tuesday, November 28, 2006
 
 
Both IOther and CBaseImpl inherit from IObject, so you're getting a Finalize() from each when you inherit from both in COther. One way to fix this is to make IObject a virtual base class in IOther or CBaseImpl, or by not inheriting IOther from IObject.
sloop
Tuesday, November 28, 2006
 
 
The "IObject" "Finalize" method is "pure virtual", since you have that "= 0" in there.

This means that method MUST be over-ridden by inheriting classes.  Now, IOther inherits from IObject, but does not override "Finalize" (at least, not in the code snippet you've given us).

So, I think it's "COther"s inherited "IOther" class, which is using the pure-virtual parent "Finalize", which is giving you the 'cannot instantiate' error.  Since IOther doesn't implement Finalize, it is still an 'abstract class'.
AllanL5
Tuesday, November 28, 2006
 
 
!!!!Use virtual inheritance where needed!!!!!


struct IObject {
    virtual void Finalize() = 0;
};

--> struct IOther : public virtual IObject { 
    virtual void AnotherMethod() = 0;
};

--> class CBaseImpl : public virtual IObject {
public:
    void Finalize() { delete this; }
};

class COther : public CBaseImpl, public IOther {
public:
    void AnotherMethod() { /* does nothing */ }
};

int _tmain(int argc, _TCHAR* argv[])
{
    COther * o = new COther(); // error C2259 'COther'
    return 0;
}
Dino Send private email
Wednesday, November 29, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz