You’ll have to forgive me if this is a really basic question; I haven’t used C++ this much in a long time so I’ve forgotten how a lot of it works.
Anyway, I have a base class and a couple derived classes like this (super oversimplified, but the gist is the same):
class Base
{
public:
Base () { }
int SomeFunction (int x, int y); // abstract definition
};
class Derived1 : public Base
{
public:
Derived1() : Base() { }
int SomeFunction (int x, int y)
{
// actual implementation
return 4;
}
};
class Derived2 : public Base
{
public:
Derived2() : Base() { }
int SomeFunction (int x, int y)
{
// actual implementation
return 7;
}
};
Later on in main I have a list of Base objects:
Base *baseobjects[10];
Later I fill that array with instances of Derived1 and Derived2. That works with baseobjects[i] = &newDerived1 (where newDerived1 is an instance of the Derived1 class). That’s all fine.
What I can’t seem to figure out is how to later iterate through the baseobjects array and call SomeFunction on every instance in the list without explicitly knowing which derived class I’m using. I’ve done this in C# and it works fine, but apparently my C++ syntax is off:
int result = baseobjects[i]->SomeFunction(a, b);
That gives me a LNK2019 error when I try to compile, apparently because it’s looking at the Base class for the implementation and it isn’t there. I’m assuming I have to use some pointer tricks to get it to look at the proper derived method, but nothing I’ve tried yet has worked. Any suggestions?
Your method should be declared
virtual. And in your case, probably purevirtual.Note that, while this is not absolutely required, you might as well declare a
virtualdestructor. Do it if you ever delete a derived instance trough a pointer of the base class.Edit:
Also, regarding the link error you posted:
Either you forgot the
= 0, either you are callingBase::SomeFunction()from somewhere.As Thomas Edleson points out,
= 0does not mean that your function has no implementation: it can have one, but it only requires the derived classes to (re)implement it to not being abstract.If you are interested in this topic, I suggest you read this post.