🔒 Locked
Pointer Object
Advertisement
Original Post
hi mates, May u help me what's the pointer Object realy means? For example if CAT be a class i can define an object with to way inlude #"cad.h" void main(){ CAD a; CAD *b; } ///// What's the real differece between these to objects.Anyway i know about the syntax of them (for I.E b->method name()) but i have some conceptual problem on it.And does b object use heap memory ?! Regards Mantana
The asterisk makes b a pointer; wheras a is just a variable. b will point to a memory address, instead of holding a value.
An important difference is that pointers are pass by reference, and variables are passed by value.
If something is passed by value, e.g. to a function, etc. then that function actually makes a copy of that variable. It does what it needs to, and returns a new value - but the variable remains unchanged.
If it is passed by reference ( like *b), then the memory address of the variable is passed, and so if changes are made to that variable in the function, then those changes carry over to the actual original pointer variable. No copy is made.
Mind you, I only have a basic undestanding of pointers myself - but this is what I gather is one of the more important concepts in regards to pointers.
An important difference is that pointers are pass by reference, and variables are passed by value.
If something is passed by value, e.g. to a function, etc. then that function actually makes a copy of that variable. It does what it needs to, and returns a new value - but the variable remains unchanged.
If it is passed by reference ( like *b), then the memory address of the variable is passed, and so if changes are made to that variable in the function, then those changes carry over to the actual original pointer variable. No copy is made.
Mind you, I only have a basic undestanding of pointers myself - but this is what I gather is one of the more important concepts in regards to pointers.
# include "cad.h"void main(){CAD a;CAD *b;}The variable a saves memory for the CAD object. The variable p is a pointer to an object. It's really the same with any pointer. A pointer is a variable for an adress which points to some other variable (be it a int, string or any object).
The operator Pointer->Method() is a short version of (*Pointer).Method().
If you want to save memory for an object at runtime you would use the operator new. If you use b = new CAD() memory for one instance of CAD will be save on the heap. Make sure you use delete if you use new.
#include <iostream>using namespace std;class Cat {public: Cat() { cout << "Cat is born!" << endl; } ~Cat() { cout << "Cat leaves us..." << endl; } void cry() { cout << "miau, miau" << endl;}};int main() { cout << "Cat program started" << endl; Cat Jerry; Cat *Tom; cout << "Jerry says: "; Jerry.cry(); cout << "Tom says: "; // Tom.cry(); // <-- not possible //Tom->cry(); (*Tom).cry(); // <-- same as Tom->cry(); return 0;}/* OUTPUTCat program startedCat is born!Jerry says: miau, miauTom says: miau, miauCat leaves us...*/
alright, i'll try to explain this, though generally, i'm not so great at teaching...anyhow, a pointer is a variable that contains a memory address. it doesn't have to be on the heap, it can be any memory address.
CAD a;
this instantiates a CAD object called a.
CAD * b;
this creates an uninitialized pointer to CAD object. the value it contains is undefined.
let's say CAD a is stored at memory location 0xDEADBEEF. that means &a equals 0xDEADBEEF
therefore the code:
b = &a
will store the location of a in b, leading to b containing 0xDEADBEEF. b now points to a.
now lets say we create another CAD instance called c:
CAD c;
we'll say c was created at memory location 0xDEADBEA7. now assigning c's location to b:
b = &c
causes b to contain 0xDEADBEA7, so now it points to c.
not sure if that was helpful at all, but i tried :)
[Edited by - workingclass77 on September 3, 2006 4:59:13 AM]
CAD a;
this instantiates a CAD object called a.
CAD * b;
this creates an uninitialized pointer to CAD object. the value it contains is undefined.
let's say CAD a is stored at memory location 0xDEADBEEF. that means &a equals 0xDEADBEEF
therefore the code:
b = &a
will store the location of a in b, leading to b containing 0xDEADBEEF. b now points to a.
now lets say we create another CAD instance called c:
CAD c;
we'll say c was created at memory location 0xDEADBEA7. now assigning c's location to b:
b = &c
causes b to contain 0xDEADBEA7, so now it points to c.
not sure if that was helpful at all, but i tried :)
[Edited by - workingclass77 on September 3, 2006 4:59:13 AM]
Well, well, well... pointers... THE biggest feature of C. If you don't get those right, you will crash your new program sooooo many times that you will consider droping programming alltoghether. My suggestion is finding a greath book that explains them at theyr best and following that book to the letter.
However, a quick explain. For every variable you are using in your program (ok, minus the ones that the compiler decides to promote to register vars and the ones that itreplaces with theyr value - but just pretend that I didn't say that) the computer needs to store them in memory. However... where exactly in memory it's the computer's job to decide. Think about the memory as a big array of "cells", each containing a byte (8 bits). More likely, 4 billion of those cells (the theoretical maximum amont of memory available on 32 bit sistems - 4GB of ram). Whenyou decide you need a variable, the computer fids out where it has the necesarry free space in memory o store your variable and stores it, at an... "address" which is no more that a number that represents the cell at which it starts to store your variable (most variables take more than one cell - byte). (OK, actually it is not like that at all, the vars you usully use are stored in the stack, BUT for understanding pointers, just believe in everything i''ve said till now).
Pointers... well, they are exactly that; numbers that tell us where exactly in memory a variable is stored. Actaually, the compilers attaches to those pointers additioal information, meaning hw many cells a variable that is stored at that address ocupy in memory. SOOOOOOOOOOOOO when you write
CAD a;
you request the computer to make for you an object of type CAD and make it avaiable to your program under the name a. Then you can say a.doSomething() and it will work. However, when you write
CAD *b; //or CAD* b; or CAD*b; - it doesn't matther where you put the star
you mearely tell the computer that you want to store an ADRESS at which you will have an object of type CAD. THE COMPUTER DOES NOT CREATE THE CAD OBJECT, but mearely lets you "remember" where you put an object. SO:
you cannot just do b->doSomething(). When you do this, the computer calls the doSomething() function, and, in that function, whenever you modify any member variable from the CAD class, it will go WHEREVER in memory b is "pointing to" and modify values. This is BAD, because usually at first it wil have a random value, so you will end modifying memory at random places and it will eventually crash your program. DON'T DO THAT.
However, you can do the following;
b=&a
this will store in b the address of variable a, declared earlyer. After that,both b and a will reffer the same piece of memory, so in fact they are both diffrent access ays of the same object in memory. So whenever you modify members of a object, or the object that b is pointing to, you modify the same object. After that, you can do a.doSomething(0 or b->doSomething, and it will have the same effect.
LATER EDIT: while I was typing that, the two guys posting before me have posted quite at the same time, so some inf may overlap. Hope it actually hels you by having the same thing explained diffrent ways. But: GRAB A GOOD BOOK.
Hope it helps,
Juian!
However, a quick explain. For every variable you are using in your program (ok, minus the ones that the compiler decides to promote to register vars and the ones that itreplaces with theyr value - but just pretend that I didn't say that) the computer needs to store them in memory. However... where exactly in memory it's the computer's job to decide. Think about the memory as a big array of "cells", each containing a byte (8 bits). More likely, 4 billion of those cells (the theoretical maximum amont of memory available on 32 bit sistems - 4GB of ram). Whenyou decide you need a variable, the computer fids out where it has the necesarry free space in memory o store your variable and stores it, at an... "address" which is no more that a number that represents the cell at which it starts to store your variable (most variables take more than one cell - byte). (OK, actually it is not like that at all, the vars you usully use are stored in the stack, BUT for understanding pointers, just believe in everything i''ve said till now).
Pointers... well, they are exactly that; numbers that tell us where exactly in memory a variable is stored. Actaually, the compilers attaches to those pointers additioal information, meaning hw many cells a variable that is stored at that address ocupy in memory. SOOOOOOOOOOOOO when you write
CAD a;
you request the computer to make for you an object of type CAD and make it avaiable to your program under the name a. Then you can say a.doSomething() and it will work. However, when you write
CAD *b; //or CAD* b; or CAD*b; - it doesn't matther where you put the star
you mearely tell the computer that you want to store an ADRESS at which you will have an object of type CAD. THE COMPUTER DOES NOT CREATE THE CAD OBJECT, but mearely lets you "remember" where you put an object. SO:
you cannot just do b->doSomething(). When you do this, the computer calls the doSomething() function, and, in that function, whenever you modify any member variable from the CAD class, it will go WHEREVER in memory b is "pointing to" and modify values. This is BAD, because usually at first it wil have a random value, so you will end modifying memory at random places and it will eventually crash your program. DON'T DO THAT.
However, you can do the following;
b=&a
this will store in b the address of variable a, declared earlyer. After that,both b and a will reffer the same piece of memory, so in fact they are both diffrent access ays of the same object in memory. So whenever you modify members of a object, or the object that b is pointing to, you modify the same object. After that, you can do a.doSomething(0 or b->doSomething, and it will have the same effect.
LATER EDIT: while I was typing that, the two guys posting before me have posted quite at the same time, so some inf may overlap. Hope it actually hels you by having the same thing explained diffrent ways. But: GRAB A GOOD BOOK.
Hope it helps,
Juian!
Quote:
Original post by Mr Safety
An important difference is that pointers are pass by reference, and variables are passed by value.
I'm sorry if this comes of nit-picky, perhaps you have just chosen the wrong wording, but in fact, pointers are really pass-by-pointer and references are pass-by-reference, even though it is true that you avoid making a copy in both cases and they generally behave similarly. Nevertheless, there are differences between the two. Generally references are always safer, since you can neither reassign, nor new/delete them.
Also (not directly related, but I think this is appropriate :)), a very simple advice that saved me a lot of headache: Declarations are read from right to left. i.e.:
Cat* pCat; // Pointer-to-CatCat& rCat; // Reference-to-CatCat*& omgCat // Reference-to-Pointer-to-CatI also second that you should get a good book/online resource, specifically pointers etc should be covered quite extensively since it's really so confusing :)
Here is a 2nd version of the above Cat program, showing the use of new. Here is a question: why is it that in the first version only one cat leaves us?
// 2nd version of Cat// shows the use of new#include <string>#include <iostream>using namespace std;class Cat {public: Cat() { cout << " cat is born!" << endl; } ~Cat() { cout << name << " leaves us..." << endl; } void give_name(string given_name){ name = given_name; } void cry() { cout << name << " says: " << "miau, miau" << endl;}private: string name;};int main() { cout << "Cat program started" << endl; Cat Jerry; Jerry.give_name("Jerry"); Cat *Tom = new Cat; (*Tom).give_name("Tom"); Jerry.cry(); // Tom.cry(); // <-- not possible Tom->cry(); //(*Tom).cry(); // <-- same as Tom->cry(); delete Tom; // delete Jerry; <-- not possible // Jerry leaves us automatically return 0;}/* OUTPUTCat program started cat is born! cat is born!Jerry says: miau, miauTom says: miau, miauTom leaves us...Jerry leaves us...*/
@yabba_dabba: not sure if it was intentional for your first example or not, but Tom is never assigned a value. Need a Tom = &Jerry in there somewhere.
thx all
When i use for example
CAT* a;
a points to a class or points to an object which i don't know it's address ?
When i use for example
CAT* a;
a points to a class or points to an object which i don't know it's address ?
Quote:
Original post by workingclass77
@yabba_dabba: not sure if it was intentional for your first example or not, but Tom is never assigned a value. Need a Tom = &Jerry in there somewhere.
Yes, exactly. But still we get the output. As LordJulian has said one shouldn't do this. (*Tom).cry() produces the output "miau, miau", but we don't have access to memory so it is a leak.
Quote:
Original post by bigmantana
thx all
When i use for example
CAT* a;
a points to a class or points to an object which i don't know it's address ?
Err, perhaps just read the posts above (again). A class is something quite different than an object. A class is an abstraction of all objects. Sokrates was the first to introduce this concept to human thought, he called the idea. Aristotle expanded this concept to classes and objects. He noted that not only we have an idea of a horse, but we can specify properties and behaviours of the horse.
For example if you see a cat that is certain cat. If you see a lot of cats you can abstract from properties of the particular cats to form an idea about CATS in general. The general idea of a cat is the class of cats or the idea of a cat. It is a represenation of all properties which are common in all cats. A class has properties and behaviours. In C++ for this we have the class keyword. Properties are the members and behaviours the methods of a class.
class CAT {// behaviours miau(); walk();// properties size; race;};CAT a_particular_cat;Second, of course you know the adress, you have it right there, it is the pointer. I would suggest you take a good C++ book and do some exercises and/or join the C++ learning group.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement