Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
955 views
in Technique[技术] by (71.8m points)

oop - Difference between Object and instance : C++

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...