2

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.

curiousguy
  • 8,038
  • 2
  • 40
  • 58
csguy
  • 1,354
  • 2
  • 17
  • 37
  • cplusplus.com has a lot of inaccuracies like this, around here you will find people recommendeing it be avoided – M.M Aug 23 '19 at 01:25

1 Answers1

8

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.

bolov
  • 72,283
  • 15
  • 145
  • 224
  • The std isn't authoritative either. It's full of inconsistencies, obvious issues; ultimately the intuition of compiler writers is authoritative. – curiousguy Aug 23 '19 at 01:24
  • 2
    @curiousguy I strongly disagree. It may be flawed and contain inconsistencies (I don't know about full of), but it is **the standard** maintained by International Organization for Standardization. It's as authoritative as authoritative can be. – bolov Aug 23 '19 at 01:26
  • Please point to one instance where the std disagreed with common sense and intuition of knowledgeable programmers and the std won in the end. – curiousguy Aug 23 '19 at 02:26
  • @curiousguy "authoritative: Proceeding from an official source and requiring compliance or obedience" – bolov Aug 23 '19 at 02:36
  • There is no compiler that complies with the absurd idea that a pointer converted to a base class still points to the most derived object. No user program complies with the idea that you can't use a string literal, ever (as its lifetime isn't start, ever). Or that you can't write any MT program because MT semantics isn't defined, as the only way to define it is by sequential consistency. – curiousguy Aug 23 '19 at 02:44