passing class members to function in c++

Hi there. I have been trying to get my head around this all week with no success. I have a procedure in an SFML program, that basically bounces a shape around the window.

This works fine. What I want to do is incorporate this code into a function so I can call it with the name of a different shape.

The code part in question is as follows
note: direct and down are bool expressions to control direction.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
if(direct)
	{
				
				if(x<1200)
				{	
					for(d=0;d<delay;d++)
					{
					}
					x++;
					circ.setPosition(250,350);
					}
				else 
				{	
					if(x>1200)
					{
					std::cout<<"\ncollision right\n";
					direct=false;
					}
				}

	}
	if(down)
	{
				if(y<700)
				{
					for(d=0;d<delay;d++)
					{
					}
					y++;
					circ.setPosition(250,350);
				}
				else 
				{	
					if(y>700)
					{
					std::cout<<"\ncollision bottom\n";
					down=false;
					}
				}	
			
	}
	if(!down)
	{
				if(y>10)
				{
					for(d=0;d<delay;d++)
					{
					}
					y--;
					circ.setPosition(250,350);
				}
				else 
				{	
					if(y<10)
					{
					std::cout<<"\ncollision top\n";
					down=true;
					}
				}
				
				
	}



	if(!direct)
	{
				if(x>10)
				{
					for(d=0;d<delay;d++)
					{
					}
					x--;
					circ.setPosition(250,350);
				}
				else
				{
					if(x<10)
					{
					std::cout<<"collision left\n";
					direct=true;
					}
				}
	}


What I need to do is set it into a function (eg void Bonce('new shape') and input the name of a new shape as a parameter, so, in the case shown circ.setPosition(,x,y) would become 'new shape'.setPosition(x,,y).

Am I just being incredibly stupid or is this really as difficult as it seems?
void Bonce( shape & );
"class members"? I'm not quite sure what you mean with that.

What is a 'circ'? It apparently is a variable of some type T, and that T has member function void T::setPosition(int,int) (or equivalent).

Who did create circ? Could that same party have more of those objects?

1
2
3
4
5
6
7
8
9
10
11
12
class Foo {
  void func( int );
};

void stranger( Foo & victim )
{
  victim.func( 42 );
}

...
  Foo theMen[3];
  stranger( theMen[0] );

One way of the solving the problem would be to use polymorphism. In fact, shapes are one of the classic examples -- for polymorphism -- in C++ tutorials.

Have you already come across polymorphism?

Andy

PS I assume that circ is an instant of type Circle (or similar). Does it already have a base class?
Sorry if I did not explain my query very well. 'circ' is a member of a class in sfml which is constructed by using: sf::CircleShape circ(20), where circ is the name of this particular instance and (20) is the radius.

I can create further circles by using different names instead of 'circ'.

What I basically need is a method for passing a new name into a function, as described. The code I have shown above is, at present, inline and it is not a very good idea to have to repeat the whole code for each instance if, for instance, I wished to use 4 circles.

Any advice will be appreciated as I appear to have confused myself even more trying to work out how to do it!

Thanks in advance.
sf::CircleShape circ(20); does, by syntax, construct a variable with name 'circ' and type 'sf::CircleShape'. The '20' is a value passed to the constructor. In the previous posts there are examples that have functions taking a latameter of type as a reference.
Sort of like this?

1
2
3
4
5
6
7
8
void MoveCircle( sf::CircleShape& circ ) // make it a const ref is possible
{
    // snip

    circ.setPosition(250,350); // circ is whatever cir you passed in

    // snip
}


where it's called like this

1
2
3
4
MoveCircle(circ1);
MoveCircle(circ2);
MoveCircle(circ3);
// etc 


Andy

PS as cf::Circle derives from cf::Shape, you could code the helper function using Shape rather than Circle so you can bounce mixed shapes?
Last edited on
Thanks all.

Andy I tried your suggestion . It certainly moves the shape but only once, not for a continuous movement like the inline code does. I printed out the x and y coordinates to check. I will carry on trying, at least you've given me a start.
Just to let everyone know - thanks for all your advice, I have now got the whole function working. Thanks again
Topic archived. No new replies allowed.