#include <Core/Core.h> using namespace Upp; class A : public Moveable<A>{ public: A(){} virtual void Hello(){ Cout() << "Hello from A" <<"\n"; } }; class B :public A, public Moveable<B>{ public: B(){} void Hello(){ Cout() << "Hello from B" <<"\n"; } }; CONSOLE_APP_MAIN { Vector<A> myVector; static_cast<B&>(myVector.Add()).Hello(); //Looking for "Hello from B" }
Array can be also used to store polymorphic elements - stored elements could be derived from T. To store such elements, you pass a pointer to an element previously created on the heap. Still, Array takes over ownership of such element (it e.g. deletes it when appropriate). You can also use this method to create an Array of elements that do not have either pick, deep copy constructor nor default constructor.
#include <Core/Core.h> using namespace Upp; class A { public: A(){} virtual ~A(){} virtual void Hello(){ Cout() << "Hello from A" <<"\n"; } }; class B :public A{ public: B(){} ~B(){} void Hello(){ Cout() << "Hello from B" <<"\n"; } }; CONSOLE_APP_MAIN { Array<A> myArray; B b; static_cast<B&>(myArray.Add(b)).Hello(); //"Hello from A" ((B&)myArray.Add(b)).Hello(); //"Hello from A" //Looking for "Hello from B" without using Array<A*> Array<A*> myArray2; myArray2.Add(&b)->Hello(); //Working properly but using ptr }
struct A { A() {} virtual ~A() {} virtual void Hello() const { Cout() << "Hello from A" << EOL; } }; struct B : A { B() {} ~B() {} void Hello() const { Cout() << "Hello from B" << EOL; } }; CONSOLE_APP_MAIN { Array<A> arrA; arrA.Create<B>(); arrA.Add(new B); arrA.Add(); for (const A& a: arrA) a.Hello(); }
It mean I must use an Array<A*> ? Their is no way to do something like the exemple bellow without having to work with ptr ?
struct A { virtual ~A() { Hello(); } virtual void Hello() const { Cout() << "Hello from A" << EOL; } }; struct B : A { void Hello() const { Cout() << "Hello from B" << EOL; } }; CONSOLE_APP_MAIN { Array<A> arrA; arrA.Create<B>(); }
Why the code below is working the way it is working (printing out "Hello from A" instead of "Hello from B")? Smile
A hint: I'm not using a keyword class. I'm using struct instead. This makes code shorter and cleaner.
Also Except Struct is well aligned in memory and you didn't set accessor flag to public, what's the difference between class and struct ?
Quote:Why the code below is working the way it is working (printing out "Hello from A" instead of "Hello from B")?
That's because we didn't define any destructor on B.
Xemuth wrote on Mon, 16 September 2019 03:35
Quote:Why the code below is working the way it is working (printing out "Hello from A" instead of "Hello from B")?
That's because we didn't define any destructor on B.
This is not a correct answer.
Novo wrote on Mon, 16 September 2019 16:31Xemuth wrote on Mon, 16 September 2019 03:35
Quote:Why the code below is working the way it is working (printing out "Hello from A" instead of "Hello from B")?
That's because we didn't define any destructor on B.
This is not a correct answer.
Since B inherite from A, B will call A's destructor right after exection of is own destructor.
B b; A* a = &b; a->Hello();
A only know is own function ?
Define Hello() as virtual on A allow us to redefine Hello() on Children but since B destructor will call A destructor at is exit and
A don't know if child have possible redefinition of Hello() he will call is own Hello() declaration.
struct A { virtual ~A() { Hello(); } void Hello() const { Cout() << "Hello from A" << EOL; } }; struct B : A { void Hello() const { Cout() << "Hello from B" << EOL; } };
B b; A* a = &b; a->Hello(); //Printing "Hello from A"
B b; A* a = &b; //here Hello() is virtual in A and redefined in B a->Hello();//C++ try to know real type of "a" and see it's a B object (or maybe a B* even if I store it in A*) then Printing "Hello from B"