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

class Signal {

    public:

        Signal() {

        }

    protected:
        std::vector<double> data;

};

class Something : public Signal {

    public:

        Something()
        {
            data.resize(100);
        }

};

class Parser : public Signal {

    public: 

        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?

Arghh!

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

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.

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.

commented: very good explanation :) +0
Member Avatar for iamthwee

Conclusion... Phorce needs more sleep :D

commented: I really am :( +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.