CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Hybrid View

  1. #1
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    vector private inheritance

    So I have a class which is implemented "as a" vector. => vector private inheritance.

    No public client is supposed to know that my class derives from vector, and since the inheritance is private, the client shouldn't be able to handle my class via vector* anyways.

    I don't have a single virtual function

    Further more, my class has no members, it is purely implemented as a vector.

    I don't intend to have any public children (though I will implement a single private child, which will also be implemented "as a")

    I get no warnings from the standard MinGW with max anal options (strict etc), but when I activate "Enable Effective C++ warnings (thanks Scott Meyers) [-WEffc++]", I get:

    Code:
    warning: base class 'class std::vector<unsigned char, std::allocator<unsigned char> >' has a non-virtual destructor
    Now I understand the warning, and that Effective C++ are more of "are you sure?" rather than "It's legal, but undefined" and but I'm fairly certain I'm safe, but I still have some doubts. So I thought I'd pass my design through here.

    My two questions:

    1) Am I safe? Did I miss something?

    2) What are the risks that I mess up in the future? What are some of the ways I can break my design? I mean, as long as the base class is never called via pointer to destroy an instance of my class, I'm 100% safe, right?

    I realize I could just go for agregation, but it just isn't as practical. And less fun
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: vector private inheritance

    I think this is just an oversight in the implementation of -WEffc++. I am a little puzzled by your statement that your "class has no members". Why not just use a typedef then?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: vector private inheritance

    Quote Originally Posted by laserlight View Post
    I think this is just an oversight in the implementation of -WEffc++. I am a little puzzled by your statement that your "class has no members". Why not just use a typedef then?
    It's not meant to be handled exactly like a vector, the client isn't allowed to call resize, insert, erase etc, but he still gets observers like size, cpacity. etc. I wrote my own modifiers.

    Oh, wait, by "no members", I mean "no member variables", there are member methods of course.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #4
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: vector private inheritance

    1) Well, you are safe, because the lack of a virtual destructor is not a problem when there is no way to actually use it.

    2) Basically yes. However, you should write a big comment at the start of your class definition that "This inheritance is private and should remain so, suck up to re-writing accessors" ;-)
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: vector private inheritance

    Quote Originally Posted by Yves M View Post
    2) Basically yes. However, you should write a big comment at the start of your class definition that "This inheritance is private and should remain so, suck up to re-writing accessors" ;-)
    my class looks something like this

    Code:
    class myClass : std::vector<int>
    {
    public:
        //my own functions
    
    public:
        using std::vector<int>::size();
        using std::vector<int>::max_size();
    }
    rather than re-writing accessors. Is there something wrong with that?

    But I agree about the big comment
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: vector private inheritance

    It would be nice if C++ has something like a 'final' keyword to prevent this. You should never try and inherit from anything in STL.

  7. #7
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: vector private inheritance

    Quote Originally Posted by ninja9578 View Post
    It would be nice if C++ has something like a 'final' keyword to prevent this.
    See http://mindprod.com/jgloss/unmaindesign.html Paragraph 24
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  8. #8
    Join Date
    Jan 2009
    Posts
    1,689

    Re: vector private inheritance

    Quote Originally Posted by JohnW@Wessex View Post
    What's the different between declaring a class final and making the dtor non-virtual? You can't inherit from either one, final just enforces it. It would also keep people from overriding methods that they shouldn't *cough* for copy on write classes *cough*
    Last edited by ninja9578; July 28th, 2010 at 08:40 AM.

  9. #9
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: vector private inheritance

    Quote Originally Posted by ninja9578 View Post
    You can't inherit from either one, final just enforces it.
    The difference is down to the underlying philosophy of C++ v Java.
    In C++ you can inherit from a non-virtual base, but it is up to you to be sure that the class will not be used polymorphically. In Java it is assumed that if you give someone the opportunity to use something incorrectly then they should be protected from yourself.

    C++ - Be careful using the scissors, because they're sharp.
    Java - You can't use scissors, because they're sharp.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  10. #10
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: vector private inheritance

    Actually, private inheritance disables polymorphic behaviour anyway

    Code:
    class A
    {
    };
    
    class B : private A
    {
    };
    
    int main()
    {
        B b;
    
        A *p = &b; // Will not compile
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured