Initializing pointer of itself through the function c++

  access-violation, c++, pointers

This is a class, and one of the variables of the class is a pointer that points to an object made from the same class. I want to make a reference to a child object(new Point object), but when I call a function getChild() from the main function it throws access violation reading location exception. It seems that creating a new Point object is no problem, but the pointer (*child) goes back to null when the function is executed. How can I keep the pointer from pointing back to null?

class Point {
private:
    double x;
    double y;
    Point *child;
public:
    Point(double a, double b){
        x = a;
        y = b;
    }
    Point getChild() {
        return *child;
    }
    void createChild(double a, double b) {
        child = new Point(a, b);
    }
}

Source: Windows Questions C++

LEAVE A COMMENT