I followed a number of posts on SO, and finally I can draw a conclusion that when we have something like :
Person name;
name
is an object of class person
.
It becomes instance when instantiate it :
name=new Person();
I am a beginner in C++, and so far I have seen we can access the functions and variables like:
Person name;
name.getValue;
name.callFunction();
We need not to use new
operator for this. So can we say the differentiating factor between an object and instance can be ignored in C++?
An object is a generic thing while an instance is a single object that has been created in memory. Usually an instance will have values assigned to it's properties that differentiates it from other instances of the type of object.
Instance refers to Reference of an object. Object is actually pointing to memory address of that instance. A single object can have more than one instance. Instance will have the both class definition and the object definition where as in object it will have only the object definition.
C Classes An instance type is a struct containing variable members called instance variables and function members called instance methods. A variable of the instance type is called an instance. A class object is a global const struct variable containing class variables and class methods.
An object is basically a block of memory that has been allocated and configured according to the blueprint. A program may create many objects of the same class. Objects are also called instances, and they can be stored in either a named variable or in an array or collection.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With