Object meaning

What is an object in terms of variables, I know that a object with an identifier is known as a variable, but what is an object?
Well, have you heard of classes in c++?

YES: an object is just a general term for an instance of a class

NO: a class is basically like a form, it has a bunch of variables in it and you make copies of it and fill in the variables for that copy. The copies are the objects.

WTF THIS ISN’T HELPFUL: just look at this then, (structs and classes are basically the same thing in this sense) http://www.cplusplus.com/doc/tutorial/structures/

That doesn’t relate to my question in the slightest bit.
How so?
The "yes" part of his answer did actually answer one definition for what an object is -- an instance of a class. However, in C++ the notion of an object is actually more generalized than what you might find in Java or other OOP-only languages. Also your tone could be better.

If you want to know positively exactly what an object is in C++ terms, look no further than the C++ standard!
https://github.com/cplusplus/draft/blob/master/papers/n4659.pdf
4.5 The C++ object model [intro.object]
1 The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is
created by a definition (6.1), by a new-expression (8.3.4), when implicitly changing the active member of a
union (12.3), or when a temporary object is created (7.4, 15.2). An object occupies a region of storage in its
period of construction (15.7), throughout its lifetime (6.8), and in its period of destruction (15.7). [ Note:
A function is not an object, regardless of whether or not it occupies storage in the way that objects do.
— end note ] The properties of an object are determined when the object is created. An object can have a
name (Clause 6). An object has a storage duration (6.7) which influences its lifetime (6.8). An object has a
type (6.9). Some objects are polymorphic (13.3); the implementation generates information associated with
each such object that makes it possible to determine that object’s type during program execution. For other
objects, the interpretation of the values found therein is determined by the type of the expressions (Clause 8)
used to access them

If that piqued your interest, read more starting on page 9.

But look. It's similar to the other thread you have. An object is a entity that we give meaning to. It's a way of defining a type, be that a built-in type or a complex class. An object contains data, functionality, or perhaps has nothing in it at all (only described by its type). An object may contain other subobjects, or contain functions that do things or manipulate the state of that object.

An object is something that is created (e.g. an instance of a class, as highwayman said), and has a lifetime. For example, you could say that two objects are created with the following line:
 
int* ptr = new int[100];

The first object created is the dynamic object which is an array of size 100.
The second object created is the ptr, which stores a handle that the array of size 100 is accessed from.
And the array itself has 100 objects contained in it, one for each array element.

Also note, that while ptr might have a name, the object which it points to doesn't have a name. It just is an array, located starting at some memory address, with a dynamic lifetime.
Last edited on
a class is a user defined type made up of smaller things … other classes or simple types. A class contains both data storage and subroutines that operate on the data you stored. The subroutines can spawn their own variables as well.

an object is a variable of the type defined by the class. Some folks refer to any variable as an object, eg an integer is an object to them, this is a (harmless but confusing) terminology issue caused by other languages that actually define integers as objects (python, etc, with the expected horrid performance by doing so).

See the answer on variable for what happens below this level.
Last edited on
Variable and object are the same thing.

It is called object because like real object it has properties.

It is called variable because the value you assigned the object with can be modified unless you make it constant.

So, declaring a string variable is the same as declaring an string object.

The properties of a string object are basically the functions you used to manipulate the string object such as you can output the string, get the size of the string, combined strings etc.





Last edited on
dexter1224 wrote:
Variable and object are the same thing
not quite:
1
2
3
4
5
6
7
// these two lines of code create one object (with type int and value 1)
// and two variables (n and r) which both name that one object
int n = 1;
int& r = n;
// this line of code creates one object (with type string and value "abc\n"),
// but no variables
std::cout << std::string("abc\n");
Last edited on
Topic archived. No new replies allowed.