Results 1 to 4 of 4

Thread: Class value members & references

  1. #1
    Join Date
    Nov 2009
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Class value members & references

    Hello, I am still fighting with C++ references! Lets say we have this:

    Qt Code:
    1. class Foo {
    2.  
    3. public:
    4. Foo(Bar& b);
    5.  
    6. private:
    7. Bar m_bar;
    8. };
    9.  
    10. Foo::Foo(Bar& b) : m_bar(b) {}
    To copy to clipboard, switch view to plain text mode 

    1. Should the constructor better have the parameter as value instead of reference?
    2. Since I am using the constructor initialization list, would there be a performance difference?
    3. Would there be a semantic difference? e.g., to the caller of new Foo


    Thank you again

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Class value members & references

    Do you need a private version of bar for your class, or would sharing the one passed to you be sufficient?

    Secondly, if the constructor did not have a reference, there would be another copy created there, so you would be creating two copies. Passing by reference avoids that copy.

  3. #3
    Join Date
    Nov 2009
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Class value members & references

    Hi fatjuicymole,

    Secondly, if the constructor did not have a reference, there would be another copy created there, so you would be creating two copies. Passing by reference avoids that copy.
    Ok but, I have read on C++ FAQ LITE this:
    Consider the following constructor that initializes member object x_ using an initialization list: Fred::Fred() : x_(whatever) { }. The most common benefit of doing this is improved performance. For example, if the expression whatever is the same type as member variable x_, the result of the whatever expression is constructed directly inside x_ — the compiler does not make a separate copy of the object.
    So the way I understand this is that by declaring the parameter as value + using the constructor initialization list is the most efficient! Fuckin confusing C++

    The most important question for me is whether there is a semantic difference for the caller of my API.

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Class value members & references

    It is the most efficient, if you want a copy of the object. If you don't mind sharing, or your access is read-only, then I would say that using a pointer would be quicker still, as then it only has to move the size of that pointer, which is 4 bytes on a 32-bit platform.

Similar Threads

  1. Replies: 5
    Last Post: 29th November 2009, 16:09
  2. QT Class GUI Interactions??
    By mikey33 in forum Newbie
    Replies: 3
    Last Post: 19th November 2009, 03:01
  3. Question about class members
    By serenti in forum Qt Programming
    Replies: 5
    Last Post: 11th June 2009, 15:20
  4. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 08:57
  5. Replies: 4
    Last Post: 26th June 2007, 20:19

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.