Results 1 to 9 of 9

Thread: private inheritance

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

    Default private inheritance

    Hi,
    I know it isn't used but for completness....Why compiler says I can't instantiate it? I've publicized it in the derived class; doesn't it should work?
    Qt Code:
    1. Pet* p = new Cat;
    2. #ifndef PET2_H
    3. #define PET2_H
    4. using std::string;
    5. using std::cout;
    6. class Pet {
    7. public:
    8. Pet() { speak(); cout << "Pet::Pet()\n"; }
    9. virtual ~Pet() = 0;
    10. virtual string speak() { return "Pet "; }
    11. };
    12. inline Pet::~Pet() { speak(); cout << "Pet::~Pet()\n"; }
    13. class Cat : private Pet {
    14. char* _ver;
    15. public:
    16. Pet::Pet;
    17. Cat() : _ver( new char('q') /* 1 */ ) { cout << "Cat::Cat()\n";}
    18. ~Cat() { cout << "Cat::~Cat()\n"; delete _verso; }
    19. string speak() { return "Cat: bark, bark\n"; }
    20. };
    21. #endif PET2_H
    22. error C2243: 'type cast' : conversion from 'Cat *' to 'Pet *' exists, but is inaccessible
    To copy to clipboard, switch view to plain text mode 
    furthermore: in /* 1 */ i'd like to initialize with a string. Which sintax, please?
    Regards

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

    Default Re: private inheritance

    You can't cast because Cat inherits Pet privately, so the outside world shouldn't even know Cat inherits Pet. If you want to access Pet from Cat, you should probably use public inheritance here and hide everything you need in its private section.

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

    Default Re: private inheritance

    yes, I see that what I read was a bit different (there's no polymorphism); so if I do Cat cat; it works. I hope this trick of private will work even with virtual..mmmm
    Regards

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

    Default Re: private inheritance

    I reply to myself (about one question on post #1):
    Qt Code:
    1. char* _ver;
    2. Cat() : _ver("Hello\n") { }
    To copy to clipboard, switch view to plain text mode 
    or forced to use a new
    Qt Code:
    1. string* _ver;
    2. Cat() : _ver( new string("hello") )
    To copy to clipboard, switch view to plain text mode 
    Still, I know isn't smart take pointer to char inside a class ( even if _ver will go on the stack; then it shouldn't be unsafe); maybe take a string* could be a bit more intelligent than a char on the heap (but I don't now how exactly)...
    Regards

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

    Default Re: private inheritance

    What are you trying to obtain?

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

    Default Re: private inheritance

    to have a string data member inside class Cat....and initialize it in the constructor initializator list....
    Regards

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

    Default Re: private inheritance

    So what is the problem?

    Qt Code:
    1. class Pet {
    2. public:
    3. Pet();
    4. };
    5.  
    6. class Cat : public Pet {
    7. public:
    8. Cat(const std::string &str) : Pet(), _ver(str){ }
    9. private:
    10. std::string _ver;
    11. };
    To copy to clipboard, switch view to plain text mode 
    or if you don't want to use std::string:
    Qt Code:
    1. class Cat : public Pet {
    2. public:
    3. Cat(const std::string &str) : Pet(), _ver(strdup(str)){ }
    4. ~Cat(){ free(_ver); }
    5. private:
    6. char *_ver;
    7. };
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: private inheritance

    what's strdup?
    Regards

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

    Default Re: private inheritance

    char *strdup( const char *str )


    - prototype in string.h

    - duplicates str, getting space with a call to malloc()
    - returns pointer to duplicated string, or NULL if space could not
    be allocated

Similar Threads

  1. Inheritance and Slots/Signals
    By LMZ in forum Qt Programming
    Replies: 5
    Last Post: 4th June 2007, 20:43
  2. [custom widget] What about private headers ?
    By lauranger in forum Qt Programming
    Replies: 3
    Last Post: 19th November 2006, 00:14
  3. We'll need a QT Jambi section!
    By GreyGeek in forum General Discussion
    Replies: 23
    Last Post: 30th September 2006, 01:38
  4. Link Errors
    By magikalpnoi in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 23:04
  5. How much inheritance do you use?
    By Michiel in forum General Programming
    Replies: 8
    Last Post: 1st August 2006, 23:29

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.