I'm currently having issues with dynamic_cast<> and how I've structured my code. So I have the Manager class, which gets a pointer and attempts to check if its a Derived object, and if it is, run some additional codes in the if statement. But in order for the dynamic_cast<> to work, it requires the class definition to come before the actual dynamic_cast<> usage.
So I've swapped the location of the class Manager and class Derived definition, to make the Derived class's definition to come earlier than the Manager class. But now, I'm having error messages saying that "Manager" is an undefined type. I'm guessing in order to pass Manager as a reference, the Derived class requires Manager's class definition to be defined first.
My best solution that I can think of is instead of passing the Manager class as a reference, pass it down as a pointer (which I am not quite a fan of since I know Manager class will always be a valid pointer so its pointless to make it a pointer). I was just wondering if there are any alternative and better solution than the one I have.
Thank you very much in advance!
Edit: I unfortunately need all of the class definition and implementations to be on one file, so splitting them into .h and .cpp file is a less ideal. For people asking, yes, it is homework, but my professor wants it in one file.
class BaseClass
{
virtual VirtualClassName() = 0;
};
class Manager
{
void FunctionNameOne(const BaseClass* const ClassPointer)
{
if(Derived* DerviedPtr = dynamic_cast<Derived*>(ClassPointer))
{
//Do stuff with DerviedPtr
}
}
};
class Derived : public BaseClass
{
Void FunctionNameTwo(const Manager& Manager)
{
//Do stuff with Manager
}
};