Reputation: 45
I am fooling around with vectors while running through a C++ book. I have been using "Vector" with an uppercase 'v' by accident. I just noticed in the book they use a lowercase 'v'. I have changed a bunch of my test programs over to use "vector" instead, and other than the color change in my IDE I have yet to discern a difference. As a sidenote I am using a custom header file called "std_lib_facilities.h" which is where I grabbed the definitions from.
The descriptions I get are as follows
vector
//Disgusting macro hack to get a range checked vector:
#define vector Vector
Vector
//Trivially range-checked vector (no iterator checking)
template< class T> struct Vector : public std::vector<T>
{ Whole bunch of code }
My first guess would be the header file I am using just happens to have created its own class named "Vector" of type "vector", to perform what looks like some sort of error handling (I have no idea how to understand most of this header file). My second guess would be the "Vector" is a revised version of "vector", its possible "Vector" hails from the STL and "vector" from old school C. The thing I find interesting is it doesn't seem to matter which one I use. Any ideas?
P.S. The header was provided by Bjarne Stroustrup as part of his Programming Principles and Practice 2ndEd book.
Upvotes: 0
Views: 837
Reputation: 238351
Before reading the answer, please first consider the the advice given in the comments of the header:
Students: please don't try to understand the details of headers just yet. All will be explained. This header is primarily used so that you don't have to understand every concept all at once.
template< class T> struct Vector : public std::vector<T>
Vector
is a class template that the author defines in that header. An instance of the template such as Vector<int>
would be a class that inherits from the class std::vector<int>
, where std::vector
is a class template defined in the standard library.
//Disgusting macro hack to get a range checked vector: #define vector Vector
vector
is a pre-processor macro. The pre-processor will replace any occurrences of the text vector
with Vector
such that if you were to write:
vector my_vector_object;
The compiler will instead see:
Vector my_vector_object;
The macro is also a disgusting hack, as documented by the author.
Difference between “vector” and “Vector”
The thing I find interesting is it doesn't seem to matter which one I use.
After that macro definition, there is no difference between writing vector
or Vector
, because the macro will replace one with the other, such that the compiler sees Vector
in either case.
Upvotes: 2