Created
October 31, 2020 14:20
-
-
Save paxbun/0ce074d4c84913ab508158a7fb69862e to your computer and use it in GitHub Desktop.
10월 5주차 튜터링(10월 26일 ~ 11월 1일) 튜터링 때 사용한 코드
This file contains hidden or 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
#include <iostream> | |
class Base | |
{ | |
private: | |
int _value; | |
public: | |
int GetValue() const | |
{ | |
return _value; | |
} | |
void SetValue(int value) | |
{ | |
_value = value; | |
} | |
public: | |
Base(int value = 0) : _value { value } | |
{ | |
std::cout << "Base()" << std::endl; | |
} | |
Base(const Base& base) : _value { base._value } | |
{ | |
std::cout << "Base(const Base&)" << std::endl; | |
} | |
~Base() | |
{ | |
std::cout << "~Base(): " << _value << std::endl; | |
} | |
public: | |
void PrintValue() | |
{ | |
std::cout << "Base has " << _value << std::endl; | |
} | |
}; | |
class Derived : public Base | |
{ | |
private: | |
double _anotherValue; | |
public: | |
double GetAnotherValue() const | |
{ | |
return _anotherValue; | |
} | |
void SetAnotherValue(double anotherValue) | |
{ | |
_anotherValue = anotherValue; | |
} | |
public: | |
Derived(int value = 0, double anotherValue = 0.0) : | |
Base { value }, | |
_anotherValue { anotherValue } | |
{ | |
std::cout << "Derived()" << std::endl; | |
} | |
Derived(const Derived& derived) : | |
Base { derived }, | |
_anotherValue { derived._anotherValue } | |
{ | |
std::cout << "Derived(const Derived&)" << std::endl; | |
} | |
~Derived() | |
{ | |
std::cout << "~Derived(): " << _anotherValue | |
<< std::endl; | |
} | |
public: | |
void PrintValue() | |
{ | |
std::cout << "Derived has " << _anotherValue | |
<< std::endl; | |
} | |
}; | |
int main() | |
{ | |
Base base { 6 }; | |
base.SetValue(25); | |
std::cout << base.GetValue() << std::endl; | |
{ | |
Derived derived { 7, 1.5 }; | |
Derived derived2 { derived }; | |
derived.SetValue(17); | |
derived.SetAnotherValue(2.5); | |
std::cout << derived.GetValue() << std::endl; | |
std::cout << derived.GetAnotherValue() << std::endl; | |
{ | |
base.Base::PrintValue(); | |
derived.Derived::PrintValue(); | |
derived.Base::PrintValue(); | |
{ | |
Base* bases[] = { &base, &derived }; | |
for (Base* ptr : bases) ptr->PrintValue(); | |
} | |
} | |
} | |
} |
This file contains hidden or 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
#include <iostream> | |
struct Base | |
{ | |
int _value; | |
}; | |
int Base_GetValue(const Base* base) | |
{ | |
return base->_value; | |
} | |
void Base_SetValue(Base* base, int value) | |
{ | |
base->_value = value; | |
} | |
void Base_Create(Base* base, int value = 0) | |
{ | |
base->_value = value; | |
std::cout << "Base()" << std::endl; | |
} | |
void Base_Create(Base* base, const Base& other) | |
{ | |
base->_value = other._value; | |
std::cout << "Base(const Base&)" << std::endl; | |
} | |
void Base_Delete(Base* base) | |
{ | |
std::cout << "~Base(): " << base->_value << std::endl; | |
} | |
void Base_PrintValue(Base* base) | |
{ | |
std::cout << "Base has " << base->_value << std::endl; | |
} | |
struct Derived | |
{ | |
Base _base; | |
double _anotherValue; | |
}; | |
double Derived_GetAnotherValue(const Derived* derived) | |
{ | |
return derived->_anotherValue; | |
} | |
void Derived_SetAnotherValue(Derived* derived, | |
double anotherValue) | |
{ | |
derived->_anotherValue = anotherValue; | |
} | |
void Derived_Create(Derived* derived, | |
int value = 0, | |
double anotherValue = 0.0) | |
{ | |
Base_Create((Base*)derived, value); | |
derived->_anotherValue = anotherValue; | |
std::cout << "Derived()" << std::endl; | |
} | |
void Derived_Create(Derived* derived, const Derived& other) | |
{ | |
Base_Create((Base*)derived, other._base); | |
derived->_anotherValue = other._anotherValue; | |
std::cout << "Derived(const Derived&)" << std::endl; | |
} | |
void Derived_Delete(Derived* derived) | |
{ | |
std::cout << "~Derived(): " << derived->_anotherValue | |
<< std::endl; | |
Base_Delete(&(derived->_base)); | |
} | |
void Derived_PrintValue(Derived* derived) | |
{ | |
std::cout << "Derived has " << derived->_anotherValue | |
<< std::endl; | |
} | |
int main() | |
{ | |
Base base; | |
Base_Create(&base, 6); | |
Base_SetValue(&base, 25); | |
std::cout << Base_GetValue(&base) << std::endl; | |
{ | |
Derived derived; | |
Derived_Create(&derived, 7, 1.5); | |
Derived derived2; | |
Derived_Create(&derived2, derived); | |
Base_SetValue(&(derived._base), 17); | |
Derived_SetAnotherValue(&derived, 2.5); | |
std::cout << Base_GetValue(&(derived._base)) | |
<< std::endl; | |
std::cout << Derived_GetAnotherValue(&derived) | |
<< std::endl; | |
{ | |
Base_PrintValue(&base); | |
Derived_PrintValue(&derived); | |
Base_PrintValue((Base*)(&derived)); | |
{ | |
Base* bases[] = { &base, &(derived._base) }; | |
for (Base* ptr : bases) | |
Base_PrintValue(ptr); | |
} | |
} | |
Derived_Delete(&derived2); | |
Derived_Delete(&derived); | |
} | |
Base_Delete(&base); | |
} |
This file contains hidden or 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
#include <iostream> | |
struct Foo | |
{ | |
int i; | |
void PrintValue(int j) | |
{ | |
std::cout << i + j << std::endl; | |
} | |
}; | |
template <typename To, typename From> | |
To transmute(From from) | |
{ | |
union | |
{ | |
To to; | |
From from; | |
} u; | |
u.from = from; | |
return u.to; | |
} | |
int main() | |
{ | |
auto func | |
= transmute<void (*)(Foo*, int), | |
void (Foo::*)(int)>(&Foo::PrintValue); | |
Foo foo { 5 }; | |
foo.PrintValue(12); | |
func(&foo, 12); | |
} |
This file contains hidden or 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
#include <iostream> | |
class Base | |
{ | |
private: | |
int _value; | |
public: | |
int GetValue() const | |
{ | |
return _value; | |
} | |
void SetValue(int value) | |
{ | |
_value = value; | |
} | |
public: | |
Base(int value = 0) : _value { value } | |
{ | |
std::cout << "Base()" << std::endl; | |
} | |
Base(const Base& base) : _value { base._value } | |
{ | |
std::cout << "Base(const Base&)" << std::endl; | |
} | |
~Base() | |
{ | |
std::cout << "~Base(): " << _value << std::endl; | |
} | |
public: | |
virtual void PrintValue() | |
{ | |
std::cout << "Base has " << _value << std::endl; | |
} | |
}; | |
class Derived : public Base | |
{ | |
private: | |
double _anotherValue; | |
public: | |
double GetAnotherValue() const | |
{ | |
return _anotherValue; | |
} | |
void SetAnotherValue(double anotherValue) | |
{ | |
_anotherValue = anotherValue; | |
} | |
public: | |
Derived(int value = 0, double anotherValue = 0.0) : | |
Base { value }, | |
_anotherValue { anotherValue } | |
{ | |
std::cout << "Derived()" << std::endl; | |
} | |
Derived(const Derived& derived) : | |
Base { derived }, | |
_anotherValue { derived._anotherValue } | |
{ | |
std::cout << "Derived(const Derived&)" << std::endl; | |
} | |
~Derived() | |
{ | |
std::cout << "~Derived(): " << _anotherValue | |
<< std::endl; | |
} | |
public: | |
virtual void PrintValue() override | |
{ | |
std::cout << "Derived has " << _anotherValue | |
<< std::endl; | |
} | |
}; | |
int main() | |
{ | |
Base base { 6 }; | |
base.SetValue(25); | |
std::cout << base.GetValue() << std::endl; | |
{ | |
Derived derived { 7, 1.5 }; | |
Derived derived2 { derived }; | |
derived.SetValue(17); | |
derived.SetAnotherValue(2.5); | |
std::cout << derived.GetValue() << std::endl; | |
std::cout << derived.GetAnotherValue() << std::endl; | |
{ | |
{ | |
Base* bases[] = { &base, &derived }; | |
for (Base* ptr : bases) ptr->PrintValue(); | |
} | |
} | |
} | |
} |
This file contains hidden or 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
#include <iostream> | |
struct Base; | |
struct VirtualFunctionTable | |
{ | |
void (*PrintValue)(Base*); | |
}; | |
struct Base | |
{ | |
const VirtualFunctionTable* _vftable; | |
int _value; | |
}; | |
void Base_PrintValue(Base* base); | |
const VirtualFunctionTable BaseVirtualFunctionTable { | |
Base_PrintValue | |
}; | |
int Base_GetValue(const Base* base) | |
{ | |
return base->_value; | |
} | |
void Base_SetValue(Base* base, int value) | |
{ | |
base->_value = value; | |
} | |
void Base_Create(Base* base, int value = 0) | |
{ | |
base->_vftable = &BaseVirtualFunctionTable; | |
base->_value = value; | |
std::cout << "Base()" << std::endl; | |
} | |
void Base_Create(Base* base, const Base& other) | |
{ | |
base->_vftable = &BaseVirtualFunctionTable; | |
base->_value = other._value; | |
std::cout << "Base(const Base&)" << std::endl; | |
} | |
void Base_Delete(Base* base) | |
{ | |
std::cout << "~Base(): " << base->_value << std::endl; | |
} | |
void Base_PrintValue(Base* base) | |
{ | |
std::cout << "Base has " << base->_value << std::endl; | |
} | |
struct Derived | |
{ | |
Base _base; | |
double _anotherValue; | |
}; | |
void Derived_PrintValue(Derived* derived); | |
const VirtualFunctionTable DerivedVirtualFunctionTable { ( | |
void (*)(Base*))Derived_PrintValue }; | |
double Derived_GetAnotherValue(const Derived* derived) | |
{ | |
return derived->_anotherValue; | |
} | |
void Derived_SetAnotherValue(Derived* derived, | |
double anotherValue) | |
{ | |
derived->_anotherValue = anotherValue; | |
} | |
void Derived_Create(Derived* derived, | |
int value = 0, | |
double anotherValue = 0.0) | |
{ | |
Base_Create((Base*)derived, value); | |
derived->_base._vftable = &DerivedVirtualFunctionTable; | |
derived->_anotherValue = anotherValue; | |
std::cout << "Derived()" << std::endl; | |
} | |
void Derived_Create(Derived* derived, const Derived& other) | |
{ | |
Base_Create((Base*)derived, other._base); | |
derived->_base._vftable = &DerivedVirtualFunctionTable; | |
derived->_anotherValue = other._anotherValue; | |
std::cout << "Derived(const Derived&)" << std::endl; | |
} | |
void Derived_Delete(Derived* derived) | |
{ | |
std::cout << "~Derived(): " << derived->_anotherValue | |
<< std::endl; | |
Base_Delete(&(derived->_base)); | |
} | |
void Derived_PrintValue(Derived* derived) | |
{ | |
std::cout << "Derived has " << derived->_anotherValue | |
<< std::endl; | |
} | |
int main() | |
{ | |
Base base; | |
Base_Create(&base, 6); | |
Base_SetValue(&base, 25); | |
std::cout << Base_GetValue(&base) << std::endl; | |
{ | |
Derived derived; | |
Derived_Create(&derived, 7, 1.5); | |
Derived derived2; | |
Derived_Create(&derived2, derived); | |
Base_SetValue(&(derived._base), 17); | |
Derived_SetAnotherValue(&derived, 2.5); | |
std::cout << Base_GetValue(&(derived._base)) | |
<< std::endl; | |
std::cout << Derived_GetAnotherValue(&derived) | |
<< std::endl; | |
{ | |
{ | |
Base* bases[] = { &base, &(derived._base) }; | |
for (Base* ptr : bases) | |
ptr->_vftable->PrintValue(ptr); | |
} | |
} | |
Derived_Delete(&derived2); | |
Derived_Delete(&derived); | |
} | |
Base_Delete(&base); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment