C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old March 8th, 2003, 09:59 AM
etones etones is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 236 etones User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 35 sec
Reputation Power: 6
Sending an object refrence?

Hi, to cut down on some code im wanting to pass an object reference to a
function (well, thats what I think im needing todo

Anywho, for instance, have a look at the following example. Hope it gives
the jist of what im trying todo, im unsure if how todo it. In PHP strings
can be re-interpretted as code using an 'eval' command, hence, in the
example below, if 'variable' was moveNorth and somebool was "evalled" php
would see 'bool somebool = newclass.moveNorth();'

PHP Code:
 void somefunction(string);

int main()
{
    
ClassName newclass(val1val2)

    
somefunction("moveNorth");

    return 
0;
}

void somefunction(string variable)
{
    
bool somebool newclass.variable();



Any help on what I need todo to pass object references? All help
appreciated.

Cheers
Taz

Reply With Quote
  #2  
Old March 8th, 2003, 10:34 AM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 26
are you trying to pass newclass object in the somefunction() function? i think that is what u mean, i believe all you have to do is this:

somefunction( string somestring, newclass &newclassVariable)

Reply With Quote
  #3  
Old March 8th, 2003, 01:34 PM
etones etones is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 236 etones User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 35 sec
Reputation Power: 6
Hi, thanks for the reply.

where you say &newclassVariable, do you mean one of the methods, eg moveNorth ?

Lastly, what do I need to put into the prototype?

void somefunction(string, what here?)

Cheers
Taz

Reply With Quote
  #4  
Old March 8th, 2003, 01:52 PM
etones etones is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 236 etones User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 35 sec
Reputation Power: 6
quick reply, im a c++ newb

Just done some digging and it appears you are passing something by reference, so, I just need to know exactly how to pass a function by reference, if anyone would be so kin

Cheers
Taz

Reply With Quote
  #5  
Old March 8th, 2003, 02:09 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
Hi,

The & symbol indicates a reference, and all you need to do is indicate what type your variable is a reference to. If your class name is Animal then you would declare an Animal variable like this:

Animal dog;

You can declare a reference to dog like this:

Animal& rover = dog;

A reference is just an alias for a variable. Your function prototype would look something like this:

void somefunction(string a_string, Animal& an_animal);

I like to separate the variable name from the type with a space:

so,

Animal& any_name

reads "any_name is an Animal reference". Others like to write it like this:

Animal &an_animal

but there's no difference.

Why do you need references? If you you pass-by-value to a function, i.e. just using a name and type, a temporary copy of the original object is made for the function, and any changes you make to the object in the function will only be to the temporary copy, which is destroyed once the function returns. The result is that when your program returns from the function the original object remains unchanged. With references, no copy of the original object is made for the function, so you're dealing with the object itself and any changes you make are permanent.

Last edited by 7stud : March 9th, 2003 at 01:33 AM.

Reply With Quote
  #6  
Old March 8th, 2003, 02:27 PM
etones etones is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 236 etones User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 35 sec
Reputation Power: 6
Hi 7stud, thanks for the reply. Ok, things are looking a little cleaer, however im still a little confused, i'll show you what i have now, hopefully 1 reply should fix it.

Side note. I have a class called Monkey with methods for moving the monkey north, east south and west, they are:

moveNorth
moveSouth
moveWest
moveEast

ok, my stripped down code:

PHP Code:
 void movement(Monkeya_movement);

int main()
{
  
Monkey bubbles();

  
movement(moveNorth// is this correct??

}

// im unsure what to write here
void movement(What do I put in here??)
{
  
// how do i know run bubbles.moveNorth etc from in here?



Right, think i've addressed what im needing help with

Cheers for any replies,
Taz

Reply With Quote
  #7  
Old March 8th, 2003, 02:35 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
Hi,

The function declaration/prototype and the function header in the definition are essentially the same thing, so if you've written your function declaration like this:

void movement(Monkey& a_movement);

then your function header in your definition has to reflect the same thing:

void movement(Monkey& a_movement)
{
//definition here
}

However, somthing doesn't seem right. Your variable name a_movement does not appear to be a variable of type Monkey. a_movement appears to be a function name.

Last edited by 7stud : March 9th, 2003 at 01:34 AM.

Reply With Quote
  #8  
Old March 8th, 2003, 02:40 PM
etones etones is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 236 etones User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 35 sec
Reputation Power: 6
Hi, when you say variable of monkey, what exactly do you mean?

Im wanting to pass an object reference to a function and a method.

I know this isnt correct, but im hoping it will explain what im trying todo better, so in the main program i could write:

int main()
{
....
movement(bubbles.moveNorth)
...
}

the function movement would then run bubbles.moveNorth somewhere within its code. But this is the big but, would it be possible to change bubbles to another instance of the money class, eg monkey2 and change moveNorth to one of the other methods?

Cheers
Taz

Reply With Quote
  #9  
Old March 8th, 2003, 02:42 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
Hi,

You don't indicate that you have a function named "movement", so the compiler is going to indicate it has no idea what you mean.
Your function names are moveNorth(), moveSouth(), etc.

So, you want something like this:


//void movement(Monkey& a_movement);
//change to:

void moveNorth(Monkey& a_monkey);

int main()
{
Monkey bubbles();

// movement(moveNorth) // is this correct? **No** you don't have a function named movement: your function is called moveNorth()

moveNorth(bubbles);


}

// im unsure what to write here
void movement(What do I put in here??)
{
// how do i know run bubbles.moveNorth etc from in here?
}

Reply With Quote
  #10  
Old March 8th, 2003, 02:47 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
"Im wanting to pass an object reference to a function and a method."

Ok, I see what you want to do. As a beginner to C++, you are trying to do things that may be too complex, however they can be done. I don't think there is such a thing as a reference to a function. You can pass a pointer to a function as an argument to a function.

Reply With Quote
  #11  
Old March 8th, 2003, 02:50 PM
etones etones is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 236 etones User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 35 sec
Reputation Power: 6
hi, i think im getting an understanding of whats going on.

What I wanted was to be able to create a new function in my main program (which was movement) which could be called and sent 2 things, an object name and a method, it would then run the method for the object

PHP Code:
 void movement(object_namemethod_to_run);

int main()
{
   
Monkey bubbles();
   
movement(bubblesmoveNorth);
}

void movement(object_namemethod_to_run)
{
   
bool worked object_name.method_to_run;



However I do belive this is not possible now?

Reply With Quote
  #12  
Old March 8th, 2003, 03:07 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
Hi,

I'm not sure why you would want to do that, but it's possible.
It's slightly complex notation, so I don't know if you'll understand it, but here is how you declare a pointer to one of your functions:

//function declaration:
void moveNorth(Monkey& a_monkey);

//pointer declaration:
void (*pfunction1) (Monkey& a_monkey);

That creates a pointer called pfunction1 that can point to functions that have return types of void and take parameters of type Monkey& (i.e references to Monkey).

//assign function to pointer:
pfunction1 = moveNorth;

Then you would declare your movement() function like this:

movement(string a_string, void (*psome_function) (Monkey& a_monkey);

and you would call your movement() function like this:

movement("some text", pfunction1);

You would need to declare pointers for all your movement functions, e.g.:

pfunction2
pfunction3,
etc.

Last edited by 7stud : March 8th, 2003 at 03:14 PM.

Reply With Quote
  #13  
Old March 8th, 2003, 03:19 PM
etones etones is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 236 etones User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 35 sec
Reputation Power: 6
excellent stuff! Thanks for taking the time to make it.

Ok, I just need to know where to place the stuff, is the following correct.

1) Currently in my header file:

...
bool moveNorth();
...

to replace with

...
bool moveNorth(Monkey& a_monkey);
...

2) Pointer declaration
void (*pfunction1) (Monkey& a_monkey);

This goes in the header file too?

3) Assigning a value to the pointer
pfunction1 = moveNorth;

Goes in the main program. Change where needed.

4) movement function
movement(string a_string, void (*psome_function) (Monkey& a_monkey);

Would the the above be the same for the prototype and the actual function?

Lastly, in the actual function, what do I write to execute objectname.method?

Thanks for the reply!
Taz

Reply With Quote
  #14  
Old March 8th, 2003, 03:38 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
1) yes

2) No. Declaring a pointer is the same as declaring an integer variable like this:

int a_number;

so, that would go in your main program.

3)yes

