N
N
Nikita Kargin2020-06-01 08:59:48
C++ / C#
Nikita Kargin, 2020-06-01 08:59:48

Why does the size of the object not match?

I have a class:

class object {
private:

public:
    vector<vector<double>> coords;
    vector<vector<int>> rels;
};

This class has a subclass:
class Terrain : public object {
private:

public:
    vector<vector<double>> coords = { {-viewportrl,viewportrl,0},{viewportrl,viewportrl,0},{-viewportrl,-viewportrl,0},{viewportrl,-viewportrl,0} };
    vector<vector<int>> rels = { {2,3},{1,4},{1,4},{2,3} };
};

In my main file I declare a vector:
Terrain terr;
vector<object> wld = { terr };

It turns out that terr.coords.size()=4. BUT wld[0].coords.size()=0.
As I understand it, the compiler refers to the coords property of the object class, but how can I make it refer specifically to the coords property of the Terrain class. If you remove the coords property from the object class, it will swear that it does not exist

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Armenian Radio, 2020-06-01
@Pakonigoosy

1) You should not allow sticking out of data field classes - all fields, except perhaps constants, should be made private.
2) In this case, your classes will have methods for accessing these fields, which will need to be declared virtual
3) Instead of storing instances in a vector of objects, you should make a vector of smart pointers to these objects
4) It is desirable to make the base class for these objects abstract , at worst - interface.
After that, your homologous class hierarchy will behave the way you want it to.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question