GuLearn
GuLearn

Reputation: 1864

Is there any overhead to define complicated (large size) class inside another class?

To make my question clear, I'll give a simplified example:

class Foo {
public:
    class Bar {
        int garbage[1000000];
    };
    void playWithGarbage() {
         //whatever...
    }
 }

Now, if I generate a lot of instances of Foo and pass them around by value or reference. Will there be any significant overhead? I tested the size, and it's OK: sizeof(Foo) returns 1

Upvotes: 1

Views: 47

Answers (1)

Joseph Mansfield
Joseph Mansfield

Reputation: 110778

There will be no overhead for objects of type Foo. Foo simply acts like a namespace in which Bar is contained. The existence of Foo::Bar objects is independent of the existence of Foo objects. If Foo had a Bar member, however, then objects of type Foo would have subobjects of type Bar and therefore have an increased size.

Upvotes: 5

Related Questions