• Create Account

## C++ difference between variable and object

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1noatom  Members

Posted 26 February 2013 - 03:09 PM

So I was reading Accelerated c++ and there is a statement like:

"A variable is an object that has a name".

That made me go into fog.

So if I have a class:

class x

{

public:

int i;

}

and i create:

x here;

is here called an object or a variable? According to accelerated c++ it's a variable,but what is an object then?!

### #2ApochPiQ  Moderators

Posted 26 February 2013 - 03:11 PM

An object is a specific instance of a class. A variable is just a piece of data. It can be a number, some words, or an object.

Your "here" is both an object (instance of the x class) and a variable (the "name" for that instance).
Wielder of the Sacred Wands

### #3SiCrane  Moderators

Posted 26 February 2013 - 03:22 PM

A variable is just a piece of data. It can be a number, some words, or an object.

No, in C++ the term "variable" refers to named objects. Not every piece of data is a variable; only declared object are variables. "Just a piece of data" would be what C++ calls "entities". In this case "here" is the name for an object, so it is both an object and a variable. If you had Object obj = new Object;. obj would be a variable and what obj points to would be an object, but it wouldn't be a variable.

Edited by SiCrane, 26 February 2013 - 03:22 PM.

### #4King Mir  Members

Posted 26 February 2013 - 03:41 PM

SiCrane has the right of it.

In the OP example, "here" is a variable. It represents an object. So it is both.

EDIT: made consistent with frob's quoting of the standard below.

Edited by King Mir, 26 February 2013 - 07:20 PM.

### #5frob  Moderators

Posted 26 February 2013 - 03:50 PM

Some of those are not quite right, in subtle ways. An object does not need to have a name. An object is not necessarily a variable.

Since this gets into the language-lawyer side of things, let's go strait to the language standard:

• A name is a use of an identifier ... that denotes an entity or label.
• ... [it does not meet the definition of a label, therefore it is an entity] ...
• Every name that denotes an entity is introduced by a declaration. ...
• A variable is introduced by the declaration of a reference other than a non-static data member or of an object. The variable's name denotes the reference or object.
• An object is a region of storage. ... An object is created by a definition, by a new-expression or by the implementation when needed. The properties of an object are determined when the object is created. An object can have a name.

So as SiCrane pointed out, "here" is a name, and it denotes an entity.  That entity is a variable, and it is an object, all at the same time.

Edited by frob, 26 February 2013 - 03:51 PM.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

### #6NightCreature83  Members

Posted 26 February 2013 - 04:41 PM

Ah so in this case:
Object obj = Object();
Object* obj1 = new Object();

Stuff that is being created on the RHS are objects and the stuff on the LHS are variables, right?

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max

### #7King Mir  Members

Posted 26 February 2013 - 07:59 PM

No. That's just a different syntax for variable declarations.

Assuming that code goes in a function, that declares 2 objects on the stack(an object of type Object, and one of type pointer to Object), and one on the heap (of type Object). It also declares two variables, that denote the two stack objects. So obj and obj1 are variables, that denote specific and different stack objects.

Contrast that example with this:
Object obj;
Object &obj1(obj);
This declares just one object, but two variables. The second line declares a reference to the first object, which makes it not an object declaration; it technically doesn't declare anything on the stack. obj and obj1 refer to the same object.

I say it technically doesn't declare on the stack, because it can be useful to think of a reference as syntax for a restricted kind of pointer. From that point of view, obj1 implies the declaration of a pointer object. So two stack objects are created, but any use of the second variable will access the object represented by obj. Also, under this view, the second object (the pointer) is surely optimized out when compiled.

Either way of describing this code is consistent with the behavior of C++, and won't mislead, but the former is the wording used by the standard, and therefore technically more correct. I hope I'm not confusing the issue by explaining thoroughly. If this and the previous paragraph confuse more than help, they can be jointly ignored.

### #8warnexus  Prime Members

Posted 26 February 2013 - 10:28 PM

Here is an example:

You can think of a variable like a box that stores one thing. The data that this box or variable can store depends on what the data type is. The way you know how to refer to a variable or know what the variable is, is by its name.

