#i have a question for cpp gurus here. is

1 messages · Page 1 of 1 (latest)

sharp yew
#

what i try to do here is by deriving from A i try to inject some functionality that operates on Actors.

hard night
#

A and Actor are related classes?

sharp yew
#

no

#

there is nothing else

#

only this code - what you see

#

this is self contained code

#

example*

hard night
#

if they are unrelated how will you access Actor subobject inside B?

sharp yew
#

as you can see i convert this pointer to actor

hard night
#

A::f() is unsafe

sharp yew
#

this is the controversial part - is this legal

#

why

hard night
#

multiple inheritance in diff mem addresses

sharp yew
#

i dont understand ^^

hard night
#

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

sharp yew
#

what do you mean be saying 'subobject' ?

hard night
#

actor is itself a object and the childs are subobjects

sharp yew
#

do you mean ue subobjects? because here there is no unreal involved here , this is pure old fashioned c++

hard night
#

so you can't access ActorVariable through this pointer which points to wrong place which will produce undefined behavior

hard night
#

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

sharp yew
#

well this compiles and runs well

#

it gives proper output

#

but i am unshure if by pure luck or no

hard night
#

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
{};

sharp yew
#

yes it wont compile

#

neither with static_cast

hard night
#

the code i posted should compile, it has a virtual destructor

sharp yew
#

ohh ok maybe with this it does

#

what difference it makes ? adding virtual destructor?

hard night
#

it makes it polyphormic class

sharp yew
#

whats polymorphic class? only those classes support dynamic_casts ?

hard night
#

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...


sharp yew
#

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

hard night
#
  • always avoid cstyle casts for cross-casting in multiple inheritance scenarios they are unsafe
sharp yew
#

i hope you understand

hard night
#

it points to A subobject within the B

#

not beginning

sharp yew
#

ok so its type is A* not B*

#

correct?

hard night
#

right

| Actor subobject | A subobject |
^                 ^
|                 |
start of B        A part inside B
sharp yew
#

ok - because this is what i was unshure of. how 'this' in B::f() is behaving.

#

thank you ^ ^

hard night
#

call b.f(), where b is type B, method f() is inherited from A.

sharp yew
#

ok i got it - thanks again ; )

hard night
#

glad I helped, I'm also not very experience in C++, still reading learncpp.com in my free time

sharp yew
#
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