Thread: Managed C++, passing arguments by reference

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    42

    Managed C++, passing arguments by reference

    Hi there,
    To quickly ilustrate my problem, say I got a ref class that's creating a window, with some variable as well as handle to second window which should modify mentioned variable, eg:

    Code:
    public ref class Window1 : public System::Windows::Forms::Form
    {
      private:
        int var;
        Window2^ win;
    // ...
    }
    ;

    And now, how to pass reference to var to some Window2 method or constructor?
    When I'm doing it this way:

    Code:
    Window2::someMethod(int& ref);
    Window1->Window2->someMethod( var );
    I'm getting this error

    Code:
    An object from the gc heap (member of a managed class) cannot be converted to a native reference
    The error message itself explains what's going on quite much, but for some reasons I couldn't find answer how to solve it (or maybe I got it all wrong).

    (I know I could use ->get() method as for single variable, but in real program I'm using more variables, that's why I thought references might come in handy... Maybe I should wrap them in some class and try to work with passing pointers? But still, how to do it using references is what's bugging me.)

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    630
    Just pass ints by values since there isnt much difference in preformance.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    Well, I need original values to be modified, and passing by value won't help then. Question that remains unaswered is, how to pass managed class data by reference, if that's even possible. (as for now I wrapped my parameters into class and I'm passing regular class pointer, which works but I don't think it's nice solution).

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by jimzy View Post
    Well, I need original values to be modified, and passing by value won't help then. Question that remains unaswered is, how to pass managed class data by reference, if that's even possible. (as for now I wrapped my parameters into class and I'm passing regular class pointer, which works but I don't think it's nice solution).
    I'm not up-to-date on all this "managed" crap but it seems like you can't really pass a "managed" variable by reference into unmanaged code, because the unmanaged code could change it and it would no longer be "managed."

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    Yes, it seems so. However, second window class (as in my example) is supposed to be managed too. Thing is, managed objects cannot by passed by native C++ reference (&) (even to managed class' own method [I know it's no point doing so, but still]), at least according to what I googled. Instead I should use some tracking references, handles, interior pointers and more which made me really confused and I didn't go much deeper into this... x.x Guess I'll just stick to pointers.

    Cheers.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by jimzy View Post
    Yes, it seems so. However, second window class (as in my example) is supposed to be managed too. Thing is, managed objects cannot by passed by native C++ reference (&) (even to managed class' own method [I know it's no point doing so, but still]), at least according to what I googled. Instead I should use some tracking references, handles, interior pointers and more which made me really confused and I didn't go much deeper into this... x.x Guess I'll just stick to pointers.

    Cheers.
    Why not copy the value into an unmanaged integer, pass a reference to that, then copy the resulting value back into the managed variable? I have no idea if that makes any sense -- like I said, I don't really know much about managed code.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    Quote Originally Posted by brewbuck View Post
    Why not copy the value into an unmanaged integer, pass a reference to that, then copy the resulting value back into the managed variable? I have no idea if that makes any sense -- like I said, I don't really know much about managed code.
    That's what I'm doing now, was just hoping somebody knows more "managed" solution to such problem :-)

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Haven't looked at the facilities of C++/CLI closely, but wasn't there some way to fix gc-references and thus get a native reference/pointer to that thing?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    Quote Originally Posted by CornedBee View Post
    Haven't looked at the facilities of C++/CLI closely, but wasn't there some way to fix gc-references and thus get a native reference/pointer to that thing?
    Not sure if that's what you mean, but I run into something that solves the whole reference problem, let's say:

    Code:
    public ref class Foo 
    {
       int^ var;
       public:
          Foo() 
          {
              var = gcnew int(10);
          }
          void modify(int^% ref)
          {
              ref = 20;
          }
    };
    // just a sample code, I know I could pass by value
    This does what regular '&' in native C++ would do, so seems like that's what I've been looking for.

    Edit: oh, forgot to add (just in case somebody has similar problem and wants to go into details)
    ^ is gc managed heap object handle and
    % is tracking reference

    Cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing arguments
    By jcafaro10 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2009, 01:14 PM
  2. Passing arguments between functions
    By Wiretron in forum C Programming
    Replies: 4
    Last Post: 05-17-2006, 04:59 PM
  3. passing by reference
    By AngKar in forum C Programming
    Replies: 4
    Last Post: 04-27-2006, 09:31 PM
  4. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  5. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM