Search code examples
c++classobjectdefinitionobject-lifetime

Definition of object and instantiation


According to What is a Class and Object in C++?

A Class is like a blueprint, an object is like a house built from that blueprint.

You can have many houses with the same layout/floorplan (read class), but each is it's own instance (read object). Each has it's own owner, furniture, etc.

Note that there are also objects whose blueprint is not a class (e.g. integers).

In summary, there are different types of object (e.g. int, float, etc.). You can create user-defined types, called 'classes'.

In C++, the term object is used for instances of any type, including classes and fundamental types.

However, according to http://www.cplusplus.com/doc/tutorial/classes/

An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.

Isn't that statement inaccurate? An object could be an instantiation of a class, but it could also be an instance of a fundamental type.

Then what is instantiation? Is creating an instance of a fundamental type (ex. int x = 3;) considered "instantiation"? Since it is technically creating an instance of a type.


Solution

  • An object is an instance of a type.

    Moving on to your questions:

    Isn't that statement (the cppreference one) inaccurate ? An object could be an instantiation of a class, but it could also be an instance of a fundamental type.

    You are correct.

    Also, wouldn't creating an instance of a fundamental type (ex. int x = 3;) be considered "instantiation"? Since it is technically creating an instance of a type.

    Again, correct.


    Personal advice: stop worrying too much about the exact definitions and spend your energy more on "learning by writing code"

    how is instantiation defined?

    Ok, since you are insisting, here is me kindly pushing back:

    All the definitions you will find online or in books are not authoritative. The only authority on these things is the C++ standard: here take a peek: N4713. As you can see, the standard is overly technical and difficult to parse and understand. And it is this way because it is in no way meant to be used for learning, it is meant to thoroughly and unambiguously define the syntax and semantics of the language. It's the document the compiler implementators turn to when writing the compiler that you use to convert that sweet C++ source code into machine code.

    Since the standard is not a way to learn C++, everybody came up with their own definitions and terminology meant to more easily convey sometimes much more complex matters. In doing so, sometimes the definitions you see are incomplete, or slightly wrong, or don't apply in weird corner cases. You will find the same terms are used slightly different by different authors. Is this a bad thing? Maybe, but not by much. When learning it is important to have simple concepts and tools at your disposal so that you can take things one step at a time. Losing that "rigorous thoroughness" is a small price to pay imho, but I see how this can be confusing for people who take every word in learning materials very seriously.

    Coming back to your question: in the standard, as far as I can see the termn "instantiation" is not used in this sense, it's used for "template instantiation" which is a different thing.

    But that's ok, we don't need the standard to understand a simple concept. For your context an instantiation is the creation of an object. It's a very simple concept, don't overthink it.

    The C++ standard uses "instantiation" for a different purpose, but you still can understand the concept. Name it something else if you will.