Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- class Parent
- {
- int id;
- public:
- Parent() = default;
- Parent(int idP) : id{ idP } { std::cout << "Parent ctor\n"; }
- //virtual const std::string& getName(bool simple = true) const {};
- virtual ~Parent() = default;
- };
- class Child : public Parent
- {
- char symb;
- std::string name;
- public:
- Child(int idP, char symbP, std::string nameP) : Parent{ idP }, symb{ symbP }, name{nameP} { std::cout << "Child ctor\n"; };
- const std::string& getName(bool simple = true) const
- {
- if(simple) return name;
- return name + " class symbol -> " + symb;
- }
- std::string& getName(bool simple = true)
- {
- //if (simple) return name;
- //return name += " class symbol -> " + symb;
- return const_cast<std::string&>((const_cast<const Child*>(this))->getName());
- }
- };
- class Other
- {
- };
- void processParents(Parent& p)
- {
- // try to proces different Parent descendants polymorphically
- }
- template <typename T>
- void typeInfo(const T& obj)
- {
- std::cout << "Real type of object is: " << typeid(obj).name() << '\n';
- }
- int strLen(const char* str)
- {
- int len{};
- for (; *str++; ++len);
- return len;
- }
- void childInfo(const Child& ch)
- {
- std::cout << "This is Child object with name -> " << ch.getName() << '\n';
- }
- int main()
- {
- // static_cast
- // dynamic_cast
- // reinterpret_cast
- // const_cast
- //int numI{ 42 };
- //float numF{ 36.6 };
- //int* ptrI{};
- //float* ptrF{};
- //void* ptrV{ &ptrF };
- //ptrI = reinterpret_cast<int*>(ptrF);
- //ptrI = (int*)ptrF;
- //std::cout << (static_cast<float>(numI) / 9) << '\n';
- /*Parent* ptrP{};*/
- ////ptrP = static_cast<Other*>(new Child{ 42,'z' });
- ////ptrP = static_cast<Parent*>(new Child{ 42,'z',"Straustrup" });
- //ptrP = new Child{ 42,'z',"Straustrup" };
- //ptrP = new Parent(13);
- //Parent pappi{ };
- //pappi = *ptrP;
- //std::cout << typeid(*ptrP).name() << " is Parent ? " << std::boolalpha << (typeid(*ptrP) == typeid(Parent)) << '\n';
- //std::cout << dynamic_cast<Child*>(ptrP)->getName() << '\n';;
- //Child* effectivePtr{ static_cast<Child*>(ptrP) };
- //Parent* effectivePtr{ (Child*)(ptrP) };
- //if (effectivePtr)
- //{
- // std::cout << "we've got child! ";
- // //std::cout << dynamic_cast<Child*>(effectivePtr)->getName() << '\n';
- // std::cout << effectivePtr->getName() << '\n';
- //}
- //else
- //{
- // std::cout << "we've got parent! ";
- //}
- //if (typeid(*ptrP) == typeid(Child))
- //{
- // std::cout << "We've got Child! ";
- // std::cout << (((Child*)ptrP)->getName()) << '\n';
- //}
- //else
- //{
- // std::cout << "We've got Parent! ";
- //}
- //Parent p;
- //Parent& refParent{ p };
- //Child child{ 11,'g',"Bill" };
- //Parent& refParent{ child };
- //try
- //{
- // Child& refChild{ static_cast<Child&>(refParent) };
- // std::cout << refChild.getName() << '\n';
- //}
- //catch (std::exception ex)
- //{
- // std::cout << ex.what() << '\n';
- //}
- //catch (...)
- //{
- // std::cout << "OOps! :(" << '\n';
- //}
- //typeInfo(23);
- //typeInfo('z');
- //typeInfo(Parent{});
- //typeInfo(Child{1,'z',"Gates"});
- //const int answer{ 42 };
- //int& refAnswer{ const_cast<int&>(answer) };
- ////int& refAnswer{ answer };
- //refAnswer = 13;
- //std::cout << "refAnswer = " << refAnswer << '\n';
- //std::cout << "answer = " << answer << '\n';
- /*const char* str{ new char[20]{"Hello, world!"} };
- std::cout << "Length of str is: " << strLen(const_cast<char*>(str)) << '\n';
- const Child ch{ 1,'z',"Baaddy" };
- childInfo(const_cast<Child&>(ch));
- childInfo(ch);
- delete[] str;*/
- //int answer{ 42 };
- //const int* ptr{ &answer };
- //*(const_cast<int*>(ptr)) = 13;
- Child ch{ 1,'z',"Baaddy" };
- std::cout << ch.getName() << '\n';
- return 0;
- }
- // RTTI - Runtime type identification
- // A -> B -> C
- // B-D -E
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement