Question

For my project I'm working with anonymous classes in C++ and I was wondering where in memory they are allocated.

I'm pretty sure that will be allocated on the heap, but I would like to know if anyone else has more detailed information.

Was it helpful?

Solution

As pointed out, classes are not allocated anywhere. Objects, which are instances of an anonymous class can be allocated in several ways:

For example as subobjects of another object:

struct S
{
  struct {
    int x, y;
  } p;
};

or together with the class definition

struct
{
  float x, y;
} p;

or using decltype:

struct S
{
  struct {
    int x, y;
  } p;
};

void g (decltype (S::p)) {}

decltype (S::p) *f ()
{
  auto p = new decltype (S::p);
  g(*p);
  return p;
}

OTHER TIPS

If the anonymous class is part of another class or structure, then it will be allocated together with the surrounding class or structure, be it on the heap or on the stack.

If the anonymous class is a global variable, it's stored together with other global variables.

If the anonymous class is a local variable, it's stored on the stack together with the other local variables of the function it's defined in.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top