#i have a question for cpp gurus here. is
1 messages · Page 1 of 1 (latest)
what i try to do here is by deriving from A i try to inject some functionality that operates on Actors.
A and Actor are related classes?
no
there is nothing else
only this code - what you see
this is self contained code
example*
if they are unrelated how will you access Actor subobject inside B?
as you can see i convert this pointer to actor
A::f() is unsafe
multiple inheritance in diff mem addresses
i dont understand ^^
the Actor subobject and the A subobject inside B are at different memory addresses, c style cast (Actor*)(this) just reinterprets the pointer without adjusting for the offset, so you get a pointer to the wrong place
what do you mean be saying 'subobject' ?
actor is itself a object and the childs are subobjects
do you mean ue subobjects? because here there is no unreal involved here , this is pure old fashioned c++
so you can't access ActorVariable through this pointer which points to wrong place which will produce undefined behavior
unreal c++ is not different
its same, unreal has just a handcrafted API to make life easier
use virtual inheritance or design A as a template or a mixin that expects the derived class to be an Actor
well this compiles and runs well
it gives proper output
but i am unshure if by pure luck or no
it will compile but its unsafe
refactor your code using dynamic cast
class Actor
{
public:
virtual ~Actor() = default;
int ActorVariable = 88;
};
class A
{
public:
void f()
{
Actor* ThisAsActor = dynamic_cast<Actor*>(this);
if (ThisAsActor)
std::cout << ThisAsActor->ActorVariable;
else
std::cout << "Not an Actor";
}
};
class B : public Actor, public A
{};
the code i posted should compile, it has a virtual destructor
it makes it polyphormic class
whats polymorphic class? only those classes support dynamic_casts ?
i think yeah
but if you want nonpolymorphic class, use template without virtual destructor
template <typename Derived>
class A
{
public:
void f()
{
Derived* ThisAsActor = static_cast<Derived*>(this);
remaining code...
0k i understand
so the last question is
the class B has a method f that uses this pointer. where this pointer points to ? to the beginning of B or to the place where A starts within B
- always avoid cstyle casts for cross-casting in multiple inheritance scenarios they are unsafe
i hope you understand
right
| Actor subobject | A subobject |
^ ^
| |
start of B A part inside B
ok - because this is what i was unshure of. how 'this' in B::f() is behaving.
thank you ^ ^
call b.f(), where b is type B, method f() is inherited from A.
ok i got it - thanks again ; )
glad I helped, I'm also not very experience in C++, still reading learncpp.com in my free time
class Actor
{
public:
int ActorVariable = 88;
};
class A
{
public:
double AVariable = -13.987873;
void f()
{
Actor* ThisAsActor = (Actor*)(this);
std::cout<<ThisAsActor->ActorVariable;
}
};
class B : public Actor, public A
{};
int main()
{
B b;
b.f();
return 0;
}
i just tried this code and indeed it gives nonse answer
because i am reading not correct memory address