CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Jan 2005
    Location
    Akron, Ohio
    Posts
    670

    override vector::insert

    Can someone please show me how to override vector::insert?

    I've tried:
    template <class T> iterator MyVector::insert ( iterator it, const T& x )
    {
    vector<T>::insert(it,x);
    // my code here
    }

    However, it tells me that I need more parameters for iterator, but I'm not sure what.

    MyVector is derived from vector.

    Thank you.
    error C2146a : syntax error : nebulizer stained in the tower floppy apple rider. Go rubble in flee smite. Bleeble snip snip.

    Documentation says: error C2146a - This means there is an error somewhere in the course of human endeavor. Fix in the usual way.

  2. #2
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: override vector::insert

    STL containers are not intended to be derived from because they don´t have a virtual destructor. Use composition instead to reuse std::vector. Your class needs to wrap its own methods around vector´s methods.

    Code:
    template<typename T>
    class myvector
    {
       std::vector<T> Impl_;
    
    public:
       // typedef iterator type
       typedef typename std::vector<T>::iterator iterator;
       typedef typename std::vector<T>::const_iterator const_iterator;
    
    
       iterator insert( iterator it, const T& op )
       {
          return Impl_.insert( Impl_.begin(), op );
       }
    }
    Please note that most methods of vector come in pairs to ensure const correctness.
    Last edited by GNiewerth; April 15th, 2008 at 07:12 AM.
    - Guido

  3. #3
    Join Date
    Jan 2005
    Location
    Akron, Ohio
    Posts
    670

    Re: override vector::insert

    Thanks! I'll give it a shot.
    error C2146a : syntax error : nebulizer stained in the tower floppy apple rider. Go rubble in flee smite. Bleeble snip snip.

    Documentation says: error C2146a - This means there is an error somewhere in the course of human endeavor. Fix in the usual way.

  4. #4
    Join Date
    Jan 2005
    Location
    Akron, Ohio
    Posts
    670

    Re: override vector::insert

    Rats. I'm going to have to recreate the interface for .at(..), .push_back(..), .pop_back(), .size(), as well as .insert(..), and perhaps a few others that I'm not readily thinking of. . .

    .at(..) returns const_reference, which apparently isn't the same thing as T&.

    Is the NO way to force the destructor for vector, so that I can keep my vector class as derived?? What if I .clear() the vector during the derived destructor?
    Last edited by paradoxresolved; April 16th, 2008 at 06:31 AM. Reason: grammatic error
    error C2146a : syntax error : nebulizer stained in the tower floppy apple rider. Go rubble in flee smite. Bleeble snip snip.

    Documentation says: error C2146a - This means there is an error somewhere in the course of human endeavor. Fix in the usual way.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: override vector::insert

    Quote Originally Posted by paradoxresolved
    Rats. I'm going to have to recreate the interface for .at(..), .push_back(..), .pop_back(), .size(), as well as .insert(..), and perhaps a few others that I'm not readily thinking of. . .

    .at(..) returns const_reference, which apparently isn't the same thing as T&.
    What exactly are you trying to accomplish by deriving from std::vector?
    Is the NO way to force the destructor for vector,
    What do you mean by "force the destructor"?
    so that I can keep my vector class as derived??
    Since you derived from vector, when your object is destroyed, vector is destroyed. There is no such thing as destroying a parent and having the derived remain intact.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: override vector::insert

    for example. Really not much to do

    Code:
    #include <vector>
    
    template<typename T>
    class VectorHolder
    {
    public:
    	virtual ~VectorHolder(){}
    
    	void clear() {TVector_.clear();}
    	bool empty() const {return TVector_.empty();}
    	std::vector<T>::size_type size() const {return TVector_.size();}
    	std::vector<T>::const_iterator begin() const {return TVector_.begin();}
    	std::vector<T>::iterator begin() {return TVector_.begin();}
    	std::vector<T>::const_iterator end() const {return TVector_.end();}
    	std::vector<T>::iterator end() {return TVector_.end();}
    
    	std::vector<T>::const_reference operator[](const std::vector<T>::size_type i) const {return TVector_[i];}
    	std::vector<T>::reference operator[](const std::vector<T>::size_type i) {return TVector_[i];}
    	std::vector<T>::const_reference at(const std::vector<T>::size_type i) const {return TVector_.at(i);}
    	std::vector<T>::reference at(const std::vector<T>::size_type i) {return TVector_.at(i);}
    
    protected:
    	std::vector<T> TVector_;
    };
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: override vector::insert

    1.
    .at(..) returns const_reference, which apparently isn't the same thing as T&.
    ???? there are two signatures for at()... one return a reference and
    one returns a const reference

    2. What exactly are you trying to accomplish in overriding insert() ?
    The normal way is composition.

    3. I think that you could use PRIVATE inheritance ... but you
    would need to provide implementations for the functions that
    you want.

  8. #8
    Join Date
    Jan 2005
    Location
    Akron, Ohio
    Posts
    670

    Re: override vector::insert

    Ok, I have tried this:

    Code:
    vector<T>::reference at(uint index);
    
    template<typename T> vector<T>::reference MyVector<T>::at(uint index)
    {
    	return this_vector.at(index);
    }
    It then returns the error:
    error C2662: 'at' : cannot convert 'this' pointer from 'const class MyVector<class boost::shared_ptr<class MyObject> >' to 'class MyVector<class boost::shared_ptr<class MyObject> > &'

    I see that there are two versions of .at(..). Is it confusing the one I want??

    Why do I want to derive a class from vector? It simplifies my code considerably. Long story. Making it a member of another class works just as well, I suppose. I just can't get the interface to play nicely now.
    error C2146a : syntax error : nebulizer stained in the tower floppy apple rider. Go rubble in flee smite. Bleeble snip snip.

    Documentation says: error C2146a - This means there is an error somewhere in the course of human endeavor. Fix in the usual way.

  9. #9
    Join Date
    Feb 2004
    Location
    Texas, USA
    Posts
    1,206

    Re: override vector::insert

    Code:
     
    template< typename t_type >
    class MyVector : public std::vector<t_type>
    {
    public:
    	~MyVector()
    	{
    		std::vector<t_type>::~vector();
    	}
    
    	iterator insert( iterator it, const t_type& x )
    	{
    		std::vector<t_type>::insert(it,x);
    		// my code here
    	}
    };
    *dodges all of the sharp weapons being thrown at him*
    --MrDoomMaster
    --C++ Game Programmer


    Don't forget to rate me if I was helpful!

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: override vector::insert

    Quote Originally Posted by paradoxresolved
    Why do I want to derive a class from vector? It simplifies my code considerably.
    How does it simplify your code? Show me an example of where code is simplified by deriving from vector. Deriving because it saves typing is not justification for derivation.

    There is something in your derived class that wasn't provided by vector, else you wouldn't have derived from it. Whatever those things are, you could more than likely have implemented them using algorithm or some other functions that worked on vector, or iterators in general.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: override vector::insert

    Quote Originally Posted by MrDoomMaster
    Code:
     
    template< typename t_type >
    class MyVector : public std::vector<t_type>
    {
    public:
    	~MyVector()
    	{
    		std::vector<t_type>::~vector();
    	}
    This code is ill-formed. The "natural" destruction process will already destroy the parent, and then you're calling it again, causing undefined behaviour.



    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Feb 2004
    Location
    Texas, USA
    Posts
    1,206

    Re: override vector::insert

    Oh, yeah! Good catch. I had a brain fart.
    --MrDoomMaster
    --C++ Game Programmer


    Don't forget to rate me if I was helpful!

  13. #13
    Join Date
    Jan 2005
    Location
    Akron, Ohio
    Posts
    670

    Re: override vector::insert

    I'm wrapping a class around vector such that, when this class is a member of a parent class, it automatically stores a pointer to the parent in the child object that is "pushed back" in the vector. It forms a tree that can search down to the root.

    Otherwise, I have to go through all my code and set the parent at each and every instance of "push_back." It's highly time-consuming, messy, and error-prone.
    error C2146a : syntax error : nebulizer stained in the tower floppy apple rider. Go rubble in flee smite. Bleeble snip snip.

    Documentation says: error C2146a - This means there is an error somewhere in the course of human endeavor. Fix in the usual way.

  14. #14
    Join Date
    Jan 2005
    Location
    Akron, Ohio
    Posts
    670

    Re: override vector::insert

    I'm still getting the error with the ".at(..)" interface. MSDN says there are two .at(..) functions in vector

    Code:
    reference at(size_type pos);
    const_reference at(size_type pos) const;


    It seems the second of the two is firing off, when the first is what I need.

    My .at(..) function is:

    Code:
    vector<T>::reference at(uint i) 
    {
    return TVector_.at(i);
    }
    Do I need to define both versions?
    Last edited by paradoxresolved; April 16th, 2008 at 07:12 PM. Reason: grammatic error
    error C2146a : syntax error : nebulizer stained in the tower floppy apple rider. Go rubble in flee smite. Bleeble snip snip.

    Documentation says: error C2146a - This means there is an error somewhere in the course of human endeavor. Fix in the usual way.

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

    Re: override vector::insert

    It seems the second of the two is firing off, when the first is what I need.
    That will only happen if the current object is const in the given context.

    Do I need to define both versions?
    Yes.
    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

Page 1 of 2 12 LastLast

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