answersLogoWhite

0


Best Answer

A variable is simply a named value where the value is not constant -- meaning it can be changed at any time.

A pointer is a variable (or a constant) that can store a memory address and provides indirect access to that memory address. It is called a pointer because it is said to "point to" the memory address it contains. A pointer that doesn't actually point at anything should store the value zero (represented by the constant NULL). When you allocate memory dynamically, you must maintain at least one pointer to that memory in order to release the memory back to the system, which is achieved by deleting the pointer, and then nullifying the pointer, or pointing it to another allocation. Pointers can also be typed, such that the memory address being pointed is treated as if it were that type, thus a pointer to a char can be used to determine the value of each individual byte in a multi-byte value, such as an int, simply by incrementing its stored address to traverse the individual bytes. By the same token an array of 4 bytes can be treated as if it were a single 32-bit value, simply by pointing at the first byte using a pointer with type int. Note that a pointer can only physically point at one byte at a time, but its type allows you to access one or more contiguous bytes according to its type, thus incrementing the pointer's stored address will advance the pointer by the size of its type. Thus a pointer to int will automatically increment (or decrement) by sizeof(int) bytes.

Pointers can also point at other pointers, adding extra levels of indirection. This is most useful when you need to pass a pointer to a function by reference, since all pointers are always passed by value. However, passing a pointer to a pointer is the same as passing the pointer being pointed at by reference, which allows the pointer itself to be manipulated, not just what it points at. Pointer to pointer types are also useful when allocating large dynamic arrays as they allow the array to be allocated non-contiguously as a series of smaller, separate arrays. This uses more memory overall, but allocating many smaller arrays is far more likely to succeed where a large contiguous allocation could easily fail if a large enough block of free memory is not available.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

In essence there is no difference. The underlying implementation is exactly the same. A variable is simply a memory allocation, the size of which is determined by the variable's type. The type could be a primitive (built-in) type, like int or char, or a more complex data type such as a structure or indeed a class type. An object is nothing more than a specific instance of a class type. But all are accessed by the name we give them -- which is simply an alias for the memory we allocated to the variable. They may appear quite different on the surface, but all are implemented in the same way: as a class of object.

Even the primitive types are implemented as objects:

int X = int(12); // int assignment using int class constructor.

Although much of the implementation is hidden from us, X is, in fact, an object of class type int. All the standard operators (+, -, &, etc) and type casts are implemented as operator overloads, but besides the constructors that's about as much access as we get to the underlying object. There is no member operator (.) and we cannot derive classes from primitives, but they are a class of object nonetheless.

Prior to C++, primitives were, of course, implemented quite differently. But in an object-oriented environment like C++, it makes sense to implement them as objects.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is different between object and variable in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the difference between instantiation and initialization in C plus plus?

Instantiation is creating the instance of the variable/object . While Initialization is to provide the variable with some value. int i; // i is an instance of an integer i=10; //initialised with the value 10


What is persistent object in c plus plus?

A persistent object is an object that retains its value between two different executions of the program that uses it. This means that some kind of disk file is used to store and load the object, using a method of the object, such as "serialize", to do so.


What is the difference between constructor and friend function in c plus plus?

A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.


What is php plus plus?

PHP++ is an object-oriented version of the PHP programming language. ++ is used in programming to increment a variable by one so it means an improved version of PHP.


What is the variable of x plus 10 plus 4x-35?

What is the variable of x plus 10 plus 4x-35?


Difference between array and pointers in c plus plus?

A pointer is simply a variable that can store a memory address and has the same purpose in both languages. The only real difference is that C++ pointers can point at objects (instances of a class) and indirectly invoke their methods, whereas pointers in C (which is not object oriented) cannot.


Objects in c plus plus and how are they created?

An object is a component of a program that knows how to perform certain actions and to interact with other pieces of the program.it is created as following...... An object is an instance of a class. For object read variable! Every time we define a variable we get a new object which has all the properties of the associated class. This is the same as with the built-in types of C (float, int etc.)-they have certain properties (range of values, operations etc), the bonus is that we can make up.


How do you define a symbol such as plus or - as a variable in python?

Use a character variable. For example: plus = '+' minus = '-' You can now refer to these symbols using the variable names "plus" or "minus".


What is the difference between an attribute and a behavior in c plus plus?

An attribute is a class member variable while a behaviour is a class member method.


What does a constant do in c plus plus?

A constant is a variable that does not change. The correct term is constant variable.


What is the answer to a variable squared plus ten times a variable plus twenty-four in?

a2/2


What is the variable in this equation 7y plus 10 equals -6 plus 14?

y is the variable