In the beginning of my journey learning C++, I thought an object is an OOP-only related term. However, the more I learn and the more I read, I can see that this not the case
From https://timsong-cpp.github.io/cppwp/n3337/intro.object
An object is a region of storage.
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.
Refering to §1.8 of the c++ standard (N4618), an object:
occupies a region of storage during its period of construction, through its lifetime and its period of destruction;
has a lifetime (for non trivial objects it start when initialization is completed and stop when the destructor starts);
has a storage duration (static, dynamic, thread or automatic)
has a type: the object type which is unique (strict aliasing).
may have a name
(Other answers already have detailed the meaning of storage duration.)
The object type (or class) is a unique property of an object. The object type specifies the meaning of the region of storage occupied by the initialized object. So the meaning is unique, from a philosophical point of view, the object type is the species of the object, not its kind.
For the compiler, it only constrains the ensemble of operation that can be applied to the region of storage: the methods associated to the object type (in which case the type is defined by class
or struct
), and all the functions taking the object as an argument (which are visible).
For the programmer, the type also specifies what will be the consequences of the application of a sequence of operations to the object during its lifetime. The type contains much more information than the compiler actualy may be able to know. For exemple after having checked that the size of an object, an_obj
of type std::vector<int>
is 0
, the programmer knows that an_obj.at(0)
will always throw, the compiler may not.
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];
};
Not to bash on the existing answers, but they're missing an element (that's arguably a standard defect).
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 ]
An object is created by a definition ([basic.def]), by a new-expression ([expr.new]) or by the implementation ([class.temporary]) when needed.
The properties of an object are determined when the object is created.
An object is a region of storage in which constuction has taken place. In fact, most of the time "object" refers to that constructed entity, with its value and state, whereas "storage" just means the memory (or whatever) it is written on.
The difference can be a simple detail:
// `s` names an object that has been constructed... somewhere.
// That storage will live exactly as long as necessary to back `s`
// as long as the object exists -- no need to worry about it.
std::string s = "hello";
// Using the object
std::cout << s << '\n';
But you can also (although it's very rarely useful) separate the object's lifetime from the lifetime of its storage:
// `storage` points at a chunk of... storage.
// It hasn't been initialized, nor does it have a type.
void *storage = malloc(sizeof(std::string));
// Now we constructed an `std::string`:
// we have an actual object inhabiting the storage!
std::string *s = new (storage) std::string("hello");
// Using the object, through the pointer we have
std::cout << *s << '\n';
// Now we destruct the object: it exists no more.
s->~basic_string();
// Now we destroy the storage.
free(storage);
I must stress that this last example is for demonstration purposes only. It's a technique you probably won't encounter, and has been performed here with no error checking whatsoever. Don't try this at home :)
Now, how does it relate to the OOP "object"? Well... not at all. "Object" is a very generic term, and OOP founders just chose to use it as well, independently.
In C++ world, an object is the instantiation of a class. It behaves (methods/functions) in certain ways and has attributes (data members) that depicts its state.
An object has lifetime. It is created (through constructor), it lives, and it dies (through destructor).
A class is like a blueprint, through which you define the behavior and attribute of an object.