New Posts  All Forums:Forum Nav:

Pointer and C++

post #1 of 8
Thread Starter 
I'm learning C++ and have an issue with pointers I can't seem to figure out.

Basically I want a point to point to another point that is a object.

I have a class called Ball, and have these two pointer:

Ball* cueBall;
Ball* balls;

balls = new Ball[16];
balls[15].setColor(WHITE); //cue ball

cueBall = &balls[15];

I wanted the pointer cueBall to point to the Ball object held in balls[15].

I use to have cueBall be its own object, but I'd like it better if it was in the array of Balls and have the pointer cueBall to be able to refer to it.

While I don't get a compiler error with the above code, the cue ball is not being displayed on the scene the same as it did went it was its own object.

Thanks for any help.
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
Reply
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
Reply
post #2 of 8
I'm kinda rusty with pointers but did you try it without the &? I would test but I don't have a compiler installed.
post #3 of 8
Thread Starter 
That didn't work.
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
Reply
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
Reply
post #4 of 8
The way you have it is correct. You just have to dereference cueball to use it. balls[15] is now equivalent to *cueball
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #5 of 8
Thread Starter 
Thought I was. I have some other strange anomaly going on then or something. As it doesn't work like it did before the change, and the only difference is there's a pointer to it now.

Thanks for the help though.
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
Reply
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
Reply
post #6 of 8
cueBall = &balls[15];
in this line, all the '&' sign does is pass by reference. The same thing you can do with a pointer...

The reason for pointers, is so you can keep track of the address location of whatever memory in an ordered fashion. Like if you have an array of 15 cueballs, you'll have a pointer array, something like this:

*cueBalls[balls] // balls being the thing that determines the size of the array...

The more excepted syntax is to place the * sign to 'attached' to the object variable.

like this:

Ball *cueBall;
Ball *balls;

This line looks really strange:

balls[15].setColor(WHITE)I'd like to see the class for this. Depending on how you have your public and private access, will effect how this will work.

You may have to set up a 'scope' to get to this data correctly....

I'd like to take a look at the whole app and see how you have yours class setup.

Are you running a pointer array? or a static array??
good enough
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6600 3ghz Gigabyte GA-EP45-DS3L 9800GT 2x2gb fatility DDR2 1066 
Hard DriveOSMonitorMouse
2 x 74gb raptor raid0 win7 64bit Samsung 245BW Razer 3G diamondback 
Mouse Pad
glass core pad 
  hide details  
Reply
good enough
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6600 3ghz Gigabyte GA-EP45-DS3L 9800GT 2x2gb fatility DDR2 1066 
Hard DriveOSMonitorMouse
2 x 74gb raptor raid0 win7 64bit Samsung 245BW Razer 3G diamondback 
Mouse Pad
glass core pad 
  hide details  
Reply
post #7 of 8
Quote:
Originally Posted by JoBlo69 View Post
cueBall = &balls[15];
in this line, all the '&' sign does is pass by reference. The same thing you can do with a pointer...
&<variable> means "address of <variable>." It creates a pointer. The line is equivalent to
Code:
Ball *cueBall=&balls[15];
A reference to balls[15] would be created by
Code:
Ball& cueBall=balls[15];
The way he has it is correct
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #8 of 8
ahh. thanks for clarifying... I still have issues with figuring out pointers, and pas by reference, pas by value...

I'm working on a school assignment with pointer arrays... Not fun.
good enough
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6600 3ghz Gigabyte GA-EP45-DS3L 9800GT 2x2gb fatility DDR2 1066 
Hard DriveOSMonitorMouse
2 x 74gb raptor raid0 win7 64bit Samsung 245BW Razer 3G diamondback 
Mouse Pad
glass core pad 
  hide details  
Reply
good enough
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6600 3ghz Gigabyte GA-EP45-DS3L 9800GT 2x2gb fatility DDR2 1066 
Hard DriveOSMonitorMouse
2 x 74gb raptor raid0 win7 64bit Samsung 245BW Razer 3G diamondback 
Mouse Pad
glass core pad 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming