Results 1 to 20 of 24

Thread: pointer in a class

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: pointer in a class

    Your code that depends on class A will compile faster as it will not have to parse vector's header file each time A's header is included. The application will run a bit slower though, as it'll have to allocate the vector at some point.

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

    Default Re: pointer in a class

    Quote Originally Posted by wysota View Post
    Your code that depends on class A will compile faster as it will not have to parse vector's header file each time A's header is included.
    but I asked how write it (this above doens't compile)
    Quote Originally Posted by wysota View Post
    The application will run a bit slower though, as it'll have to allocate the vector at some point.
    I guess the cause is around the allocation on the heap...
    Regards

  3. #3
    Join Date
    Jan 2006
    Posts
    26
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanked 1 Time in 1 Post

    Default Re: pointer in a class

    I think the forward declaration has to look something like:

    Qt Code:
    1. template <class T> class vector;
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: pointer in a class

    I'd say it should be "class vector<float>;". I don't see a reason to use a pointer to a vector in this situation, though... It's just asking for trouble and a necessity to write more code.

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

    Default Re: pointer in a class

    Quote Originally Posted by wysota View Post
    I'd say it should be "class vector<float>;". I don't see a reason to use a pointer to a vector in this situation, though... It's just asking for trouble and a necessity to write more code.
    Qt Code:
    1. class vector<float>;
    2.  
    3. class Shape {
    4. const std::vector<float>* _sides;
    To copy to clipboard, switch view to plain text mode 
    It doesn't compile; compile gets many error on the first line (like as I missed the ";").
    I know It's unuseful, but I'd like compile it for study....
    thanks.
    Regards

  6. #6
    Join Date
    Jan 2006
    Posts
    26
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanked 1 Time in 1 Post

    Default Re: pointer in a class

    Probably because you didn't put it in a namespace. Try:

    Qt Code:
    1. namespace std { class vector<float>; }
    To copy to clipboard, switch view to plain text mode 

    or:

    Qt Code:
    1. namespace std { template <class T> vector; }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: pointer in a class

    nothing to do
    Regards

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

    Default Re: pointer in a class

    IMHO the pointer shouldn't be const.

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

    Default Re: pointer in a class

    Quote Originally Posted by wysota View Post
    IMHO the pointer shouldn't be const.
    nothing to do again....
    Regards

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

    Default Re: pointer in a class

    What do you mean by "nothing to do"?

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

    Default Re: pointer in a class

    Quote Originally Posted by wysota View Post
    What do you mean by "nothing to do"?
    Sorry, my english is not correct.....It means that compiler gets the same errors (a lot) on the line "class vector<float>;" (I took off "const" as you suggested) . Of course, if I include vector.h, there aren't problems...I don't understand how to do this forward declaration
    Regards

  12. #12
    Join Date
    Jan 2006
    Posts
    26
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanked 1 Time in 1 Post

    Default Re: pointer in a class

    Sorry, I made a mistake in my template declaration. Try:

    Qt Code:
    1. namespace std { template <class T> class vector; }
    To copy to clipboard, switch view to plain text mode 

    That compiles fine on my computer.

Similar Threads

  1. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 07:57
  2. Saving object pointer
    By MarkoSan in forum General Programming
    Replies: 4
    Last Post: 11th January 2008, 11:53
  3. How to use Signal through direct connection
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th December 2007, 07:07
  4. Connecting to a base class signal?
    By AaronMK in forum Qt Programming
    Replies: 4
    Last Post: 26th October 2007, 22:37
  5. Replies: 2
    Last Post: 4th May 2006, 19:17

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.