|
-
Nov 22nd, 2002, 12:34 AM
#1
Thread Starter
Addicted Member
Pointers
I need some pointers on pointers (chuckle)
I have an object declared on the free store:
Obj *myObj = new Obj;
if i passed this pointer to a function like:
blah(Obj *pointer);
would this create a new instace of myObj on the free store? Or would it just create another pointer that points to it? If so, would those to pointers be identical in that they both pointed to the same spot in memory?
Also, I would like to store a pointer to this object in another class, and i was wondering what the syntax would be: I've been trying, but i've been getting so many errors i can't make heads or tails of it. This is waht i've done:
class Obj2 {
public:
void setPointer(Obj *&pObj);
private:
Obj *pointer;
};
void Obj2::setPointer(Obj *&pObj) {
(*pointer) = pObj;
}
Obj2 *myObj2 = new Obj2;
myObj2->setPointer(myObj);
Would this create a direct pointer to myObj in Obj2? Or would it create a pointer to the pointer of myObj? What i basically want is just a pointer in myObj2 that is a copy of the pointer to the object myObj. I hope this makes sense.
thanks
-
Nov 22nd, 2002, 07:23 AM
#2
Fanatic Member
Re: Pointers
Originally posted by jmiller
I need some pointers on pointers (chuckle)
I have an object declared on the free store:
Obj *myObj = new Obj;
if i passed this pointer to a function like:
blah(Obj *pointer);
would this create a new instace of myObj on the free store? Or would it just create another pointer that points to it? If so, would those to pointers be identical in that they both pointed to the same spot in memory?
The pointer in function blah is the same as the original pointer. They both point to the same object.
Also, I would like to store a pointer to this object in another class, and i was wondering what the syntax would be: I've been trying, but i've been getting so many errors i can't make heads or tails of it. This is waht i've done:
class Obj2 {
public:
void setPointer(Obj *&pObj);
private:
Obj *pointer;
};
void Obj2::setPointer(Obj *&pObj) {
(*pointer) = pObj;
}
Obj2 *myObj2 = new Obj2;
myObj2->setPointer(myObj);
Would this create a direct pointer to myObj in Obj2? Or would it create a pointer to the pointer of myObj? What i basically want is just a pointer in myObj2 that is a copy of the pointer to the object myObj. I hope this makes sense.
thanks
You're trying to pass a pointer of an object passed by reference. I don't think this will work. If you want just the pointer, remove the ampersand (&), and it should work.
In that case you'll pass the pointer of the object. The pointer is then copied.
Code:
void Obj2::setPointer(Obj *pObj) {
pointer = pObj;
}
-
Nov 22nd, 2002, 09:10 AM
#3
The ampersand would work, but it isn't necessary.
(*pointer) = pObj;
doesn't work because (*pointer) is of type (Obj) while pObj is of type (Obj *).
pointer = pObj;
will work but it only creates a copy of the pointer. If you actually want to copy the object you'd have to do
pointer = new Obj(*pObj);
but that will only work if you have explicitly defined a copy constructor (a constructor with the syntax
Obj(const Obj &other);
that copies all data of the object).
Don't forget to delete the new object.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 22nd, 2002, 07:24 PM
#4
Thread Starter
Addicted Member
Check out this code, it keeps giving me errors (in case your wondering, its for a game called "Llama Racing"):
class Track {
public:
Track();
~Track();
void Draw(Llama *cLlama);
void Draw();
void setNumLanes(int value) {numLanes = value;}
int getNumLanes() {return(numLanes);}
void setLaneWidth(int value) {LaneWidth = value;}
(error) void PlaceBet(int llama, long money, User *pUser);
private:
int numLanes;
int LaneWidth;
long curBetMoney;
int curBetLlama;
(error) User *curBetPUser;
};
Track::Track() {}
Track::~Track() {
cout << "Track Deleted\n";
(error) delete *curBetPUser;
curBetPUser = 0;
}
void Track: raw(Llama *cLlama) {
GotoXY(0,0);
for (int curLane = 0; curLane < numLanes; curLane++) {
cout << (curLane + 1) << "||";
for (int x = 0; x < LaneWidth; x++) {
if (cLlama[curLane].getTrackPos() == x) cout << "L";
else cout << "-";
}
cout << "||\n\n";
}
}
void Track: raw() {
GotoXY(0,0);
for (int curLane = 0; curLane < numLanes; curLane++) {
cout << (curLane + 1) << "||";
for (int x = 0; x < LaneWidth; x++) cout << "-";
cout << "||\n\n";
}
}
(error) void Track::PlaceBet(int llama, long money, User *pUser) {
curBetLlama = llama;
curBetMoney = money;
curBetPUser = pUser;
delete pUser; pUser = 0;
}
/**************************************************************************************************** ***
**************************************************************************************************** ***/
class User {
public:
User();
~User();
void setTMoney(long value) {TotalMoney = value;}
long getTMoney() {return(TotalMoney);}
void Bet(int llama, long money);
private:
long TotalMoney;
int BetIndex;
};
User::User() {
TotalMoney = 100;
BetIndex = 0;
}
User::~User() {cout << "User Deleted\n";}
void User::Bet(int llama, long money) {}
The errors all have to do with the pointer to the User that placed the bet (in the Track class). By the way the User is declared on the free store. I want the Track to have a pointer to the User so it can handle the bet after the race is done, without any outside calls (outside of the track).
-
Nov 22nd, 2002, 07:52 PM
#5
Thread Starter
Addicted Member
nevermind all that, the error was because the user class needs to come first, before the track class. What if i needed to reference to the track class in the user class, and then the user class in the track class? You couldn't have both of them come first...Is there a way to prototype classes?
-
Nov 22nd, 2002, 08:22 PM
#6
Monday Morning Lunatic
You just go class Llama; Simple statement.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|