Pointers which point to an Object?

Hi,

I am totally new to C++ but OF seems to be a nice introduction to it. So far I only programmed in AS3.

It’s a really stupid question but I don’t get it:

  
  
for (int i=0; i<size; i++){  
		  
		p[i] = new Particle();		  
		*p[i].moveTo(i*40,200);  
		  
  
}  
  

What I thought would happen is that *p[i] would get the value of the memorylocation, which is the newly created Particle. But the error I get is:

Error:request for member ‘moveTo’,which is of non-class type ‘Particle’

What I am doing wrong?

Thanks for the help!

Joris

Hi,

I am new to C++ as well, but I will give it a try to help on this subject, not sure how you declare the p array but try this

  
for (int i=0; i<size; i++) {  
      p[i] = new Particle();        
     *p[i]->moveTo(i*40,200);  
}  

Hope that helps
rS

[quote author=“nardove”]Hi,

I am new to C++ as well, but I will give it a try to help on this subject, not sure how you declare the p array but try this

  
for (int i=0; i<size; i++) {  
      p[i] = new Particle();        
     *p[i]->moveTo(i*40,200);  
}  

Hope that helps
rS[/quote]

Hi nardove,

it seems to do the trick because it tries the ‘moveTo’ function. But then an error occurs:

error: void value not ignored as it ought to be

That is weird because in my Particle.h I have the Public function

void moveTo(float xpos,float ypos);

in the Particle.ccp file the function looks like this:

void Particle::moveTo(float xpos,float ypos){

x=xpos;
y=ypos;

}

Feeling stupid and a newbie all over again :oops:

Your code looks fine, now how is the p array and the x, y variables in the Particles class declared?

rS

Particle.h

class Particle {

private:
float x; // current y position
float y; // current x position

int color;
int radius;

public:
Particle();

float targetX;
float targetY;

void update();
void draw();
void moveTo(float xpos,float ypos);

};

EasingLines.h (this is where I want my particles to live in)

#define size 5

class EasingLines : public ofBaseApp{

private:

int centerX;
int centerY;
float rotation;

public:

void setup();
void update();
void draw();

Particle* p[size];
};

Easinglines.cpp this is where the particles are made

for (int i=0; i<size; i++){

p[i] = new Particle();
//*p[i]->moveTo(i*40,200);
*p[i]->test();

}

Thanks a million for helping me out!

Well, that looks good to me, apparently the error means a function is declared to return nothing (void) but you are assigning the return code to a variable.

But I dont see it, maybe a more trained eye can spot the error.

In which line of the code you get it?

rS

[quote author=“nardove”]Well, that looks good to me, apparently the error means a function is declared to return nothing (void) but you are assigning the return code to a variable.

But I dont see it, maybe a more trained eye can spot the error.

In which line of the code you get it?

rS[/quote]

The error occurs here :

*p[i]->moveTo(i*40,200);

So that means that the -> is setting a value to the *p[i]? Which should not be the case because I only want the newly created Particle at *p[i] to fire it’s own function moveTo(xpos,ypos).

whaa, learning new languages is a b#tch

Yes, I dont get it, this is out of my scope, sorry

I am interested to see a solution, so I hope some one else jump in and share some light of wisdom for us mortals :stuck_out_tongue:

Cheers
rS

the *, in addition to representing a pointer is also a “dereference operator” so you can use it two ways:

int * position;
(create a variable position which is a pointer to an int)

*position = 5;
(turn position, which is a pointer, into the thing it points to… ie, get rid of the reference)

here:

  
  
//*p[i]->moveTo(i*40,200);  
*p[i]->test();  
  

you are saying turn p[i], which is a pointer to a particle int a particle, but when you use the -> you are referring to it as a pointer. ie:

  
pointer = ->   
non pointer = .  
  

so you can write what you’ve written two ways:

  
(*(p[i])).test();      
// we use the dot, since (*(p[i])) is a particle, not a pointer to a particle.   
  
p[i]->test();  

(added parentheses to make it more clear how the operator is working)

does that help explain?

take care,
zach

Thanks!

I totally understand that now :slight_smile:

I’ll post the result, thanks a lot for helping out

Just to complement all the nice examples given above:
http://www-numi.fnal.gov/offline-softwa-…-ction.html

Also, remember that a variable declared as an array in C++ is actually a pointer to the start of the array. It’s just a specialized type of pointer that provides the convenient [] syntax so you don’t have to do strange things such as: (myArray+sizeof(int)); to access it

This is why you need to put the array accessor inside parenthesis before dereferencing it *(p[i]).doStuff(); Otherwise you are dereferencing the array itself instead of the actual array element … In my experience, most people generally don’t bother with the dereferencing syntax, though and prefer the arrow accessor of p[i]->doStuff(); … it’s quite a bit nicer to read and less typing.

It is also important to remember that arrays are just specialized pointers when you are passing an array as a function parameter… Arrays are always “pass by reference”. Meaning that if you change the array parameter within the function, it will change it in the calling function as well.

Thank you guys!
It’s working now.

One thing I don’t get is the difference between :

WORKING

  
void EasingLines::update(){  
  
	for (int i=0; i<size; i++) {  
		  
		Particle par =*p[i];  
		  
	  
		(*p[i]).targetX=mouseX;  
		(*p[i]).targetY=mouseY;  
		  
		(*p[i]).update();  
  
	}  
  
}  

versus
THE TARGETX AND TARGETY ARE NOT SET BUT UPDATE FUNCTION IS TRIGGERED

  
void EasingLines::update(){  
  
	for (int i=0; i<size; i++) {  
		  
		Particle par =*p[i];  
		  
	  
		par.targetX=mouseX;  
		par.targetY=mouseY;  
		  
		par.update();  
  
	}  
  
}  

in the first example, you’re changing the targetX/Y vars of the particle in the i position. in the second one you’re making a copy of that particle and changing targetX/Y in the copy not in the original.

So if I am understanding this correctly:

  
 Particle par =*p[i];  

creates a new instance of Particle with the properties(same x,y,targetX,targetY etc.) of the particle that was created in p[i]?
So in C++ you will never use a variable as a reference of a *pointer?

Thanks for the support!

creates a new instance of Particle with the properties(same x,y,targetX,targetY etc.) of the particle that was created in p[i]?

Yes, that’s correct.

  
  
So in C++ you will never use a variable as a reference of a *pointer?  

Well, sometimes you do use this…but as long as you remember that its a copy and not the original.

and usually if you want to store a reference to a pointer then you use another pointer:

  
  
Particle * par = p[i];  
par->targetX=x;  
...