Different array size in derived classes
I need an array to hold long integer type data in base class. For derived
classes, this arrays element will be different. How should I handle this problem?
Thanks for help.
[183 byte] By [
Jian Sun] at [2007-11-19 19:44:39]

# 1 Re: Different array size in derived classes
Use templates.
# 2 Re: Different array size in derived classes
How should I handle this problem?
I feel that inheritance should not be used in your case.
Or maybe, your base class is just too concrete, and some members should be removed and replaced by abstractions.
Please, post the code of your class, and explain what they are.
# 3 Re: Different array size in derived classes
Another possible solution is a pointer to void.
Kuphryn
# 4 Re: Different array size in derived classes
Another possible solution is a pointer to void.
Do not listen to that! Never ever use void*!!! In the worst case, if there is no other option boost::any or variant can be considered.
# 5 Re: Different array size in derived classes
Do not listen to that! Never ever use void*!!! In the worst case, if there is no other option boost::any or variant can be considered.
Why? 'void *' works, and has none of the overhead of a boost::any, or a variant. A void pointer is perfectly acceptable, especially if used correctly (and properly commented; but I digress).
Viggy
# 6 Re: Different array size in derived classes
Why? 'void *' works, and has none of the overhead of a boost::any, or a variant. A void pointer is perfectly acceptable, especially if used correctly (and properly commented; but I digress).
void* is acceptable when we know what we do with it, and may be necessary with old C-style callbacks (nowadays, C++ programs tend to use abstract base classes as "interfaces" instead of callbacks, avoiding unnecessary typecasts).
void* may seldomly be useful in arrays. Because, usually, all objects must implement at least an abstract base class.
But I feel that it is not correct for the OP problem.
The first question we should ask is : If inheritance is public, does these classes respects the LSP (Liskov Substitution Principle), and a IS-A relationship.
Note : the IS-A relationship is not always easy to explain or understand, but the LSP is easy to understand for anywho has a brain.
If inheritance is private, is it necessary, or may delegation be used?
Up to now, we cannot answer these questions, without having more information about these classes (code would help).
# 7 Re: Different array size in derived classes
void* is acceptable when we know what we do with it, and may be necessary with old C-style callbacks (nowadays, C++ programs tend to use abstract base classes as "interfaces" instead of callbacks, avoiding unnecessary typecasts).
void* may seldomly be useful in an array. Because, usually, all objects must implement at least an abstract base class.
But I feel that it is not correct for the OP problem.
The first question we should ask is : If inheritance is public, does these classes respects the LSP (Liskov Substitution Principle), and a IS-A relationship.
Note : the IS-A relationship is not always easy to understand for everybody, but the LSP is easy to understand for anybody (with a brain).
If inheritance is private, is it necessary, or may delegation be used?
Up to now, we cannot answer these questions, without having more information about these classes (code would help).
Agreed. I was just responding to DragForce's comment:
Do not listen to that! Never ever use void*!!!
Viggy
# 8 Re: Different array size in derived classes
Different array size in derived classes
I need an array to hold long integer type data in base class. For derived
classes, this arrays element will be different. How should I handle this problem?
Use vectors of the datatypes that you want to store.. You do not need to put limitations on the size's of the arrays in this way...
As for void* - it really depends on what you are using it for... there is "no one rule to fit all" ;)
Because, usually, all objects must implement at least an abstract base class.
Are you hinting towards something like the Base Object class as they have in Java? It is not surprising to see that even they are now (even .Net) falling back to Generics/Templates to get that bit of type safety that Base Object caused to lose... Regards..
# 9 Re: Different array size in derived classes
A void pointer is perfectly acceptable, especially if used correctly (and properly commented; but I digress).
Viggy
Another description of the null set... :)
# 10 Re: Different array size in derived classes
To the OP. My initial reaction is that your design is wrong and that you're going to end up making problems for yourself. If you can give more details of what you're trying to achieve, then maybe we can give more useful help.
# 11 Re: Different array size in derived classes
Why? 'void *' works, and has none of the overhead of a boost::any, or a variant. A void pointer is perfectly acceptable, especially if used correctly (and properly commented; but I digress).
Because it is a source of errors. It breaks all type safety rules.
Proper comments does not help because they have to be read, while type correctness insures that the software won't compile if one uses wrong type.
The overhead is negligible in vast majority of the cases. And it is well known that premature optimisation is the greatest sin. First any software has to work and only if it works one may think of optimising bottlenecks, which are tiny bits of code.
# 12 Re: Different array size in derived classes
Are you hinting towards something like the Base Object class as they have in Java? It is not surprising to see that even they are now (even .Net) falling back to Generics/Templates to get that bit of type safety that Base Object caused to lose... Regards..
Abstract base classes are type safe as long as there is no downcasting.
Templates can't be used in the OP problem (except if his design is modified, which is probably the correct solution), because the template type cannot change from the base class to a derived class.