Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    236
    Rep Power
    13

    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
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    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)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    236
    Rep Power
    13
    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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    236
    Rep Power
    13
    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
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    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 02:33 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    236
    Rep Power
    13
    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
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    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 02:34 AM.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    236
    Rep Power
    13
    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
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    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?
    }
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    "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.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    236
    Rep Power
    13
    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?
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    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 04:14 PM.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    236
    Rep Power
    13
    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
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    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 05:45 PM.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    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.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo