Difference between Object and instance : C++

42,564

Solution 1

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 instances 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 classes. Pattern matching on the class templates 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 classes. 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 templates and function templates. 14.7 is "template instantiation and specialization", and points of instantiation (of a template) are all formal terms from the standard.

Solution 2

First, you should know that there is no difference between "object" and "instance". They are synonyms. In C++, you also call instances of primitive types like int or double "objects". One of the design principles of C++ is that custom types (i.e. classes) can be made to behave exactly like primitive types. In fact, in C++, one often prefers to refer to "types" and not "classes".

So, types and objects it shall be. Now that we've settled this, I'm afraid I must tell you that your conclusions are wrong.

Person is a type. name is a (not very well named) variable to access an object of that type.

A whole line of C++ code would look like this:

Person name;

This means: "create an object of type Person and let me access it via the name variable".

new Person() is much more complicated. You may be familiar with the new keyword from languages like Java, but in C++, it's a very different beast. It means that a new object of type Person is created, but it also means that you are responsible for destroying it later on. It also gives you a different kind of handle to the newly created object: a so-called pointer. A Person pointer looks like this:

Person*

A pointer is itself a type, and the types Person* and Person are not compatible. (I told you that this would be much more complicated :))

You will notice the incompatibility when you try to compile the following line:

Person name = new Person();

It won't compile; you will instead receive an error message. You'd have to do it like this instead:

Person* name_ptr = new Person();

And then you'd have to access all the members of Person with a different syntax:

name_ptr->getValue();
name_ptr->callFunction();

Finally, remember you must explicitly destroy the object in this case:

delete name_ptr;

If you forget this, bad things can happen. More precisely, your program will likely use more and more memory the longer it runs.

I think that pointers are too advanced yet for your level of C++ understanding. Stay away from them until you actually need them.

Solution 3

Object and instance are two words for the same thing.

Solution 4

"Object" and "instance" are almost interchangeable. In C++, an object is formally any region of storage. "Instance" is not a formally defined term, but we typically refer to "instances of type X", most commonly used with class types.

Foo f;

This declaration creates an object named f. The object's type is Foo. You could say the object f is an instance of Foo.

Your attempt to distinguish the terms was incorrect. The two things you've actually pointed out are two different ways of creating objects.

Person name;

In this case, we're creating an object name of type Person.

Person* name = new Person();

In this case, we're creating an object name of type Person* (pointer to Person). We are also creating another object of type Person using the expression new Person(). This expression returns a pointer, which we are initialising the name object with.

Solution 5

It is very simple but very important

Take a general example: what is the general meaning of object? its nothing but which occupies some space right....keep that in mind we now talk about Object in java or C++

example: here I am creating Object     Student std=new Student();

where Student is a Class and std is a Object because we Created a memory for std with the help of new keyWord it means it internally occupies some space in memory right thats y we call std as Object

if you won`t create memory for an object so we call that object as instance.

example: Student std;

here Student is a class and std is a instance(means a just a copy that class),with this we won`t do anything untill unless we create a memory for that.

Thats all about object and Instance :)

Share:
42,564
joey rohan
Author by

joey rohan

ready 2 help :D

Updated on September 12, 2020

Comments

  • joey rohan
    joey rohan over 3 years

    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++?

  • DSquare
    DSquare about 10 years
    Furthermore, you should always say instance of. So a certain object is an instance of a certain class.
  • joey rohan
    joey rohan about 10 years
    What i have distinguished, will that be correct if we refer Java instead of C++?
  • Joseph Mansfield
    Joseph Mansfield about 10 years
    @joeyrohan No, but "instance" is much more of a Java term. In Java, people tend to talk about "instances of X" rather than "objects of type X".
  • joey rohan
    joey rohan about 10 years
    So there is absolutely no difference between object and instance?
  • Christian Hackl
    Christian Hackl about 10 years
    Right, there is absolutely no difference. Have a look at this FAQ from isocpp.org: isocpp.org/wiki/faq/classes-and-objects#overview-object
  • Christian Hackl
    Christian Hackl about 10 years
    @DSquare: that's debatable. You will find that many C++ programmers talk about "objects". IIRC this is also the preferred terminology in the standard (someone correct me if I'm mistaken).
  • DSquare
    DSquare about 10 years
    @ChristianHackl I'm not saying that you should always say "instance", I'm saying that If you say "instance" what you are saying is a short-hand for "instance of [class]". And we know that an instance of a class is an object. So "instance" equals "object".
  • Christian Hackl
    Christian Hackl about 10 years
    I've searched in the C++11 draft. The word "instance" only appears a few times (mostly in "for instance" :)). I think your point is interesting, and I would probably use the same terminology, but can you point to a place in the standard where it actually talks about an "instance of reference type"?
  • Yakk - Adam Nevraumont
    Yakk - Adam Nevraumont about 10 years
    @ChristianHackl Nope, the standard doesn't seem to talk about instances of non-object types generically. It refers to objects and references, but I cannot find a generic term for "instance of a type" used in the standard...