0

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 vector

But I want to use all the original functions and add on actions for each function. 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 trying to call the vector push_back but it is not working.

Anyone has any idea?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
darven
  • 367
  • 1
  • 5
  • 7
  • By the way, your include guard is broken. All names starting with underscore followed by a capital letter are *reserved*. They may conflict with names used or defined by the compiler or the standard library. (The same is true for any name containing a double underscore, and for underscore followed by lower-case letter at namespace scope. Easiest solution is to just avoid using leading underscores.) – jalf Oct 04 '10 at 17:31

3 Answers3

6
  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.

Community
  • 1
  • 1
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Inheriting an STL container is not dangerous. It's using a pointer to an STL container that would be dangerous. – aschepler Oct 04 '10 at 16:18
  • @aschepler: It isn't dangerous per se, but it is dangerous because it makes it really easy to do something really dumb. – James McNellis Oct 04 '10 at 16:28
  • It's still my opinion that using a `vector*` is usually dumb whether there are derived classes or not. But the key word there is "opinion". – aschepler Oct 04 '10 at 16:41
  • @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 Oct 04 '10 at 17:29
2

.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
  • 53,498
  • 9
  • 91
  • 140
  • The poster wants to use the methods defined by vector by default. Encapsulation wouldn't do that (except by manually forwarding every public method of vector). – aschepler Oct 04 '10 at 16:17
  • @aschepler - understood, see @James McNellis's answer for rationale behind my suggestion here. – Steve Townsend Oct 04 '10 at 16:19
0

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
  • 70,891
  • 9
  • 107
  • 161