-
-
Save hcyang1012/0fe0a92e36d614fec1069ab3c4e1109a to your computer and use it in GitHub Desktop.
Downcast Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CPU { | |
public: | |
void Work(); | |
private: | |
}; | |
class Intel : public CPU{ | |
public : | |
void WorkIntel(); | |
private: | |
}; | |
class AMD : public CPU{ | |
public: | |
void AMDWork(); | |
private: | |
} | |
enum CPUType{ | |
Intel = 0, | |
AMD = 1 | |
} | |
class CPUFactory{ | |
public: | |
static CPU* Get(const CPUType& type){ | |
if(type == CPUType::Intel){ | |
return new Intel(); | |
}else if (type == CPUType::AMD){ | |
return new AMD(); | |
}else{ | |
return nullptr; | |
} | |
} | |
private: | |
} | |
int main(){ | |
// Downcast!!! | |
Intel *intel_cpu = dynamic_cast<Intel *>(CPUFactory::Get(CPUType::Intel)); | |
AMD *amd_cpu = dynamic_cast<AMD*>(CPUFactory::Get(CPUType::AMD)); | |
intel_cpu->IntelWork(); | |
amd_cpu->AmdWork(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment