Thread: Iterator

  1. #16
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Okay here's the deal w/inheriting std::vector and the template arguments:

    Case 1:
    Code:
    #include <vector>
    
    class SizeVector : public std::vector<int>{ // template parameter is defined
    
    };
    
    int main(){
    
      SizeVector szvec; // template parameter not needed
      return 0;
    }
    Case 2:
    Code:
    #include <vector>
    
    template <typename T>
    class SizeVector : public std::vector<T>{ // template parameter ambiguous
    
    };
    
    int main(){
    
      SizeVector<int> szVec; // template parameter needed
      return 0;
    }
    So it depends on what you need your class to do I guess.

    Here's an example of how to do this in SizeVector's initialization list:
    Code:
    #include <iostream>
    #include <vector>
    
    template <typename T>
    class SizeVector : public std::vector<T> {
    	
    	public:
    		
    		SizeVector<T>() : std::vector<T>() {} // Default constructor
    		
    		SizeVector<T>( const std::vector<T>& rhs ) // Alternate constructor copies an existing std::vector
    		: std::vector<T>( rhs ) {}
    };
    
    int main(){
    	
    	std::vector<int> aVector;
    	aVector.push_back( 5 );
    	
    	SizeVector<int> szVector( aVector );
    	
    	std::cout << szVector[0] << "\n";
    	
    	return 0;
    }
    Last edited by dudeomanodude; 05-19-2008 at 01:27 PM.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Don't inherit from std::vector.

    If you want a mathematical vector ("an element of vector space"), an iterator seems unneeded to me.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Thanks dudeomanodude, your comments are really helpful. I will attack it tomorrow evening...

    I won't derive from std::vector, since that purpose does not fulfill the Liskov substitution principle? (corned beef I've read your posts!). At least it's not an abstraction relation.

    My vector template class will be by specialized by any field of numbers (in the strict sense), for example complex, real, signed integers etc etc. It will be part of a complete as possible algebraic structure, but this is a future dream. I have therefore prepared many traits and policy/promotion classes for my algebraic template classes. It was really the iterator patterns which I don't know that well.

    I will use my iterators for sure, for coherence with matrices and the various algorithms on their multiplication or other. Storage of matrices will strongly depend on their structure, this is where many many template parameters for storage policies etc etc will come (and some are yet) into play. Parametrized iterators will keep algorithms on objects of these classes, so independent of storage, transparent. That is what I see as the largest beauty of generic programming.

    I'll definitely have questions about iterators, thanks a lot in the mean time!!

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I won't derive from std::vector, since that purpose does not fulfill the Liskov substitution principle?
    No, in this case it's simply a language-technical issue. vector is not meant to be derived from. You can do it, but there's restrictions you'll have to observe. Anyway, the C++ way of doing things is to supply the additional functionality you want in free functions, or to write a class that has a vector as a member.

    And a math vector with a variable number of dimensions (which is what one built from std::vector would be, or else there's little point in using std::vector) isn't terribly useful.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #20
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    a math vector with a variable number of dimensions isn't terribly useful.
    This is where I doubt to make the dimension a template parameter, for the moment I haven't.

    Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  3. linked list iterator class inheritience
    By neandrake in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2004, 10:03 AM
  4. Link list library
    By Brighteyes in forum C Programming
    Replies: 4
    Last Post: 05-12-2003, 08:49 PM
  5. Search and Build Tree
    By 1999grandamse in forum C++ Programming
    Replies: 17
    Last Post: 11-14-2002, 01:36 PM