Reputation: 27
This is an educational question:
If I have created a class
class bank_account
And in the main function, I declared
bank_account *pointer = new bank_account();
Then I am initializing variables such as follows
(*pointer).account_name ="Random Name";
My confusion is what is happening here because I usually declare an object with a NAME, not a pointer, if that object is a pointer, and a pointer is just some variable which holds an address to a variable. What does it mean if a pointer is declared as an object and what it is actually representing? Is the pointer to an object is referring to an invisible object?
Upvotes: 0
Views: 354
Reputation: 27864
A pointer is a variable that holds a memory address, and it exists wherever it is pointing to something that makes sense or not, meaning you can declare this pointer and not necessarily instance an object for it to point to, it can just point to nullptr
which generally signifies the object does not exist at that moment. This alone is useful. You can use it as place holder or to keep track of the program's state.
Another property is that it can point to an array of objects, so you may use it to create a dynamic number of objects at once instead of just one or a predetermined number of objects.
But the most important property is that the object you instance with new
does not belong to that particular scope, if the function ends it will not be automatically deleted. This object can be created in a subroutine and then exist throughout the program's life or until you delete
it, and all you have to do to pass this object around is pass it's pointer, which is a quite small piece of data compared to doing something silly like copying the object around, this is huge for performance.
You have to pay attention to memory leaks though. Since this object is not deleted automatically, you have to do it yourself when necessary, otherwise the longer the program runs, more memory it will use until it runs out of it.
You can also have multiple pointers pointing to the same place, which is very useful when iterating through linked lists, arrays and all sorts of structures, so a pointer's purpose doesn't necessarily have to be that of holding a specific object, but that of a tool to browse data in memory efficiently.
Upvotes: 0
Reputation: 238331
and a pointer is just some variable which holds an address to a variable
Correction: A pointer can point at any object; Not necessarily a variable. Variables have names. There can be objects that are not directly named by a variable such as sub-objects, temporaries, and objects in dynamic storage.
In your program for example, the expression new bank_account()
creates an object in dynamic storage.
What does it mean if a pointer is declared as an object
It's really unclear what you mean by "declared as an object". If you declare a pointer to have the type bank_account*
, it means that it can point at an object of type bank_account
, which happens to be a class.
If you declare a variable to have a pointer type, then the object named by the variable is a pointer.
and what it is actually representing?
A pointer represents the address of an object. Besides containing an address of an object, it can also have the null pointer value (which points to no object) or it can have an invalid value (an address that may have contained an object, but that object no longer exists).
Then I am initializing variables such as follows
(*pointer).account_name ="Random Name";
To be pedantic, this technically does not initialise a variable. Initialisation is performed on objects when they are created. This member variable has been created earlier and this expression assigns a value to it. But if the variable is previously uninitialised, then colloquially speaking, it would not be terribly wrong to talk about initialisation.
when I declare an object as pointer what is the pointer pointing to?
In your example program, pointer
points to an object that was created in dynamic storage, using the keyword new
.
In general, pointer points at some object whose address is stored in the pointer, or a pointer might not point at an object at all (invalid, or null value).
You said an object is created
Yes. The new-expression creates an object in dynamic storage.
but I declared a pointer
Yes. You did.
so the pointer is pointing to the object?
You've initialised the value of the pointer with the result of the new-expression. The pointer points at the object that was created in dynamic storage.
and what is the name of that object then?
Objects do not have names. However: Variables do have names, and variables are associated with an object, so one could colloquially say that those objects associated with a variable have a name. But objects in dynamic storage are not named by a variable.
Upvotes: 3
Reputation: 12354
a pointer is a variable which contains an address of another variable. Any pointer uses space in memory needed to keep the address. on 64-bit platforms it usually needs 8 bytes.
When you create an class object, it also is resided in memory and occupies as many bytes as it needs. The pointer gets assigned a value of the address of this class object.
bank_account *pointer = new bank_account();
The above declares a pointer to the object of type bank_account
. new
allocates space for the object in the memory and returns its address. It also calls a constructor of the class. The address returned by the new
gets assigned to the pointer variable named pointer
. Later you can use it to access the object as
(*pointer).account_name ="Random Name";
or equivalently
pointer->account_name ="Random Name";
pointer is just an address. Pointer type is just a syntactic sugar which allows the compiler to do its job and to provide you with useful information about your program.
Upvotes: 1