Results 1 to 14 of 14

Thread: Writing a QVector subclass

Threaded View

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

    Default Re: Writing a QVector subclass

    But isn't that the same for any non-template class that I define in a separate .cpp file
    No, as long as you compile and link methods from the "separate .cpp file", compiler can "look" into compiled code and take any method it needs. In the template case, there is no code in separate .cpp generated for the specific template instantiation.
    So the declarations are available at compile time, right ?
    Declarations - yes, definitions - no.
    But I still don't understand why there is a difference to a non-template class.
    Because when you create non-template class derived from QVector<Type>, compiler has everything it needs - QVector<Type> is instantiated, so its like a normal class from now on.
    Main difference is in the .cpp files -
    Case 1: template-based approach:
    Qt Code:
    1. // .cpp file with template methods:
    2. template <class T> SSVector<T>::SSVector()
    3. {}
    4. template <class T> SSVector<T>::SSVector(const SSVector<T>& that)
    5. {}
    6. // compiler has a template, but when you compile this file, it does not "see" that
    7. // somewhere in other files you want to use above methods for a specific T
    To copy to clipboard, switch view to plain text mode 
    Case 2: non-template approach:
    Qt Code:
    1. // .cpp file with non-template methods
    2. #include "ssvector.h"
    3. // include actually pastes the file content, so it looks like:
    4. class SSVector : public QVector<int/*or anything else*/>
    5. {
    6. public:
    7. SSVector();
    8. SSVector(const SSVector& that);
    9. };
    10.  
    11. SSVector::SSVector()
    12. {}
    13. SSVector(const SSVector& that)
    14. {}
    15. // all is ok, compiler knows what is QVector<int>, and it has both declarations and
    16. // definitions of all needed SSVector methods
    To copy to clipboard, switch view to plain text mode 
    I hope it helps.
    Last edited by stampede; 19th June 2011 at 22:31. Reason: typo

  2. The following user says thank you to stampede for this useful post:

    Computer Hater (19th June 2011)

Similar Threads

  1. QVector, QLinkedList
    By aash_89 in forum Qt Programming
    Replies: 2
    Last Post: 22nd July 2010, 02:20
  2. Replies: 8
    Last Post: 12th February 2010, 02:41
  3. Qvector
    By phillip_Qt in forum Qt Programming
    Replies: 3
    Last Post: 27th November 2007, 10:46
  4. QVector
    By sabeesh in forum Qt Programming
    Replies: 2
    Last Post: 17th September 2007, 14:37
  5. Subclass
    By merry in forum General Programming
    Replies: 2
    Last Post: 1st March 2007, 10:34

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.