Results 1 to 6 of 6

Thread: inlined costructor

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

    Default inlined costructor

    Hello, I read TC++ on page 393 and I don't understand if it says that in WithMember class it's better do a non-inlined class; and what does it refer with that //trivial? THanks
    Qt Code:
    1. class Member {
    2. int i, j, k;
    3. public:
    4. Member(int x = 0) : i(x), j(x), k(x) {}
    5. ~Member() { cout << "~Member" << endl; }
    6. };
    7.  
    8. class WithMembers {
    9. Member q, r, s; // Have constructors
    10. int i;
    11. public:
    12. WithMembers(int ii) : i(ii) {} // Trivial?
    13. ~WithMembers() {
    14. cout << "~WithMembers" << endl;
    15. }
    16. };
    17.  
    18. int main() {
    19. WithMembers wm(1);
    20. }
    To copy to clipboard, switch view to plain text mode 
    Regards

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

    Default Re: inlined costructor

    Trivial means there is no logic in it - it's empty (apart from calling the base class constructor).

  3. #3
    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: inlined costructor

    It says "Trivial?" because it only looks trivial. Although there is no code between {}, the constructor looks like this:
    Qt Code:
    1. <invoke Member::Member() on q>
    2. <invoke Member::Member() on r>
    3. <invoke Member::Member() on s>
    4. i = ii
    To copy to clipboard, switch view to plain text mode 
    In this case Member::Member() is rather simple, but in general case speed-up gained through inlining the constructor might be just a small fraction of the total time needed to construct the object, so you are just bloating your code.

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

    Default Re: inlined costructor

    Probably the word trivial refers to the code and not the logic behind it. And the question mark probably serves the same purpose as in the chess notation

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

    Default Re: inlined costructor

    Quote Originally Posted by jacek View Post
    It says "Trivial?" because it only looks trivial. Although there is no code between {}, the constructor looks like this:
    Qt Code:
    1. <invoke Member::Member() on q>
    2. <invoke Member::Member() on r>
    3. <invoke Member::Member() on s>
    4. i = ii
    To copy to clipboard, switch view to plain text mode 
    In this case Member::Member() is rather simple, but in general case speed-up gained through inlining the constructor might be just a small fraction of the total time needed to construct the object, so you are just bloating your code.
    Yes, WithMember constructor is big because it calls 3 object costructors; WithMember is inlined, but (if I understand right) in the worst case the compiler won't do the constructor inlined; and with piece of code "big", there no reasons to do inline (we prefer don't soil the class declaration or the .h file); At the end, if I understand, that empty constructor ( { } ), it's trivial, in the sense that we are inlining without gain (but it's even true that in somewhere we have to write the constructor body; maybe, when we're writing the code, until the body constructor is empty, it's simple/faster(in the sense of "writing") put it within the class declaration and not in a cpp). Is it ok?
    Regards

  6. #6
    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: inlined costructor

    Quote Originally Posted by mickey View Post
    At the end, if I understand, that empty constructor ( { } ), it's trivial, in the sense that we are inlining without gain (but it's even true that in somewhere we have to write the constructor body; maybe, when we're writing the code, until the body constructor is empty, it's simple/faster(in the sense of "writing") put it within the class declaration and not in a cpp). Is it ok?
    Not quite, whether the constructor is trivial or not depends not only on the code you wrote in its body, but also on the automatically generated code that calls the base class constructor and initializes member variables.

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
  •  
Qt is a trademark of The Qt Company.