0

How can I make an instance of a class inside its definition?

class Test{
  Test _test;
};

This throws

error: field _test has incomplete type

Using a pointer works but I would like to avoid using a pointer if possible.

simonc
  • 41,632
  • 12
  • 85
  • 103
tambalolo
  • 1,035
  • 3
  • 14
  • 30
  • 1
    Why do you want to do this? – Nathan White Mar 05 '13 at 10:44
  • 1
    You can't, or you get instanception. – StoryTeller - Unslander Monica Mar 05 '13 at 10:46
  • 1
    How do you imagine it would work? What would be the size of `Test`? – Maxim Egorushkin Mar 05 '13 at 10:46
  • 2
    I think your are asking us about a solution you had in mind instead of the problem you try to solve. Why do you think you need an instance within itself? – StoryTeller - Unslander Monica Mar 05 '13 at 10:47
  • 1
    `Test& get_innermost_instance() { return this->_test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test._test. – PlasmaHH Mar 05 '13 at 10:59
  • The term *`throws`* usually refers to a runtime error. I think you mean the compiler reports an error. – Peter Wood Mar 05 '13 at 11:30
  • 1
    You want to create a box that has an exact copy of itself inside, including the exact copy. How much would it weigh? – Peter Wood Mar 05 '13 at 11:31

4 Answers4

8

Just imagine you could do something like that: this would be a recursive definition. An object of type Test contains an object of type Test, which contains an object of type Test, and so on without a termination until you or your compiler go crazy.

I don't really think you would want anything like that, even if it compiled. Using pointers, on the other hand, is OK for two reasons:

  • A pointer could by null, thus breaking the otherwise infinitely recursive chain of Test objects (which would take infinite space at run-time);
  • A pointer has fixed size, so the compiler doesn't need to compute the size of Test in order to compute the size of Test itself (leading to infinite recursion at compile-time).

Using a pointer works but I would like to avoid using a pointer if possible.

If you are worried about manual memory management, just use a smart pointer:

#include <memory>

class Test
{
    std::unique_ptr<Test> _test;
};
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
4

Answer this question: how big would your class be?

The only two solutions I can think of is either by using a pointer:

class Test
{
public:
    Test * data;
};

This solution is common in advanced structures, such as lists or trees. Another way is to declare the internal field as static:

class Test
{
public:
    Test(int i);

    static Test data;
}

Test Test::data = Test(5);

This solution is common for singletons, multitons or similar structures. Notice though, that you can access the field only from the class, not from its instance:

Test test = Test::data;
Community
  • 1
  • 1
Spook
  • 25,318
  • 18
  • 90
  • 167
0

Your Test is not yet defined completed so how come you create its instance. Sure you can create pointer. Because pointer is incomplete type. That is why Singleton class and link list have pointers to their reference and not complete object itself.

Pranit P Kothari
  • 326
  • 3
  • 11
0

The memory for the class variables is initialized when an object of the class is made. But if one makes the object of the class in its definition only then during that point the compiler will not be able to initialize the variable of the class as at that point the definition of the class is yet not completed.

Saurabh B
  • 165
  • 10