Thread: Passing "this" as function parameter

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    7

    Passing "this" as function parameter

    Hello there,

    I'm trying to pass a reference to a current class as a parameter of a method of another class without success.

    Say in class foo I call a function in class bar and pass a reference to foo as a parameter.

    Here is the prototype in bar:
    Code:
    void someFunction(foo & reference);
    Here is the call to the function in foo:
    Code:
    myBar.someFunction(this);
    Why is this not allowed? How can foo pass a reference to itself to bar?

    Thanks for your help.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    this is a pointer. someFunction expects a reference. I suppose dereferencing this might work.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    7
    Quote Originally Posted by anon View Post
    this is a pointer. someFunction expects a reference. I suppose dereferencing this might work.
    I'm afraid not. The whole point of this is that I need either a reference of a pointer to the calling class... and even if I change my prototype to the following I get the same problem.

    Code:
    void someFunction(foo * reference);
    Last edited by pgavigan; 07-13-2007 at 09:35 AM.

  4. #4
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Exactly what errors are you getting? Dereferencing this should work in your original case. Your problem may lie elsewhere.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    7
    The following error keeps poping up, and it points to my function prototype.

    error C2061: syntax error : identifier 'foo'
    Last edited by pgavigan; 07-13-2007 at 09:37 AM.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    May-be paste some code?

    Has the compiler seen declaration of foo() before you try to call it?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by pgavigan View Post
    The following error keeps poping up, and it points to my function prototype.

    error C2061: syntax error : identifier 'foo'
    I reckon you need a forward declaration.

    Try putting this before class bar:
    Code:
    class foo;
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    7
    I have tried that, and with no luck!

    Here are some complete classes:
    Code:
    class something {
    public:
    	something(void);
    	~something(void);
    
    	void someFunction(void){
    		awful it;
    		it.someFunction(this);
    	}
    };
    And another class:
    Code:
    class awful {
    public:
    	awful(void);
    	~awful(void);
    
    	void someFunction(something & reference) {
    		cout << "Something awful happened";
    	}
    };

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    7
    Wow, I figured it out!

    The compiler demands that the prototype have "const" in the parameter list.

    Strange.

    Thanks for all your help.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    And you get an error about identifier foo()?

    Firstly, you'll need to pass *this. Secondly, you may need to move awful before something and/or separate method declarations from implementations.

    Edit:
    The compiler demands that the prototype have "const" in the parameter list.

    Strange.
    Yes, that is strange, because in awful and something there isn't anything that demands the use of const.

    Posting real code helps...
    Last edited by anon; 07-13-2007 at 09:52 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Assuming these are in the same file, arrange them like so:

    Code:
    class something;
    
    class awful {
    public:
    	awful(void);
    	~awful(void);
    
    	void someFunction(something & reference) {
    		cout << "Something awful happened";
    	}
    };
    
    class something {
    public:
    	something(void);
    	~something(void);
    
    	void someFunction(void){
    		awful it;
    		it.someFunction(*this);
    	}
    };
    This should work for this silly example. However, if someFunction actually does something with reference, the code for someFunction needs to be moved down so that it is below class something { /* blah */ };

    This sort of thing is best arranged across multiple files. I don't know if you're doing that or not, but if not you should consider it.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  12. #12
    Registered User
    Join Date
    Jul 2007
    Posts
    7
    Here is the working code:

    Code:
    class something;
    
    class awful {
    public:
    	awful(void){}
    	~awful(void){}
    
    	void someFunction(const something * reference) {
    		cout << "Something awful happened";
    	}
    };
    Code:
    class something {
    public:
    	something(void){}
    	~something(void){}
    
    	void someFunction(void){
    		awful it;
    		it.someFunction(this);
    	}
    };
    Thanks again for your help.

  13. #13
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    I don't see any reason why that would need const. What compiler are you using?
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yep, doesn't this really compile?

    Code:
    #include <iostream>
    using std::cout;
    
    class something;
    
    class awful {
    public:
    	awful(void){}
    	~awful(void){}
    
    	void someFunction(something *reference) {
    		cout << "Something awful happened";
    	}
    };
    
    class something {
    public:
    	something(void){}
    	~something(void){}
    
    	void someFunction(void){
    		awful it;
    		it.someFunction(this);
    	}
    };
    
    int main()
    {
        something s;
        s.someFunction();
        std::cin.get();
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  15. #15
    Registered User
    Join Date
    Jul 2007
    Posts
    7
    Quote Originally Posted by AverageSoftware View Post
    I don't see any reason why that would need const. What compiler are you using?
    You're right... my bad, it looks like all that it was complaining about was the lack of a forward declaration. I must have mixed up my tests .

    It is strange though that it needs a forward declaration like that... I haven't mentioned it in my posts up to now, but I have my code in separate files with "#include" references to the other files at the top. Shouldn't the "#include" take care of any requirements for forward declarations?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem passing double array as a function parameter
    By pppbigppp in forum C++ Programming
    Replies: 7
    Last Post: 06-06-2006, 03:08 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. passing counters between function
    By BungleSpice in forum C Programming
    Replies: 18
    Last Post: 02-21-2004, 06:16 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 2
    Last Post: 03-31-2002, 12:34 AM