How are classes not objects in C ++?

I read " Design Patterns: Elements of Reusable Object-Oriented Software" (in particular, the chapter on the prototype design pattern), and he stated that ...

"The prototype is especially useful with static languages ​​such as C ++, where classes are not objects and type information is not available at all at runtime." (pg 121)

(my emphasis)

I always thought that classes are synonyms for objects, and I'm confused as to what this statement means. How are classes not objects and why does it matter if the language is static?

+4
source share
9 answers

In C ++, this declares a class:

class A { public: int a; }; 

whereas this declares an object:

 A a; 

You cannot interrogate a class at runtime because you can interrogate an object. It makes sense to say: "object" a ", what is your address? Please call operator+ , etc." In C ++ with its static typing, it makes no sense to say: "Class A, what is your list of members? Please add a new member" b "."

In other languages ​​(Python comes to mind), you can manipulate classes this way because each class is also an object. In addition to being used as a template for objects, the class itself is an object - it can be printed, edited, etc.

+3
source

A class in C ++ is not an object: a class is a description of how to build an object, and a reference to the type of object.

Compare with a language like Python: in python, like in C ++, you instantiate an object from a class. Unlike C ++, the class you use is also an object: it is usually of type type , and you can create new ones at runtime, manipulate them like any other object, or even create objects that are classes that themselves have different types.

You might be wondering why you need it, and usually you don’t need it - it is like metaprogramming a C ++ template, you only need it when you need it, because you cannot achieve your goal in any other way. This is probably also the case when the problems you solved in Python are with metaclasses that you would have solved in C ++ using template meta-programming.

+5
source

Expand on what Andrew Islett said.

A class in C ++ is not an object: a class is a description of how to build an object, and a reference to the type of object.

Also in languages ​​like Python or smalltalk. All this is an object. A function is an object, a class is an object. Thus, these languages ​​are dynamically typed, which means that types are checked at runtime, and variables can accept any type.

C ++ is statically typed. Variables can take only one type, and type checking is performed at compile time.

So, for example, in python, you can modify the class on the fly. Add functions and fields because it is an object and is subject to change.

+2
source

For example, a class may describe what a book is: Name, Author, Date of publication, Description.

The class object "Book" will be a specific book: C ++ Primer Plus, Stephen Prata, 2005, The book in which C ++ is taught.

So classes do not match objects.

+2
source

This sentence refers to the fact that classes are not first-order objects in C ++. In other languages, you can pass a class as a parameter to a function, for example, just as you can pass an object or function as a parameter.

There are many more consequences for being first-order entity classes or not, for example, the ability to change a class at runtime or to check the complete internal components of a class, etc.

Typically, classes are considered first-order objects in dynamic languages ​​such as ruby, or in the protocol of meta objects for lisp, etc.

Hope this clarifies this a bit.

+1
source

When a class is called an object, it means that there is an object that represents this class at runtime. In C ++, classes dissolve at compile time. Their instances (i.e., Objects) are simply sequences of bytes containing the fields of an object, without reference to the class itself. C ++ now provides some type information at runtime via RTTI, but this is only for polymorphic types and is not considered an object of the class.

The lack of objects representing classes at runtime is the reason for the lack of reflection in C ++ - there is simply no way to get information about a particular class, since there is no object that represents it.

BTW, C ++ is considered a two-level language: objects are instances of classes, but classes are not instances of anything, because they exist only in the compiler. In three-level languages, such as C # and Java, classes are also objects at runtime, and they themselves are instances of yet another class (class in Java, Type in C #). The last class is an instance of itself, so the language has only 3 levels. There are languages ​​with many levels, but this is beyond the scope of this question ...

+1
source

Classes do not match objects. A class is a (more or less) type, and an object is an instance similar to the following:

 int i; YourClass object; 

Here you would not say that i and int same - not YourClass and object .

What the operator says: Many object-oriented languages ​​are very object-oriented, so they begin to make everything (or almost all) an object (of a particular class). Thus, in many languages, a class will be an instance (hence, an object) of some class class (which can be misleading).

This sometimes has its advantages, since you can consider classes (types) in languages ​​such as, for example, you can consider any other object. You can do very dynamic things with them, for example, store them in variables or even manipulate classes at runtime (for example, create new classes that your program considers necessary).

Take a look at this C ++ - similar pseudo code:

 YourClass myObject = new YourClass(); // creates an object (an instance) Class baseClass = myObject.get_class(); // store the class of myObject in baseClass. That storing a type in a variable (more or less) Class subClass = myObject.inherit(); // dynamically create a new class, that only exists in variable subClass (a Class-object), inheriting from baseClass subClass.add_method(some_function); // extend the new class by adding a new method subClass.get_class() subClass.create_instance(); // declare a new variable (object) of your newly created type BaseClass another_onne = subClass.create_instance(); // also valid, since you inherited 

This obviously translates poorly to C ++, due to the strict typing of C ++. Other languages ​​are more dynamic when typing, and this flexibility can come in handy (and make it more complex, sometimes both at the same time). However, I think this explains the principle if you understand C ++.

+1
source

I always thought classes are synonyms for objects

The language in the OOP literature is sometimes undefined. This does not help either because programming languages ​​have slightly different ideas about what an object is.

A class is a template or definition from which objects (instances of this class) are created. That is, the class provides the structure, signature type, and behavior that are objects of this class (or type ... more on this later.)

An object is just a place in the memory of an instance of this class.

Wikipedia provides good documentation on this. I suggest you read it:

http://en.wikipedia.org/wiki/Class_(computer_programming )

http://en.wikipedia.org/wiki/Object_(object-oriented_programming )

In addition, there is a concept of type. A type (or interface, sometimes referred to in some literature or programming languages) is usually a collection of type / method signatures (and possibly behavior). Things like Java interfaces and pure C ++ virtual classes tend to represent types (but not exactly the same).

Then the class corresponding to this type (whether it be an interface or a pure virtual class) is an implementation of this type.

This class, an implementation of this type, is just a recipe for creating objects of this class / type in memory.

When you create an instance of a class / type, you confirm to create an instance (object) of this class in memory.

In C ++, a class is not an object, since the class itself is not created. A C ++ class is not an instance of any other class (see Definitions above).

OTH, in languages ​​like Java, the class itself is represented by instances of the original class (java.lang.Class). Thus, class X has an object in memory (an instance of java.lang.Class) associated with it. And along with this, with the help of this "class" of the object, you can (theoretically) create an instance or create another instance (or object) of class / type X.

This can be confusing. I highly recommend that you search and read literature on classes, types, prototypes, and objects / instances.

and I'm confused as to what this means. Like classes of no objects,

As explained above. The class is not an object. An object is an instance, a piece of memory, built and initialized with a “recipe” of a class or type.

and why does it matter if the language is static?

This part of the book is a bit misleading because Java, for example, is statically typed, but classes can still be the objects themselves. Perhaps the text refers to dynamically typed languages ​​(like JavaScript), where classes can also be objects or instances.

My suggestion is to never use the word object and just limit the dictionary to “classes” and “instances”. But this is my personal preference. Other people may disagree, and so will be.

+1
source

The simpler, the better for you to understand:

An object is a "physical" instance of a class. It consumes memory while the program is running.

The class describes the object: Hierarchy, properties, methods. A class, it looks like a "template" for creating objects.

+1
source

All Articles