3

I have inherited some code that I am looking at extending, but I have come across a class\constructor that I have not seen before. Shown below in the code snippet

class A {
 public:
    A() {};
    ~A() {};
 //protected:
    int value_;

class B: public std::vector<A> {
  public:
    B(int size = 0) :
      std::vector<A>(size)
    {}

So from what I gather class B is a vector of class A which can be accessed using the *this syntax because there is no variable name. I would like to initiate class A in the constructor, but I am unsure how to do this with in this context. I have looked at this but they have declared the vector as an object where in this case it is the class.

This seems slightly different from normal inheritance where I have inherited many instances of a single class, compared to the usual one to one in most text books. What I was trying to do, was propagate a value to intialise class A through both class B and class A constructor. Something like below is what I tried but doesn't compile.

#include <iostream>
#include <vector>
using namespace std;
class A {
 public:
    A(int int_value) :
    value_(int_value)
    {};
    ~A() {};
 //protected:
    int value_;
};

class B: public vector<A> {
  public:
    B(int size = 0, int a_value) :
      vector<A>(size, A(a_value))
    {};

    vector<int> new_value_;

    void do_something() {
        for (auto& element : *this)
            new_value_.push_back(element.value_);
    }
};

int main() {
    cout << fixed;
    cout << "!!!Begin!!!" << endl;
    B b(20,23);
    cout << b.size() << endl;

    b.do_something();
    for(auto& element : b.new_value_)
        cout << element << endl;

    cout << "finished" << endl;
    system("PAUSE");
    return 0;
}

I guess a follow up question is, is this a good implementation of what this code is trying to achieve

Cyrillm_44
  • 701
  • 3
  • 17

1 Answers1

2
B(int size = 0, int a_value) ... 

is incorrect. You may not have an argument with a default value followed by an argument that does not have a default value.

Possible resolutions:

  1. Provide default values for both arguments.

    B(int size = 0, int a_value = 0) ... 
    
  2. Provide default value only for the second argument.

    B(int size, int a_value = 0) ... 
    
  3. Change so that neither has a default value.

    B(int size, int a_value) ... 
    
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Thanks that was silly. I follow up question is, is this good practice to create a class of a vector of classes without assigning an object name to it? and anther one just for good measure. Why is it that class B can't inherit protected objects from class A? – Cyrillm_44 Jun 12 '18 at 03:45
  • @Cyrillm_44, I don't think that is good coding practice. You use inheritance for an *is-a* relationship. You use aggregation for a *has-a* relationship. It's rarely that anything *is-a* sub-type of `std::vector`. – R Sahu Jun 12 '18 at 03:47
  • 1
    "Why is it that class B can't inherit protected objects from class A?" You aren't inheriting from class A but from class std::vector. – Goswin von Brederlow Jun 12 '18 at 09:00