Example:

Dog sparky = new Dog();

sparky would be the variable. sparky is of type Dog.

An object is an instance of a class. What that means is that object is an example of a class.

For example: Dog is an instance of the class Pet. Cat is an instance of the class Pet.

Instance are examples. Dog is an example of a Pet. Cat is an example of a Pet.

### #9MrDaaark  Members

Posted 26 February 2013 - 10:46 PM

As Simply as possible.

A variable is an unknown value that can and will change during the execution of a program. It varies. It's given a name as an alias over an address or offset. The name is used for convenience, since we don't need to know or care what the address offset is.

So something like

int32 Number;

Means that at some memory address, there are 32 bits(4 bytes) of data that represent a whole number.

So when you write

int32 Number2 = Number - 16;

The program has to go to that memory address to read the value of 'Number' before it can complete the operation.

Constants are data that does not vary. They are known values.

So when you write

const int32 Number = 16;
int32 Number2 = Number - 16;

The program ends up just doing 16 - 16 because in this case 'Number' is a known value, and it gets replaced with '16' at compile time. There is nothing to look up.

A class is just a data structure layout. A data structure can be any combination of variables, constants, pointers, and even other data structures.

class Player
{
int posX;
int posY;
int health;
etc...
}

It's just a definition. In order to use a class, you have to create an instance of it.

Player PlayerOne;
Player PlayerTwo;

PlayerOne and PlayerTwo are now objects. Since the main idea behind C++ is to use class definitions to model objects and their behaviors, we call it object oriented programming.

### #10King Mir  Members

Posted 26 February 2013 - 11:27 PM

warnexus and Daaark are talking about the definition of a object in the context of object oriented programming, which is an abstract way to think about and design code. That's not the definition that sentence is using. It is related, because OOP objects are C++ objects, but not all c++ objects need to be treated as OOP objects.

### #11Matt-D  Members

Posted 27 February 2013 - 05:59 PM

A memory is a set of words, each with an address and a content. The addresses are values of a fixed size, called the address length. The contents are values of another fixed size, called the word length. The content of an address is obtained by a load operation. The association of a content with an address is changed by a store operation. Examples of memories are bytes in main memory and blocks on a disk drive.

An object is a representation of a concrete entity as a value in memory. An object has a state that is a value of some value type. The state of an object is changeable. Given an object corresponding to a concrete entity, its state corresponds to a snapshot of that entity. An object owns a set of resources, such as memory words or records in a file, to hold its state.

See:

http://cpp-next.com/archive/2009/11/%E2%80%9Celements-of-programming%E2%80%9D-chapter-1-foundations/

Also, this time from "C++ Primer":

Terminology: What is an Object?

C++ programmers tend to be cavalier in their use of the term object. Most
generally, an object is a region of memory that can contain data and has a
type.

Some use the term object only to refer to variables or values of class types.
Others distinguish between named and unnamed objects, using the term

variable to refer to named objects. Still others distinguish between objects
and values, using the term object for data that can be changed by the
program and the term value for data that are read-only.

In this book, we’ll follow the more general usage that an object is a region
of memory that has a type. We will freely use the term object regardless of
whether the object has built-in or class type, is named or unnamed, or can

### #12King Mir  Members

Posted 27 February 2013 - 07:55 PM

There is a problem with defining an object as something in memory -- it implies objects cannot be in registers. Now you can program in C++ without knowledge of registers, but eventually one's likely to run into the term. A register is like a memory location, except instead of being represented by a pointer, it is represented by an identifier in assembly language. There are few registers, but using them is faster than using memory. C++ uses registers implicitly, so it's not something that's important to remember when learning to program.

Defining an object as something that has an address is even worse, because you cannot take an address of an rvalue/temporary, but rvalues are objects. An rvalue is a value returned by a function, as a result of a cast, constructor call, or similar operation. For primitive types, and in all of C, an rvalue can only be found on the right side of an assignment operation, thus the name.

So with these two points in mind it's better to say that an object "has storage."

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.