Results 1 to 7 of 7

Thread: Pass by reference

  1. #1
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Pass by reference

    How to force a class to pass by reference like how java does
    (using C++)??

    Thanks

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Pass by reference

    This article should make it clear. Feel free to ask if still in doubt.
    J-P Nurmi

  3. #3
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pass by reference

    Quote Originally Posted by jpn View Post
    This article should make it clear. Feel free to ask if still in doubt.
    Hello jpn,
    You didnt get me.

    Let me explain...
    -->in normal C++
    Qt Code:
    1. class A
    2. {
    3. .....
    4. };
    5. void func( A &a ) //Is passed by reference
    6. {
    7.  
    8. }
    9. int main()
    10. {
    11. A a;
    12. func( a );
    13. return 0;
    14. }
    To copy to clipboard, switch view to plain text mode 

    -->What I want???
    Qt Code:
    1. class A
    2. {
    3. .....
    4. };
    5. void func( A a ) //should be forced to pass by reference always. This happens in java
    6. {
    7.  
    8. }
    9. int main()
    10. {
    11. A a;
    12. func( a );
    13. return 0;
    14. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Pass by reference

    As far as I know there is no other way around. "A &a" is how you do it in C++.
    J-P Nurmi

  5. #5
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pass by reference

    Quote Originally Posted by jpn View Post
    As far as I know there is no other way around. "A &a" is how you do it in C++.
    That is fine, but I need to make my class smart enough to provide such functionality. Is there some way I can implement my class like that???

    Thanks

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pass by reference

    What do you mean?
    You just pass the parameter by reference...

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pass by reference

    Quote Originally Posted by vermarajeev View Post
    I need to make my class smart enough to provide such functionality.
    This is counter intuitive in C++ and might lead to mistakes and confusion, but if you really want it, just implement data sharing class without copy-on-write mechanism (AFAIR it's called a single state class).

Similar Threads

  1. Can't compile custom class derived from QGLWidget
    By stealth86 in forum Qt Programming
    Replies: 1
    Last Post: 5th July 2007, 17:55
  2. error undefined reference ...............
    By amit_pansuria in forum Qt Programming
    Replies: 2
    Last Post: 8th June 2007, 14:28
  3. how to correctly compile threads support?
    By srhlefty in forum Installation and Deployment
    Replies: 9
    Last Post: 25th June 2006, 19:15
  4. Strange error while using Q3Canvas
    By Kapil in forum Newbie
    Replies: 13
    Last Post: 15th June 2006, 19:36
  5. linking user space and kernel space programs with qmake
    By zielchri in forum Qt Programming
    Replies: 9
    Last Post: 8th March 2006, 23:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.