'Instance of the same class inside a class in C++ [duplicate]

I want my class to have an attribute which would be an instance of the same class, something like this:

class Person
{
public:
    std::string name;
    Person nested;
};

Here my IDE (Visual Studio 2022) says incomplete type is not allowed. Is it possible to have an instance of the same class inside a class in C++? How can I implement this?



Solution 1:[1]

Person nested; will have default value, which is empty Person
So when you create Person, inside it automatically will be created Person, inside which will be created Person and so on

to avoid this, you need to use pointer

class Person
{
public:
    std::string name;
    Person* nested;
};

source: https://mrcodehunter.com/incomplete-type-is-not-allowed/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rigorich