Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: pointer in a class

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

    Default pointer in a class

    Hello,
    I'm wondering when is necessary/better declare a pointer to an object in a class or a pointer to a vector in a class
    Qt Code:
    1. class A {
    2. public:
    3. vector<flaoat> fvec;
    4. //or
    5. vector<float>* fpvec;
    6. A() {
    7. fvec.resize(1000);
    8. //or
    9. fpvec = new vector<float>(1000);
    10. }
    11. };
    To copy to clipboard, switch view to plain text mode 
    What's the thing that lead me to opt one or other?
    THe same question if I had a Person p[10]; in the class. Should be better take a pointer to an array of person? How to choice?

    thanks
    Regards

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

    Default Re: pointer in a class

    Quote Originally Posted by mickey View Post
    I'm wondering when is necessary/better declare a pointer to an object in a class or a pointer to a vector in a class
    The only reasons to declare a pointer instead of an object I see are:
    1. The object pointed by the pointer is not owned by the containing object or the instance is shared among more than one containing object.
    2. One wants to only forward declare a class in the header file to speed up compilation of dependent objects and slow down initialization of objects.
    3. The platform the application is running on has limitations on the stack size and as much data as possible is pushed onto the heap.

    Apart from those three situations I see no reason to use pointers instead of objects.

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

    Default Re: pointer in a class

    Although if you do decide not to use a pointer, it might be a good idea to give the vector its initial size in the initialization list:

    Qt Code:
    1. A() : fvec(1000) {}
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: pointer in a class

    Oh, I just thought of another reason to use a pointer instead of a full object - when the use of the object is optional and the full object is allocated only when needed - then you'll need a pointer. On the other hand it doesn't make much sense to do that with a vector because it already lazy allocates it data on the heap, so both conditions are fulfilled with using a vector object instead of vector pointer. The difference in memory footprint will be minimal (unless you resize the vector when constructing it - then there is no lazy allocation there).

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

    Default Re: pointer in a class

    Is there any change to use forward declaration with vector, like this:
    Qt Code:
    1. class vector; // ?
    2.  
    3. class A {
    4. vector<float>* fvec;
    5. .......................
    6. };
    To copy to clipboard, switch view to plain text mode 
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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.

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

    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

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

    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 

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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.

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

    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

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

    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 

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

    Default Re: pointer in a class

    nothing to do
    Regards

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

    Default Re: pointer in a class

    IMHO the pointer shouldn't be const.

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

    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

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

    Default Re: pointer in a class

    What do you mean by "nothing to do"?

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

    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

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

    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.

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

    Default Re: pointer in a class

    Quote Originally Posted by Shadowfiend View Post
    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.
    Maybe it can be the solution; but then, my problem is in a other place; try to compile this ,please (It doens't compile on .net2005)
    Qt Code:
    1. //main.cpp
    2. #include <iostream>
    3. #include "forward.h"
    4. #include <vector>
    5. using namespace std;
    6.  
    7. int main (int argc, char argv[]) {
    8.  
    9. Forward f;
    10. vector<float>* fvec = new vector<float>; //EDIT <----------------------------
    11.  
    12. return EXIT_SUCCESS;
    13. }
    14. //forward.h
    15. #ifndef FORWARD_H
    16. #define FORWARD_H
    17.  
    18. namespace std { template <class T> class vector; }
    19. // #include <vector> // with only this, all compiles
    20.  
    21. class Forward {
    22. std::vector<float>* _vec;
    23. public:
    24. Forward();
    25. ~Forward();
    26. };
    27.  
    28. #endif FORWARD_H
    29.  
    30. //forward.cpp
    31. #include "forward.h"
    32. #include <vector>
    33. using namespace std;
    34.  
    35. Forward::Forward() {
    36.  
    37. _vec = new vector<float>;
    38. }
    39. Forward::~Forward() {}
    To copy to clipboard, switch view to plain text mode 
    Last edited by mickey; 25th May 2008 at 22:25.
    Regards

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

    Default Re: pointer in a class

    Ah, that's right. std::vector takes an std::allocator as its second parameter. Well that just gets nasty...

    Here's a place where this same issue is discussed: http://objectmix.com/c/251244-forwar...compliant.html

    Looks like it's possible, but the advantage is negligible and there is not necessarily a guarantee that it will work.

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

    Default Re: pointer in a class

    Sorry, I did a mistake; what I want to show is that the code doens't still work; see the code I correct it....what I say is that, with your solution, if I put #define <vector> in such file that include even "forward.h", the compiler gets errors; does anyone know how to correct the last code above ,please?
    Regards

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.