Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

give a subobject a pointer to a parent object in the parent object's constructor?

Tags:

c++

object

oop

this

I am trying to find an easy way to assign the value of the "this" pointer to another pointer. The reason I want to be able to do this is so that I can have an automatic pointer to every seed's parent apple object. I know I can manually assign the address of a parent apple to a seed as such: MyApple.ItsSeed->ParentApple = &MyApple; but I am trying to find a way to do this more conveniently using the "this" pointer. Let me know if this is recommended/possible and if so - tell me what I am doing wrong.

This is what I have right now:

main.cpp:

#include <string>
#include <iostream>
#include "Apple.h"
#include "Seed.h"

int main()
{
///////Apple Objects Begin///////
    Apple       MyApple;
    Seed        MySeed;

    MyApple.ItsSeed = &MySeed;

    MyApple.Name = "Bob";

    MyApple.ItsSeed->ParentApple = &MyApple;

    std::cout << "The name of the apple is " << MyApple.Name <<".\n";
    std::cout << "The name of the apple's seed's parent apple is " << MyApple.ItsSeed->ParentApple->Name <<".\n";

    std::cout << "The address of the apple is " << &MyApple <<".\n";

    std::cout << "The address of the apple is " << MyApple.ItsSeed->ParentApple <<".\n";

    return 0;
}

Apple.h:

#ifndef APPLE_H
#define APPLE_H

#include <string>

#include "Seed.h"


class Apple {
public:
    Apple();
    std::string Name;
    int Weight;
    Seed* ItsSeed;
};

#endif // APPLE_H

Apple.cpp:

#include "Apple.h"
#include "Seed.h"

Apple::Apple()
{
    ItsSeed->ParentApple = this;
}

Seed.h:

#ifndef SEED_H
#define SEED_H

#include <string>

class Apple;

class Seed {
public:
    Seed();
    std::string Name;
    int Weight;
    Apple* ParentApple;
};

#endif // SEED_H

Seed.cpp:

#include "Seed.h"

Seed::Seed()
{

}

Everything compiles fine. But whenever I uncomment ItsSeed->ParentApple = this; the program crashes without producing any output. This is a contrived example to demonstrate the problem. I feel like the problem is related to a misuse of the "this" pointer or it might have something to do with a circular loop of some kind. But I am not sure - I am not getting good results assigning the value of "this" to anything. Thanks.

like image 644
Stepan1010 Avatar asked Nov 25 '25 12:11

Stepan1010


1 Answers

This is expected, because you have not initialized ItsSeed to anything at that point; you are dereferencing an uninitialized pointer. This triggers undefined behavior, which in this particular instance has caused a crash.

You need to initialize the pointer to something non-null before you try to dereference it.

For example, you might use a pair of constructors, and only set the seed's ParentApple field when you have been given a non-null pointer:

Apple::Apple() : ItsSeed(NULL)
{
}

Apple::Apple(Seed * seed) : ItsSeed(seed)
{
    if (seed) {
        seed->ParentApple = this;
    }
}
like image 90
cdhowie Avatar answered Nov 27 '25 01:11

cdhowie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!