简体   繁体   中英

What is exactly an object in C++?

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, and I can find that the term "object" has a more generalized meaning. I read a lot of materials on the net, but I could not yet find something clear/solid. May be I could not get to the correct place. I could get the standards and it has good paragraphs about this, but as you may know standard language is a bit difficult. and the information usually too scattered.

My question: would you please show me in a simple English What is an object in C++ outside the OOP world? or at least point me where I can find something good, concrete and simple to read about this.

AND Please if you downvote leave a comment. This is also a source to learn

The C++11 standard is pretty clear:

1.8 The C ++ object model [ intro.object ]

An object is a region of storage. [ Note: A function is not an object, regardless of whether or not it occupies storage in the way that objects do. — end note ]

That's it. An object is a chunk of memory in which data can be stored.

If you think about it OO or O bject O rientation makes more sense when you realize that in the old days the programs were organized around the functions which operated upon the objects (or data).

The term "object" was around long before object orientation .

What object orientation did was change the program organization from being organized around the functions to being organized around the data itself - the objects .

Hence the term object orientated .

Change of paradigm .

Here we see the paradigm shift from the old days:

struct my_object
{
    int i;
    char s[20];
};

void function(my_object* o)
{
    // function operates on the object (procedural / procedure oriented)
}

To what we have now:

struct my_object
{
    void function()
    {
        // object operates on itself (Object Oriented)
    }

    int i;
    char s[20];
};

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.

Not to bash on the existing answers, but they're missing an element (that's arguably a standard defect).

An object is a region of storage. [ Note: A function is not an object, regardless of whether or not it occupies storage in the way that objects do. end note ]

An object is created by a definition ([basic.def]), by a new-expression ([expr.new]) or by the implementation ([class.temporary]) when needed.

The properties of an object are determined when the object is created.

An object is a region of storage in which constuction has taken place . In fact, most of the time "object" refers to that constructed entity, with its value and state, whereas "storage" just means the memory (or whatever) it is written on.

The difference can be a simple detail:

// `s` names an object that has been constructed... somewhere.
// That storage will live exactly as long as necessary to back `s`
// as long as the object exists -- no need to worry about it.
std::string s = "hello";

// Using the object
std::cout << s << '\n';

But you can also (although it's very rarely useful) separate the object's lifetime from the lifetime of its storage:

// `storage` points at a chunk of... storage.
// It hasn't been initialized, nor does it have a type.
void *storage = malloc(sizeof(std::string));

// Now we constructed an `std::string`:
// we have an actual object inhabiting the storage!
std::string *s = new (storage) std::string("hello");

// Using the object, through the pointer we have
std::cout << *s << '\n';    

// Now we destruct the object: it exists no more.
s->~basic_string();

// Now we destroy the storage.
free(storage);

I must stress that this last example is for demonstration purposes only. It's a technique you probably won't encounter, and has been performed here with no error checking whatsoever. Don't try this at home :)

Now, how does it relate to the OOP "object"? Well... not at all. "Object" is a very generic term, and OOP founders just chose to use it as well, independently.

Refering to §1.8 of the c++ standard (N4618), an object:

  1. occupies a region of storage during its period of construction , through its lifetime and its period of destruction ;

  2. has a lifetime (for non trivial objects it start when initialization is completed and stop when the destructor starts);

  3. has a storage duration (static, dynamic, thread or automatic)

  4. has a type : the object type which is unique (strict aliasing).

  5. may have a name


About object type

(Other answers already have detailed the meaning of storage duration.)

The object type (or class) is a unique property of an object. The object type specifies the meaning of the region of storage occupied by the initialized object. So the meaning is unique, from a philosophical point of view, the object type is the species of the object, not its kind.

For the compiler , it only constrains the ensemble of operation that can be applied to the region of storage: the methods associated to the object type (in which case the type is defined by class or struct ), and all the functions taking the object as an argument (which are visible).

For the programmer , the type also specifies what will be the consequences of the application of a sequence of operations to the object during its lifetime . The type contains much more information than the compiler actualy may be able to know. For exemple after having checked that the size of an object, an_obj of type std::vector<int> is 0 , the programmer knows that an_obj.at(0) will always throw, the compiler may not.

In C++ world, an object is the instantiation of a class. It behaves (methods/functions) in certain ways and has attributes (data members) that depicts its state.

An object has lifetime. It is created (through constructor), it lives, and it dies (through destructor).

A class is like a blueprint, through which you define the behavior and attribute of an object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM