Results 1 to 11 of 11

Thread: Question about references in C++

  1. #1
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Question about references in C++

    Hello, having this simple code:

    Qt Code:
    1. void myFunction(int& iAux){iAux = myINTMemberVariable;}
    To copy to clipboard, switch view to plain text mode 

    when it's executed, iAux is a copy of the integer or it's aimed to the adress in memory of the member variable?

    I ask this because this is my usual way to obtain the private members for a class, and sometimes they have a huge QList of objects. In some programs I have this operation few times each few miliseconds... so it can be expensive.

    Thanks!

  2. #2
    Join Date
    Jul 2009
    Posts
    74
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: Question about references in C++

    hi !
    your example it's weird...
    int myFunction() { return memberVariable; }

    anyway...
    in that function iAux is a reference to memory... (more or less like a pointer)....
    so...it's cheap and fast. So... that's the way to go.
    If you need to pass a variable by value: const int & iAux

    ----
    now in spanish:
    uff.. tio... menudo inglés que tienes !

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Question about references in C++

    You don't have to be afraid of passing a huge list by value. It is cheap because it is implicit shared. So it's like passing a pointer.

  4. #4
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Question about references in C++

    Thank you!

    your example it's weird...
    int myFunction() { return memberVariable; }
    With simple type data I agree, but I prefer to use references instead of pointers when I have to pass lists, maps...

    Javimoya: I feel sorry if my english is bad or something is difficult to understand... I'm recently working too much and this can affect my poor brain :P

  5. #5
    Join Date
    Jul 2009
    Posts
    74
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: Question about references in C++

    qlist and many other containers and classes in qt are a pointer to a data structure... so it's cheap and fast to return a qlist.
    look for "implicit shared" in qt docs

  6. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Question about references in C++

    Don't confuse the implicit sharing with reference, and also don't confuse pointers with references:

    The pointer is another variable that has as value an address of some object.

    The reference is something like an alias (just another name) for the variable/object, and any change to reference will change the object itself.

    The implicit sharing constructs another object that will share the same data with the first until it's modified, when you modify the implicit shared object you create a copy and the original remains un-modified.

    Example:
    Qt Code:
    1. #include <QString>
    2. #include <QDebug>
    3. #include "iostream"
    4.  
    5. void StrImpShared_funct (QString inStr){ //just take an qstring object by value, this will create an implicit shared object (since QString class uses implicit sharing)
    6.  
    7. //no copy of the data in made yet
    8.  
    9. inStr += "World"; //here, when we modify the implicit shared object it will copy the data from original and will modify the copy, and leave the original unchanged
    10. qDebug() << "the ImplicitShared string is: " << inStr;
    11. }
    12.  
    13. void StrRef_funct (QString& inStr){ //take a reference and add something to the qstring, this will modify the original
    14. inStr += "World";
    15. qDebug() << "the Reference string is: " << inStr;
    16. }
    17.  
    18.  
    19. int main()
    20. {
    21. QString str = "Hello ";
    22. qDebug() << "the original string is: " << str << '\n';
    23.  
    24. StrImpShared_funct(str);
    25. qDebug() << "the original string is still: " << str << '\n';
    26.  
    27. StrRef_funct (str);
    28. qDebug() << "the original string is, now: " << str << '\n';
    29.  
    30. std::cin.get();
    31. return 0;
    32. }
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to Zlatomir for this useful post:

    Seishin (24th July 2012)

  8. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Question about references in C++

    You don't have to be afraid of passing a huge list by value. It is cheap because it is implicit shared. So it's like passing a pointer.
    I hope you mean "passing a huge QList"

    IMHO, passing a QList, QString ect. by value is bad.
    I know about implicit data sharing in QT, but this can introduce bad programming habits in general (especially for begginers).

  9. #8
    Join Date
    Jul 2009
    Posts
    74
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: Question about references in C++

    Quote Originally Posted by stampede View Post
    I hope you mean "passing a huge QList"

    IMHO, passing a QList, QString ect. by value is bad.
    I know about implicit data sharing in QT, but this can introduce bad programming habits in general (especially for begginers).

    totally agree.

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Question about references in C++

    Quote Originally Posted by stampede View Post
    IMHO, passing a QList, QString ect. by value is bad.
    I know about implicit data sharing in QT, but this can introduce bad programming habits in general (especially for begginers).
    Passing by reference is a much worse habbit and is ambiguous.

    Take a look at this snippet, is "x" passed by value or by reference?
    Qt Code:
    1. int x = 7;
    2. doSomething(x);
    3. qDebug() << x;
    To copy to clipboard, switch view to plain text mode 

    If you expect doSomething() to change "x", it is cleaner to pass it by pointer
    Qt Code:
    1. int x = 7;
    2. doSomething(&x);
    3. qDebug() << x;
    To copy to clipboard, switch view to plain text mode 

    Some C++ purists will complain but the fact is it is a construct that doesn't force you to look at the declaration of doSomething().

    If you don't want such constructs, you can always pass "x" by value or by a const reference and use the return value:
    Qt Code:
    1. SomeType x = 7;
    2. x = doSomething(x);
    To copy to clipboard, switch view to plain text mode 

    Compare the three situations and decide yourself which ones are clear and which are not. Especially the last syntax is where implicit sharing helps.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Question about references in C++

    Passing by reference is a much worse habbit and is ambiguous.
    I totally agree with this. I never pass arguments by reference if I want to change them inside method or function (because of the 'unclear' syntax reason, I prefer pointers for this) and always use const references instead of values (for complex datatypes). That's what I meant in my previous post.

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Question about references in C++

    I sometimes pass Qt objects (and PODs) by value and not by const reference if I expect that I might need to modify the value locally:
    Qt Code:
    1. void setName(QString x) {
    2. if(x.endsWith('\n'))
    3. x.chop(1);
    4. doSomething(x);
    5. }
    To copy to clipboard, switch view to plain text mode 
    instead of:
    Qt Code:
    1. void setName(const QString &x) {
    2. QString y = x;
    3. if(x.endsWith('\n'))
    4. y.chop(1);
    5. doSomething(y);
    6. }
    To copy to clipboard, switch view to plain text mode 

    Besides, implicit sharing is so cheap that passing always by value is not a big problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Class value members & references
    By agnus in forum General Programming
    Replies: 3
    Last Post: 6th December 2009, 15:18
  2. Pointers and references
    By PaceyIV in forum Newbie
    Replies: 10
    Last Post: 27th July 2009, 11:53
  3. QSqlRelationalTableModel - two references to the same table
    By adamkoczur in forum Qt Programming
    Replies: 0
    Last Post: 12th February 2009, 15:45
  4. QWebView undefined references
    By xtreme in forum Qt Programming
    Replies: 2
    Last Post: 28th July 2008, 09:08

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.