I might be a bit tired, but why does the following not work?

class Signal {


        Signal() {


        std::vector<double> data;


class Something : public Signal {




class Parser : public Signal {


        Parser()  {
            std::cout << this->data.size();

int main()
    Something w; // resizes vector data to 100

    Parser f; // outputs size

Since I call Something which resizes the vector.. But, in Parser I can't seem to access the data member?


The output is 0 -> If, however, I inherit from Something it will work?

Edited by phorce

3 Years
Discussion Span
Last Post by iamthwee

But, in Parser I can't seem to access the data member?

In the constructor Parser(), you access the data member of the Parser object (named f in your code). You resized the data object of the object named w. That's a different object.

Edited by Moschops


You must be very tired indeed. ;)

When you construct the object "w" of class Something, it will contain a data vector with a size of 100. When you construct a second object "f" of class Parser, it will contain a data vector with a size of 0, because that's the size of an empty vector. The constructor of the class Parser does not initialize the size of the data vector within the object under construction to anything different than the 0 that it starts out with.

You almost seem to be confused about the difference between an object and a class. Each separate object contains its own set of data members (as declared in the class declaration). These data members are not shared between different objects, regardless of inheritance relationship, or even if they are of the same class. The data members that are tied to a class (instead of an object) are called "static data members", and must be declared with the static qualifier.

Votes + Comments
very good explanation :)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.