-
July 28th, 2010, 03:32 AM
#1
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.
-
July 28th, 2010, 03:36 AM
#2
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?
-
July 28th, 2010, 03:48 AM
#3
Re: vector private inheritance
Originally Posted by laserlight
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.
-
July 28th, 2010, 04:05 AM
#4
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.
-
July 28th, 2010, 05:15 AM
#5
Re: vector private inheritance
Originally Posted by Yves M
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.
-
July 28th, 2010, 07:09 AM
#6
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.
-
July 28th, 2010, 08:09 AM
#7
Re: vector private inheritance
Originally Posted by ninja9578
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
-
July 28th, 2010, 08:38 AM
#8
Re: vector private inheritance
Originally Posted by JohnW@Wessex
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.
-
July 29th, 2010, 02:32 AM
#9
Re: vector private inheritance
Originally Posted by ninja9578
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
-
July 29th, 2010, 02:39 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|