tags:

views:

51

answers:

3

hi, i have 2 questions.

  1. does any one know what an hpp file is? why would someone do that ?

  2. i am trying to implement a class that extends vecotr

but i want to use all thee riginal functinos and add on actions for each functin. so i wrote:

#include <iostream>
#include <vector>

#ifndef _MY_PERSONAL_VECTOR
#define _MY_PERSONAL_VECTOR


class PersonalVec: public std::vector<int>{

public:
    PersonalVec();

    void push_back(const int& Val);


};

#endif

and in the cpp file:

#include <iostream>
#include "PersonalVec.hpp"

using namespace std;

PersonalVec::PersonalVec(): std::vector<int>(){
}

void PersonalVec::push_back(const int& Val):vector<int>::push_back(Val){
    cout<<"new improved vector";
}

so in the function push_back i am trieng to call the vector push_back but it is not workink.

any one have any idea? thanks very much :)

+2  A: 

.hpp is a naming convention, sometimes used to distinguish header files containing template classes from non-template classes, or to distinguish C code from C++ code.

You need to encapsulate the vector and delegate to it.

class PersonalVec{

public:
    PersonalVec();

    void push_back(const int& Val);

private:
    std::vector<int> data;

};

void PersonalVec::push_back(const int& Val){
    cout<<"new improved vector";
    data.push_back(Val);
}
Steve Townsend
The poster wants to use the methods defined by vector<int> by default. Encapsulation wouldn't do that (except by manually forwarding every public method of vector<int>).
aschepler
@aschepler - understood, see @James McNellis's answer for rationale behind my suggestion here.
Steve Townsend
+6  A: 
  1. Does anyone know what an hpp file is?

.hpp is a commonly used file extension for C++ header files.

  1. I am trying to implement a class that extends vector

You probably don't want to do that. The standard library containers do not have virtual destructors and are not intended to be derived from. You should prefer to:

  • use composition (have a container as a member variable), or
  • extend the functionality using non-member functions

Which is better depends entirely on what you need to do.

James McNellis
Inheriting an STL container is not dangerous. It's using a pointer to an STL container that would be dangerous.
aschepler
@aschepler: It isn't dangerous per se, but it is dangerous because it makes it really easy to do something really dumb.
James McNellis
It's still my opinion that using a `vector<int>*` is usually dumb whether there are derived classes or not. But the key word there is "opinion".
aschepler
@aschepter: inheriting isn't dangerous, it just creates a type that is dangerous to *use*. `std::vector` is designed so that it is basically safe to use, even if you create pointers to it. Inheriting from a standard container destroys that guarantee. Now you can perform an otherwise perfectly normal operation, which makes your vector derivative fail.
jalf
A: 

A *.hpp file is a header file. It's no different from an *.h file, except that some people like to name them that way so they can immediately know whether the file is a C header or C++ header.

You can only use :stuff() that way in the definition of a constructor. Your method definition should be:

void PersonalVec::push_back(const int& Val) {
  vector<int>::push_back(Val);
  cout << "new improved vector";
}
aschepler