11

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?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Kamal Zidan
  • 474
  • 2
  • 9
  • Something that has an address in memory. –  May 12 '17 at 21:56
  • 1
    read, for example, http://en.cppreference.com/w/cpp/language/object – Stephan Lechner May 12 '17 at 21:59
  • Wow, Great source. How come I did not come to this page before?! Thanks a million @Stephan Lechner. – Kamal Zidan May 12 '17 at 22:03
  • @KamalZidan.. Anonymous downvotes are sth you have to get used to if you want to spend some time in SO. Just ignore them and go on (unless they are more than 1 Downvote/Sec then there is sth horribly wrong with your question) – Humam Helfawi May 12 '17 at 22:06
  • Thanks @Humam Helfawi. This was a great thing to know. – Kamal Zidan May 12 '17 at 22:19
  • A region of storage with associated semantics. this is from isocpp super faq. from element of programming: Object: A representation of a concrete entity as a value in memory. An object is changeable and has a computer-specific implementation. – chedy najjar May 12 '17 at 22:27

5 Answers5

8

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 Object Orientation 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];
};
Galik
  • 47,303
  • 4
  • 80
  • 117
  • 2
    That's a much broader definition than I would have expected from C++. It even would apply to uninitialized memory. – Mark Ransom May 12 '17 at 22:01
  • @MarkRansom "uninitialized" or "unused"? As far as the standard (and the abstract machine it describes) goes, unused memory doesn't exist at all. – Quentin May 12 '17 at 22:03
  • I mean for example something returned by `malloc`. – Mark Ransom May 12 '17 at 22:04
  • @MarkRansom mmh, true. – Quentin May 12 '17 at 22:08
  • 1
    Maybe every object is a region of storage, but not every region of storage is an object ;) – rwols May 12 '17 at 22:10
  • 1
    You could consider a `malloc`'d region of storage as an object IMO. Maybe not a particularly useful one, but you would still work with it just like every other object. The only difference between a `void*` and `myclass*` is that `myclass*` has something useful I can do with it. I could still work with a block of memory just the same, but maybe not in an object-oriented manner. –  May 12 '17 at 22:24
  • 1
    @rwols According to the standard, even uninitialized storage is referred to as an object. – Galik May 12 '17 at 22:28
  • 1
    Due to its incompleteness this answer is realy close to be wrong. Referring to the same paragraph of the standard one can read that an object: occupies a **region of storage**, has a **life time**, has a **storage duration**, has a **type** -> the **object type** (which is unique). It also has a periode of construction and a period of destruction. – Oliv May 13 '17 at 06:41
7

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.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
7

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.

Community
  • 1
  • 1
Quentin
  • 62,093
  • 7
  • 131
  • 191
  • 2
    "An object is a region of storage in which constuction has taken place." - That would seem to imply that given `int n;`, then `n` is not an object. –  May 12 '17 at 22:27
  • 2
    @NeilButterworth it is: it's a default-constructed `int`. Construction does not imply initialization for trivially-constructible types :) – Quentin May 12 '17 at 22:28
  • @Quentin In the case of `int n;` nothing has taken place in the region occupied by `n`, construction or otherwise. –  May 12 '17 at 22:39
  • 1
    @Galik I'm struggling with my sentence. I just mean that both groups (C++ and OOP) had to name two completely different concepts, and they both settled on "object" because they found it fitting. Hence, a vocabulary collision, made more probable by the vagueness of "object" as a word. If you can come up with better wording, feel free to edit :) – Quentin May 12 '17 at 22:42
  • 1
    @NeilButterworth Maybe I'm reading this wrong, but [dcl.init] (https://timsong-cpp.github.io/cppwp/n3337/dcl.init#6) provides a bullet point precisely about (airquotes) "non-initializing initialization". – Quentin May 12 '17 at 22:48
  • @Galik I'm admittedly a bit rusty on my OOP, but I seem to recall that an "object" there is a modeled entity, along with its state, associated functions, invariants, etc. -- much more than just a piece of storage. IOW, a dumb `int` is a C++ object, but not an OOP object. – Quentin May 12 '17 at 22:50
  • In the case where `n` is a local variable, some space for it is allocated on the stack when the function containing it starts, and that's it. –  May 12 '17 at 23:08
  • 4
    @NeilButterworth of course nothing is actually *done* at the machine level in an optimized build, but this is still conceptually a construction. Debug runtimes may also initialize it to some recognizable value. – Quentin May 12 '17 at 23:13
  • note that based on [p0593](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p0593r6.html) an object can be also be created _in some cases_ using malloc. See also: [this SO answer](https://stackoverflow.com/a/61999151/2085626) – Amir Kirsh May 25 '20 at 09:47
5

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.

Oliv
  • 17,610
  • 1
  • 29
  • 72
-5

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.

zam
  • 7
  • 5
    Unlike other languages, "object" in C++ does not necessarily refer to an instance of a class. An `int` is an object. – Quentin May 12 '17 at 22:31