What is the difference between Type and Class?

前端 未结 20 2314
梦谈多话
梦谈多话 2020-11-28 01:00

What makes a type different from class and vice versa?

(In the general language-agnostic sense)

20条回答
  •  眼角桃花
    2020-11-28 01:08

    To add another example of distinction: in C++ you have pointer and reference types which can refer to classes, but are not classes in and of themselves.

    Bar b; // b is of type "class Bar"
    Bar *b2 = &b; // b2 is of type "pointer to Class Bar"
    Bar &b3 = b; // b3 is of type "reference to Class Bar"
    Bar *b4[7]; // b4 is of type "7-element array of pointers to Class Bar"
    Bar ***b5; //b5 is of type "pointer to a pointer to a pointer to Class Bar"
    

    Note that only one class is involved, but a near infinite number of types can be used. In some languages, function are considered "first-class-objects" in which case, the type of a function is a class. In others, the type of a function is merely a pointer. Classes generally have the concepts of being able to hold data, as well as operations on that data.

提交回复
热议问题