0

Hi, I'm C++ beginner, and I'm looking for a solution of the pointer now. I write source code first because my description skills of the English is too bad.(Thank you, google translator!) This is my code example.

#include <iostream>
#include <vector>

using namespace std;

class UNIT
{
public :
    struct OBJ
    {
        float x,y,z;
    };
    vector<OBJ> obj;
    virtual void add(int x,int y,int z)=0;
    virtual void size()=0;
};
class KNIGHT : public UNIT
{
public :
    struct OBJ
    {
        float x,y,z;
    };
    vector<OBJ> obj;
    void add(int x,int y,int z)
    {
        OBJ sample;
        sample.x=x;
        sample.y=y;
        sample.z=z;
        obj.push_back(sample);
    }
    void size()
    {
        cout << obj.size() << endl;
    }
};

int main()
{
    KNIGHT knight;
    KNIGHT::OBJ sample_knight;
    UNIT* pt = &knight;

    pt->add(11,22,33);
    knight.obj.push_back(sample_knight);
    pt->size();
    cout << pt->obj.size() << endl;

    getchar();

    return 0;
}

And result is

2
0

(If I access the KNIGHT's variables likes pt->obj[0].x directly, it cause 'line 932 out of range')

From this experiment, I extracted the following results.

  1. UNIT* pt didn't point to KNIGHT's vector obj.
  2. But the UNIT* pt can use KNIGHT's add() and size().
  3. Probably it is associated with inheritance between UNIT and KNIGHT.

In addition, I'd tried pt->obj.push_back() (In this case, sample is UNIT::OBJ) and print pt->obj.size(), it showed me increased value. But pt->size() was the same as before. I expect it will cause problems of any kind at somewhere, and it is useless because it isn't connected with KNIGHT's add() and size().

So conclusion is this.

How can I get vector<OBJ> of KNIGHT(not UNIT) in directly, with UNIT* pointer or something?

※I'll use this in character pick function.

for example :

struct CHAR_LIST
{
    int type,num;
};
UNIT* Select_Unit(int type)
{
    switch(type)
    {
    case CHAR_KNIGHT :
        {
            return &knight;
        }
    case CHAR_ARCHER :
        {
            return &archer;
        }
    }
}
vector<CHAR_LIST> char_list;
UNIT* pt;
for (int num=0; num<char_list.size(); num++)
{
    pt = Select_Unit(char_list[num].type);
    pt->obj[char_list[num].num].x+=10;
    pt->obj[char_list[num].num].y=0;
}
1
  • Note that if you actually intended to hide the inherited names, you can access the base versions by doing something like UNIT::obj to access the base class version inside the derived class. (Replace UNIT with whatever the base name is, of course) Commented Sep 8, 2013 at 15:27

2 Answers 2

2

The OBJ and obj in KNIGHT hide those in UNIT. Comment them out and try again:

class KNIGHT : public UNIT
{
public :
    // struct OBJ
    // {
    //     float x,y,z;
    // };
    // vector<OBJ> obj;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. Certainly, out of range does not occur. But I want various types of units what have own values..(for the polymorphism) Is there no way for indivisual's OBJ struct?
1

You have two different instances of the vector<OBJ> obj. One in the base class and one in the derived class. Because KNIGHT derives publicly from UNIT it has access to UNIT's public vector<OBJ> obj and that's the one you want to use. Instead you are redefining it in the base class.

Basically, KNIGHT should look like:

    class KNIGHT: public UNIT
    {
      public:
        void add(int x,int y,int z)
        {
            OBJ sample;
            sample.x=x;
            sample.y=y;
            sample.z=z;
            obj.push_back(sample);
        }
        void size()
        {
            cout << obj.size() << endl;
        }
    };

As an observation, it doesn't look like you need the functions add and size to be pure virtual (or even virtual) and you could just define them in the class UNIT. If, however, you want some other class to have a different behaviour when calling add or size then you want to keep them virtual.

I somehow missed the last line where you actually asked the question. The thing is that the UNIT* can only access what's in the UNIT class. It has no idea that it's actually pointing to a KNIGHT object.

1 Comment

Thanks for your help. I'm going to have to use the method that get down individual characteristics to reluctantly if there is no way exists..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.