All I want is the C++ equivalent of the java code for: if(ParentClassObject.myIdentifier == "Child1") (Child1) ParentClassObject ... do things
So all I want is to --Take an object I created as a Child object --Use it in a method that wasn't sure what type of Child it was, so it's treating it as a Parent Object --Cast it as the appropriate Child object
After 3 hours I haven't been able to get this work and my computer is about to become airborne. I've also read a dozen SE articles and tutorials on OOD but didn't find/didn't recognize the solution.
In the stripped down code below, CombatLogLine is the Parent and "CombatLogVersionLine" is the Child. determineTypeOfEvent is the method that created it as a child and then returned it as a parent. Full code is at https://docs.google.com/document/d/1OXF-YyR0DF0VfdUe9r4OUgPIYNRYXsiMMmQ_fkV-QIQ/edit?usp=sharing
The code went into two blocks for some reason, you might have to scroll down to see all the code in the first block. And the lines of interest have the XXXXXXXXXXs in them.
//CombatLogLine.h
#include stdafx.h
class CombatLogLine
{
public:
bool virtual areFieldsOk();
void virtual parseLine();
string typeOfEvent;
//a bunch of stuff I removed
protected:
string theLineOfRawText;
};
class CombatLogVersionLine : public CombatLogLine
{
public:
CombatLogVersionLine(string _theLineOfRawText) : CombatLogLine(_theLineOfRawText)
{
typeOfEvent = "COMBAT_LOG_VERSION";
}
bool areFieldsOk();
void parseLine();
private:
};
//CombatLogLine.cpp
#include "stdafx.h"
#include "CombatLogLine.h"
CombatLogLine::CombatLogLine(string _theLineOfRawText)
{
//stuff I removed for this example involving _theLineOfRawText
typeOfEvent = "TBA";
}
bool CombatLogLine::areFieldsOk()
{
cout << "This is the base classes areFieldsOk" << endl;
return false;
}
bool CombatLogVersionLine::areFieldsOk()
{
if(typeOfEvent == "COMBAT_LOG_VERSION")
{
if(sizeOfLine == 2)
{
cout << "Hallelujah!" << endl;
return true;
}
}
return false;
}
void CombatLogVersionLine::parseLine()
{
}
// main method
....
for(unsigned int i = 0; i < combatLogSplitIntoLines.size(); i++)
{
CombatLogLine currentLine = determineTypeOfEvent(combatLogSplitIntoLines[i]);//determine type of event also creates an appropriate CombatLogLine
if(currentLine.typeOfEvent == "COMBAT_LOG_VERSION")
{
currentLine.areFieldsOk();
cout << "type id " << typeid(currentLine).name() << endl;
CombatLogVersionLine * temp5 = dynamic_cast<CombatLogVersionLine *>(¤tLine); //XXXXXXXXXXXXXXXXX I don't even want to use pointers here but think I have to
if(temp5 == 0)
{
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX dynamic cast fails
cout << "The dynamic cast failed." << endl;
system("PAUSE");
return 0;
}
temp5->areFieldsOk();
continue;
}
...
//method in main file
CombatLogLine determineTypeOfEvent(string _aLine)
{
if(_aLine == "")
{
cout << "Something went wrong asdghaer" << endl;
system("PAUSE");
}
if(_aLine.find("COMBAT_LOG_VERSION") != string::npos)
{
CombatLogVersionLine returnValue(_aLine);
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXX this works properly and creates a "CombatLogVersionLine"
cout << "A CombatLogVersionLine was created. " << typeid(CombatLogVersionLine).name() << endl;
return returnValue;
}
return (CombatLogLine("FAIL"));
}