Difference between Object and instance : C++

前端 未结 5 765
傲寒
傲寒 2020-12-16 05:39

I followed a number of posts on SO, and finally I can draw a conclusion that when we have something like :

Person name;

name

5条回答
  •  伪装坚强ぢ
    2020-12-16 06:22

    "Object" and "instance" are almost interchangeable. In C++, an object is formally any region of storage. "Instance" is not a formally defined term, but we typically refer to "instances of type X", most commonly used with class types.

    Foo f;
    

    This declaration creates an object named f. The object's type is Foo. You could say the object f is an instance of Foo.

    Your attempt to distinguish the terms was incorrect. The two things you've actually pointed out are two different ways of creating objects.

    Person name;
    

    In this case, we're creating an object name of type Person.

    Person* name = new Person();
    

    In this case, we're creating an object name of type Person* (pointer to Person). We are also creating another object of type Person using the expression new Person(). This expression returns a pointer, which we are initialising the name object with.

提交回复
热议问题