class vector<?>


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: class vector<?>

  1. #1
    Join Date
    Dec 2004
    Posts
    47

    class vector<?>

    Hi there, :WAVE:

    I can't manage with vector<void*> class implementations, so I don't understand the following use of push_back func.
    Code:
    class StringVector : public vector<void*> {
    public:
        void push_back(string* s) {
            vector<void*>::push_back(s);
        }
    In vector.h (expected) are no implementations.
    Where can I find all of implementations of overloaded push_back functions?

    The definition above, seems a bit odd to me.
    If the base class function is overloaded in its own class to dealing with string*,
    why should we override it in inherited class to get the same form?

    One more strange thing is vector<void*> class.
    Is it possible that base vector<void*> class is 'created' in inherited class for first time (meaning, no declaration for that class in library)?
    In other words, are there implementations of vector class (somewhere in library) for all numerous arg. types (<int>,<int*>,<int&>,<float>,..........)?
    If not, seems strange to inherit something from 'nonestablished' class.




    Thanks

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by pseudo
    Hi there, :WAVE:

    I can't manage with vector<void*> class implementations, so I don't understand the following use of push_back func.
    Code:
    class StringVector : public vector<void*> {
    public:
        void push_back(string* s) {
            vector<void*>::push_back(s);
        }
    In vector.h (expected) are no implementations.
    Where can I find all of implementations of overloaded push_back functions?

    The definition above, seems a bit odd to me.
    If the base class function is overloaded in its own class to dealing with string*,
    why should we override it in inherited class to get the same form?

    One more strange thing is vector<void*> class.
    Is it possible that base vector<void*> class is 'created' in inherited class for first time (meaning, no declaration for that class in library)?
    In other words, are there implementations of vector class (somewhere in library) for all numerous arg. types (<int>,<int*>,<int&>,<float>,..........)?
    If not, seems strange to inherit something from 'nonestablished' class.




    Thanks
    First of all, inheriting from vector is a very bad idea. This class has no virtual member functions, let alone a virtual destructor. This means that you can neither override any of its member functions nor allocate dynamically objects of type 'vector <void*> *' that point to derived classes. Secondly, I can't see the reason for implementing you own string class when you already have such a standard class: std::string -- unless it's an exercise.
    Another point: vector is meant to contain objects, not pointers. Although it's possible to store pointers in a vector (for instance, in order to simulate a heterogeneous container), this technique incurs a lot of gotchas and potential bugs so it's better to use vector <C> rather than vector <C*>.

    Finally, the vector.h file is not the correct header. The standard name is <vector> -- without the .h extension. This name may be mapped to another file or files that contain the implementation.
    A general note about templates: vector does not contain explicit inatntiations for <float> <char> and so on. Doing that would nullify the very prupose of using templates, wouldn't it? What you have is a template from which the compiler synthesizes *at compile time* (which means you can't see the source files) the necessary specializations. In this case, it generates the code for vector <void*>. If you still want to reuse the implementation of vector (i.e., automatic memory management) a better solution is to use a vector as a private data member of a string class or perhaps private inheritance which we discussed here a few weeks ago. Public inheritance is a no-no when dealing with STL containers.

    Back to your original question:

    class StringVector : public vector<void*> {
    public:
    void push_back(string* s) {
    vector<void*>::push_back(s);
    }

    This code won't work because you need to call push_back like this:

    class StringVector : public vector<void*> {
    public:
    void push_back(string* s) {
    push_back(s);
    }

    This is still problematic because void * is not the same as string * as far as templates are concerned. You may have to cast s explicitly to void before calling push_back and then convert it back, at yoru own risk, to string *. Now you're probably seeing why vector <void*> is evil...
    Last edited by Danny; 02-13-2005 at 08:35 AM.
    Danny Kalev

  3. #3
    Join Date
    Dec 2004
    Posts
    47
    I suppose the main problem here actually is that B.Eckel introduces the vector class problems before explanating templates in general (templates come in the next chapters). /Eckel often uses most complicated examples possible - very strange way to teach/ :mad:

    In fact, this exercise is about inheritance.

    I understand now, that vector<void*> class is created at compile time.

    However i would emphasize the following question:
    class StringVector : public vector<void*> {
    public:
    void push_back(string* s) {
    push_back(s);
    }

    Why do we need to define the push_back function in inherited class at all, if we have it as a base function?
    Why don't we just call it when we need it?


    TVM

    p.s.
    How can I find vector class implementations.(Dev-Cpp compiler)

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by pseudo
    I suppose the main problem here actually is that B.Eckel introduces the vector class problems before explanating templates in general (templates come in the next chapters). /Eckel often uses most complicated examples possible - very strange way to teach/ :mad:

    In fact, this exercise is about inheritance.
    My goodness! I hate to say this but I have never been able to understand why people are so impressed with this book. I admit that I haven't read it (and the more I get to know it from your posts, I think it's just as well...) but I know there are so many better books around. Why on earth bundle a useless template with inheritance? And to think that so many C++ programmers learn from it...sigh.


    Quote Originally Posted by pseudo
    I understand now, that vector<void*> class is created at compile time.

    However i would emphasize the following question:
    class StringVector : public vector<void*> {
    public:
    void push_back(string* s) {
    push_back(s);
    }

    Why do we need to define the push_back function in inherited class at all, if we have it as a base function?
    Why don't we just call it when we need it?
    Another sigh. For the life of me, I really don't know what Eckel was trying to show here but suffice it to say that 1) no sane programmer would ever write such code for the oh so many good reasons I gave in my previous post. 2) If you really insist on inherting from vector (in spite of my warnings) then the most reasonable solution would be to use private inheritance AND a vector <string*> as a base class. This way, you really wouldn't need to reimplement push_back inside your class. I assume that in this example the push_back is implemented for clarity and because the author didn't want to rely on the implicit conversion of string * to void *. As a matter of fact, I doubt that this code would even compile because templates are very fussy about type matching. Anyway, I'm not even going to test it. I'm embarrassed to let my compiler see such code;)

    Quote Originally Posted by pseudo
    TVM

    p.s.
    How can I find vector class implementations.(Dev-Cpp compiler)
    I don't know about Dev-CPP but you should look for <vector> or <vector.stl> or even <vector.cc>. Dev-CPP is based on GNU's compiler but the question is which version of GNU. If it's lower than 3.0, then you really want to toss it out of the window.
    Last edited by Danny; 02-14-2005 at 07:50 AM.
    Danny Kalev

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center