Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are classes not objects in C++?

Tags:

c++

class

I was reading "Design Patterns: Elements of Reusable Object-Oriented Software", (specifically the chapter about the prototype design pattern) and it stated that...

"Prototype is particularly useful with static languages like C++ where classes are not objects, and little or no type information is available at run-time." (pg 121)

(emphasis mine)

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

like image 469
Michael0x2a Avatar asked Dec 02 '22 22:12

Michael0x2a


1 Answers

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

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

You may be wondering why you'd want this, and usually you don't need it -- it's like C++ template meta-programming, you only need it when you need it because you can't achieve your goal in any other way. It's probably also the case that problems you'd solve in Python with meta-classes you'd solve in C++ using template meta-programming.

like image 64
Andrew Aylett Avatar answered Dec 08 '22 00:12

Andrew Aylett