Results 1 to 4 of 4

Thread: passing an object

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default passing an object

    Hello,
    I have one easy question:
    Qt Code:
    1. class A {
    2. private:
    3. int _number;
    4. vector<A> Vector;
    5. public:
    6. A(int n) { _number = n; }
    7. A() {}
    8. void addCopy(A a) { Vector.push_back(a); }
    9. void addReference(A* a) {
    10. Vector.push_back(*a);
    11. }
    12. };
    13.  
    14. int main (int argc, char** argv) {
    15. A aa;
    16. A bb(99);
    17. aa.addCopy(bb);
    18. aa.addReference(&bb);
    19. return EXIT_SUCCESS;
    20. }
    To copy to clipboard, switch view to plain text mode 

    In this case, A keep a vector and when push_back() is called, it makes a copy; Then regarding addCopy: 1. pass bb to add (and it makes one copy of bb); 2. push a into Vector (and make one other copy). With referenceCopy instead I can avoid overhead of passing bb to the add() (and if bb it's large it can enhance the performance). With this above I wonder: why should I pass an objects (e.g. A) as value? I'm thinking that It's better pass an object always by reference (IF I DON'T CHANGE IT INSIDE THE FUNCTION, like I do in this case)
    Is this above right?

    thanks
    Regards

  2. #2
    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: passing an object

    You can pass by value if copying is cheap (for example you want to pass an integer or char) or when you know that you have to make a copy anyway. Otherwise passing by reference is cheaper.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: passing an object

    yes, but suppose to have a program larger than 100 000 lines of code where I could have many function that pass one char or int; if I did all by value (suppose even in those case by value or by reference has no difference) isn't it bad?
    Regards

  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: passing an object

    Passing a primitive data type (char, short, int, long, enum...) by value costs the same as passing it by reference.
    J-P Nurmi

Similar Threads

  1. Getting std::string object from QString object ( qt3)
    By joseph in forum Qt Programming
    Replies: 11
    Last Post: 28th March 2013, 20:09
  2. Replies: 2
    Last Post: 17th May 2006, 21:01
  3. Passing Object to dll
    By ankurjain in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 09:50
  4. Using QSA: A very basic question
    By yogeshm02 in forum Newbie
    Replies: 3
    Last Post: 26th January 2006, 07:34
  5. passing a QPainter object to widgets
    By vratojr in forum Qt Programming
    Replies: 9
    Last Post: 11th January 2006, 15:27

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.