4) A prototype and function header are essentially the same thing. A prototype placed before your main program alerts the main program that the function exists when it comes across the function name, and not to freak out and give you a bunch of errors because it doesn't know what the name is. You don't have to have function prototypes--you can just define the function before your main program. However, there could be a problem if you do that. If you have function "a" that refers to another function "b", then you have to put "b" first, so the compiler has seen the name "b" before it is referenced in "a". But, what if each of the functions calls the other? That's why you have function prototypes--you just list all your function's prototypes at the top of your file and you don't worry about the order or anything else, and then in your function definitions somewhere below the prototypes the compiler will have already seen all the names that can be referenced, so you won't get an error.

Prototypes don't have to be indentical to the function header. With function prototypes, you can just list the types with no names:

void a_function(int, int);

and the variable names in the prototype and function header don't have to be the same:

void a_function(int a, int b);

void a_function(int number1, int number2)
{
return number1 + number2;
{

(but the variable names in the function definition have to agree with the variables in the function header). I think it's easiest to follow the code if you make the prototypes and function headers exactly the same.

Last edited by 7stud : March 8th, 2003 at 04:45 PM.

Reply With Quote
  #15  
Old March 8th, 2003, 03:47 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
There's something else you're probably going to need to know: how to use the function passed to movement(). In the definition of movement(), you would do this:

pfunction1(monkey1);

You just use pfunction1 in place of the function name.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Sending an object refrence?