Skip to content

Instantly share code, notes, and snippets.

@hcyang1012
Created November 29, 2022 00:52
Show Gist options
  • Save hcyang1012/6a8f404556ce24b819cb5778cdcf0fcd to your computer and use it in GitHub Desktop.
Save hcyang1012/6a8f404556ce24b819cb5778cdcf0fcd to your computer and use it in GitHub Desktop.
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