|
|
Home » Community » Newbie corner » |SOLVED] Vector of object: cast to inherited class
|SOLVED] Vector of object: cast to inherited class [message #52353] |
Fri, 13 September 2019 16:32  |
 |
Xemuth
Messages: 387 Registered: August 2018 Location: France
|
Senior Member |
|
|
Hello,
Is it possible to do something like that :
#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"
}
Without using Vector of ptr or reference ?
Thanks in advance
Best regard.
[Updated on: Sun, 15 September 2019 12:53] Report message to a moderator
|
|
|
|
|
|
Re: Vector of object: cast to inherited class [message #52364 is a reply to message #52363] |
Sat, 14 September 2019 21:39   |
Novo
Messages: 1430 Registered: December 2006
|
Ultimate Contributor |
|
|
Check how code below works.
A hint: I'm not using a keyword class. I'm using struct instead. This makes code shorter and cleaner.
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();
}
Regards,
Novo
|
|
|
Re: Vector of object: cast to inherited class [message #52365 is a reply to message #52363] |
Sat, 14 September 2019 21:45   |
Novo
Messages: 1430 Registered: December 2006
|
Ultimate Contributor |
|
|
Xemuth wrote on Sat, 14 September 2019 07:41
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 ?
Array<A> is similar to Vector<A*>.
The only difference is that it (Array<A>) owns data. This means that Array<A> in its destructor will call delete on each pointer it stores, so, you cannot store pointers to objects on stack in an Array.
Regards,
Novo
|
|
|
|
|
|
Re: |SOLVED] Vector of object: cast to inherited class [message #52377 is a reply to message #52353] |
Mon, 16 September 2019 09:35   |
 |
Xemuth
Messages: 387 Registered: August 2018 Location: France
|
Senior Member |
|
|
Hello Novo,
Quote:Why the code below is working the way it is working (printing out "Hello from A" instead of "Hello from B")? Smile
That's because we didn't define any destructor on B.
Edit : I just tried to define B destructor and A destructor is still called after B destructor call.
That's not the behaviour I would have imagined but it's quite logique.
Quote: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 ?
Thanks in advance.
[Updated on: Mon, 16 September 2019 10:43] Report message to a moderator
|
|
|
Re: |SOLVED] Vector of object: cast to inherited class [message #52381 is a reply to message #52377] |
Mon, 16 September 2019 16:29   |
Novo
Messages: 1430 Registered: December 2006
|
Ultimate Contributor |
|
|
Xemuth wrote on Mon, 16 September 2019 03:35Also Except Struct is well aligned in memory and you didn't set accessor flag to public, what's the difference between class and struct ?
Default visibility for class is private.
Default visibility for struct is public.
That includes inheritance.
There is no other difference.
Memory alignment is a completely separate topic.
Regards,
Novo
|
|
|
Re: |SOLVED] Vector of object: cast to inherited class [message #52382 is a reply to message #52377] |
Mon, 16 September 2019 16:31   |
Novo
Messages: 1430 Registered: December 2006
|
Ultimate Contributor |
|
|
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.
Regards,
Novo
|
|
|
|
Re: |SOLVED] Vector of object: cast to inherited class [message #52384 is a reply to message #52383] |
Mon, 16 September 2019 17:44   |
Novo
Messages: 1430 Registered: December 2006
|
Ultimate Contributor |
|
|
Xemuth wrote on Mon, 16 September 2019 10:47Novo 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.
This particular statement is correct, but Hello() is a virtual method and it is supposed to print "Hello from B".
B b;
A* a = &b;
a->Hello();
This code will print "Hello from B".
Why the code in quiz is working differently?
Regards,
Novo
|
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Tue May 13 11:20:55 CEST 2025
Total time taken to generate the page: 0.00831 seconds
|
|
|