In C++ "object" and "instance" are used nearly interchangably.
There is a general programming design pattern of class
and instance
. The class
holds the information about all instance
s in that class
.
In C++ when you declare a class
or struct
, the compiler makes code that describes how you create an instance
of that class
, what the data layout is, and provides some methods that can be used to interact with that instance
(up to and including destruction).
virtual
methods and inheritance seemingly moves some of the methods and layout to the instance: but the amount is quite limited. Instead, each instance holds pointers to virtual
class data. In some languages, you can do things like replace individual methods of an instance at runtime: but not in C++.
When you create an instance of that class
or struct
, it can be via an automatic named variable on the stack (like Foo f;
), an anonymous automatic named variable (like some_function( Foo(17,22) )
), an instance on the free store (like new Foo(17, 22)
), or via placement-new
(which is how std::vector
and std::make_shared
creates instances).
Confusingly, there is a separate parallel class
-instance
pattern in C++ -- class template
-class
. The class template
is the class
, the instantiation is the instance. The template
arguments and specializations indicate how, at compile time, you can "construct" the class
es. Pattern matching on the class template
s provide a limited amount of properties that are not tied to the instances ("class properties" in the pattern). (Arguably the function template-function is another instance of the pattern).
If you look at the C++1y proposal for concepts lite you will see where object and instance might mean different things in C++.
int x = 0;
int& foo = x;
int* bar = &x;
x
is both an object and an instance of the type int
.
foo
is an instance of the type int&
, but calling foo
an object is probably wrong! It is a reference -- an alias, or a different name for some object (in this case x
).
bar
is a pointer to an int
, which is an instance of type int*
, and calling it an object is probably correct.
This is a useful distinction: a type does not have to denote an object type if it is a reference type. Object types behave differently than reference types in a number of important ways.
Now, some types have "reference semantics", in that they behave like references in many ways, but are actually class
es. Are instances of such a type better called references or objects? In horrible cases, some instances have a mixture of both reference and object semantics: such is often a bad sign.
Via latest standard in 3.9 [Types] we have the kinds of types in C++. They describe what an object type is:
Types describe objects (1.8), references (8.3.2), or functions (8.3.5)
and
An object type is a (possibly cv-qualified) type that is not a function type, not a reference type, and not a void type.
So calling the "instances" of things that are function types or reference types "objects" seems incorrect. Note that accessing the "representation" of a function or a reference instance is basically impossible: references alias into the object they refer to, and using the name of a function decays to a pointers-to-functions at the drop of a hat (and pointers-to-a-function are basically opaque handles that let you invoke them).
So arguably functions are not instances, and references are not instances.
On the third hand, we do talk about instantiations of class
template
s and function template
s. 14.7 is "template instantiation and specialization", and points of instantiation (of a template
) are all formal terms from the standard.