What is exactly an object in C++?

Short answer

From https://timsong-cpp.github.io/cppwp/n3337/intro.object

An object is a region of storage.


A slightly longer answer

In traditional OOP and OOD, an Object is used to describe class of objects some times and to an instance of a class some times.

In C++, class and struct represent classes.

An object in C++ can be an instance of a class or a struct but it can also be an instance of a fundamental type.

A few simple examples:

int i;

i is an object. It is associated with a region of storage that can be used by the program.

struct foo { int a; int b;};
foo f;

f is an also object. It is also associated with a region of storage that can be used by the program.

int* ptr = new int[200];

ptr is a pointer that points to 200 objects of type int. Those objects are also associated with a region of storage that can be used by the program.


The C++11 standard is pretty clear:

1.8 The C ++ object model [ intro.object ]

An object is a region of storage. [ Note: A function is not an object, regardless of whether or not it occupies storage in the way that objects do. — end note ]

That's it. An object is a chunk of memory in which data can be stored.

If you think about it OO or Object Orientation makes more sense when you realize that in the old days the programs were organized around the functions which operated upon the objects (or data).

The term "object" was around long before object orientation.

What object orientation did was change the program organization from being organized around the functions to being organized around the data itself - the objects.

Hence the term object orientated.

Change of paradigm.

Here we see the paradigm shift from the old days:

struct my_object
{
    int i;
    char s[20];
};

void function(my_object* o)
{
    // function operates on the object (procedural / procedure oriented)
}

To what we have now:

struct my_object
{
    void function()
    {
        // object operates on itself (Object Oriented)
    }

    int i;
    char s[20];
};

Tags:

C++

Object