What is exactly an object in C++?

前端 未结 5 1370
花落未央
花落未央 2020-12-17 20:33

In the beginning of my journey learning C++, I thought an object is an OOP-only related term. However, the more I learn and the more I read, I can see that this not the case

5条回答
  •  悲哀的现实
    2020-12-17 21:08

    Short answer

    From https://timsong-cpp.github.io/cppwp/n3337/intro.object

    An object is a region of storage.


    A slightly longer answer

    In traditional OOP and OOD, an Object is used to describe class of objects some times and to an instance of a class some times.

    In C++, class and struct represent classes.

    An object in C++ can be an instance of a class or a struct but it can also be an instance of a fundamental type.

    A few simple examples:

    int i;
    

    i is an object. It is associated with a region of storage that can be used by the program.

    struct foo { int a; int b;};
    foo f;
    

    f is an also object. It is also associated with a region of storage that can be used by the program.

    int* ptr = new int[200];
    

    ptr is a pointer that points to 200 objects of type int. Those objects are also associated with a region of storage that can be used by the program.

提交回复
热议问题