Results 1 to 9 of 9

Thread: basic inherited or vector problem?

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

    Default basic inherited or vector problem?

    Qt Code:
    1. class Network {
    2. private:
    3. Layer lay;
    4. .....................
    5. };
    6.  
    7. void Network::_create() {
    8. lay._ne->resize(10);
    9. }
    10. //lay.h
    11. #include "ne.h"
    12. #include <vector>
    13. class Network;
    14. class Lay {
    15. protected:
    16. std::vector <Ne>* _ne;
    17. friend Network;
    18. public:
    19. Lay();
    20. ~Lay();
    21. };
    To copy to clipboard, switch view to plain text mode 
    Hi, I dont want use inherit for this program for my reasons.
    Problems:
    1. when resize is called program crash; I guess it happen because I'm working with vector of pointers...maybe they should be set to NULL (?). But how can I do this? Before resizing vector size is 0.....
    2. when net.create() is executed; then why do I have to use 'class network, friend network'? (otherwise I can't access to member protected _ne). Wich is the reason? If I done a class as member I guess that I can do everything with that oblect.....
    Are you understand?
    Are there any other implementation of this ? (without inherit please)
    thanks
    Last edited by mickey; 13th February 2007 at 01:37.
    Regards

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

    Default Re: basic inherited or vector problem?

    I solved so:
    Qt Code:
    1. Lay::Lay {
    2. _ne = new std::vector<Ne>;
    3. }
    To copy to clipboard, switch view to plain text mode 
    so memery is initialized and I can resize _ne everywhere. Is it correct? But maybe wasn't it better declare in lay.h std::vector <Ne> _ne; and not std::vector <Ne>* _ne; Any suggets?
    I'm hoping for the 2nd question.....
    Regards

  3. #3
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: basic inherited or vector problem?

    It is dangerous to use a pointer to a vector because when the vector is resized it is very likely that it will change it's location inside memory and thus invalidate your pointer. So std::vector<Ne> _ne; is what you're looking for.
    You have to declare Network as a friend because class A cannot access private or protected members of other classes (unless A is declared as a friend of the other classes). So what you are doing in your source-code is forward-declaring class Network and then telling your class Lay that Network is a friend and as such is allowed to access Lay's private/protected members.

  4. #4
    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: basic inherited or vector problem?

    Quote Originally Posted by Methedrine View Post
    when the vector is resized it is very likely that it will change it's location inside memory and thus invalidate your pointer.
    Not quite, only the location of internal buffer will change. The object itself will remain where it was.

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

    Default Re: basic inherited or vector problem?

    The problem here was that the poster was declaring a pointer to the object, but no object was created, therefore he was accessing random values in memory which didn't form a std::vector object which led to a crash. One has to differenciate between a pointer to an object and address of an object. Their values have the same type (object*) but creating a pointer doesn't create an object. I can have several pointers pointing to the same object (and that's the whole point of using pointers).

    Qt Code:
    1. int var;
    2. int *ptr1 = &var;
    3. int *ptr2 = &var;
    4. int *ptr3 = ptr2;
    To copy to clipboard, switch view to plain text mode 
    Now if I modify var ( var = 7; ) all pointers will still point to the same object, so dereferrencing them will still give me "7". Moreover if I now do:
    Qt Code:
    1. int var2 = 5;
    2. ptr2 = &var2;
    To copy to clipboard, switch view to plain text mode 
    ptr2 will point to var2, but ptr3 will still point to var. The bottom line is that pointers are not tied to any objects, they just point to a place in memory, regardless of its contents.

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

    Default Re: basic inherited or vector problem?

    Quote Originally Posted by mickey View Post
    Qt Code:
    1. class Network {
    2. private:
    3. Layer lay;
    4. .....................
    5. };
    6.  
    7. void Network::_create() {
    8. lay._ne->resize(10);
    9. }
    10. //lay.h
    11. #include "ne.h"
    12. #include <vector>
    13. class Network;
    14. class Lay {
    15. protected:
    16. std::vector <Ne>* _ne;
    17. friend Network;
    18. public:
    19. Lay();
    20. ~Lay();
    21. };
    22. Lay::Lay {
    23. _ne = new std::vector<Ne>;
    24. }
    To copy to clipboard, switch view to plain text mode 
    Sorry, I'm a bit confused;
    this above seems work. Is it dangerous or not?
    I could create _ne as no pointer: std::vector <Ne> _ne; but Isn't better declare "pointer to" in the class (.h) and then do "new" in their constructors? (in terms of performance...)
    Regards

  7. #7
    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: basic inherited or vector problem?

    Quote Originally Posted by mickey View Post
    Is it dangerous or not?
    No, it's not, but don't forget to implement operator= and copy constructor.

    Quote Originally Posted by mickey View Post
    but Isn't better declare "pointer to" in the class (.h) and then do "new" in their constructors? (in terms of performance...)
    Why should it increase the performance? It might affect the compilation speed, because you can use forward declarations instead of #includes.

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

    Default Re: basic inherited or vector problem?

    Quote Originally Posted by jacek View Post
    No, it's not, but don't forget to implement operator= and copy constructor.
    sorry, but why copy constructor and = operator?? (I gues for lay class but why and = operator ???). Is there here a particularry purpouse?
    Regards

  9. #9
    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: basic inherited or vector problem?

    Quote Originally Posted by mickey View Post
    Is there here a particularry purpouse?
    Yes, it's because Lay::_ne is a pointer. The default copy contructor and operator= implementations will make a copy of that pointer, not the object it points to. In result Lay's destructor might delete that object more than once.

    If you don't need operator= and copy constructor, you should just in case declare them as private (you can leave them unimplemented) or derive your class from boost::noncopyable.

Similar Threads

  1. Need Basic html Browser
    By awalesminfo in forum Newbie
    Replies: 6
    Last Post: 21st March 2006, 17:14

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.