简体   繁体   中英

Array Random Access C++

I'm probably going to confuse myself while writing this, sorry in advance:

Is there a way I can access a location in a dynamic array(increment an array pointer) of pointers using the sizeof() the object that is in the array?

For example: I have an dynamic array of type base class Student populated with derived class objects(Graduate, Undergraduate).

Due to this I can't just step through my array in the normal fashion to display information because the actual objects Graduate and Undergraduate are different sizes than Student. Each array step will move sizeof(Student) when the actual objects are larger.

Depending on the type of student I am doing(this is for Graduate):

Student *student = new Graduate(undergrad, fName, lName, major, idNum, arr2, cSize, 
        degreeType, thesis);
arr[i] = student;

Where arr was declared: Student *arr = new Student[size];

Using my array I had created this in a for loop:

if (!students[i].getGradStatus()){
    handleGraduate(&students[i], i);
    step = step + sizeof(Graduate);
}
else if (students[i].getGradStatus()){
    handleUndergraduate(&students[i], i);
    step = step + sizeof(Undergraduate);
}

I was trying to come up with a way to change the step size. I don't think this will work with a for loop but a while loop may be different. Pretty much I'm trying to go something similar to a file seekg() but manually on an array.

And as I've noticed everyone likes to question the use of dynamic arrays over vectors so let me just say I cannot use vectors on this project(No STL is allowed :( ). And I have to use Polymorphism, thus why I have an array pointer of type Student holding derived class objects.

You can't store different sized objects in an array like that. When you try to copy a derived type like Graduate over a base type like Student you get what's known as slicing because of the differences in object size (parts can get chopped off).

To do this you need to store Student* (pointers to Students .)

class Student
{
    std::string name;
public:
    Student(const std::string& name): name(name) {}
    virtual ~Student() {} // virtual destructor

    virtual void handle() = 0; // implementation must override this

    std::string get_name() const { return name; }
};

class Graduate
: public Student
{
public:
    Graduate(const std::string& name): Student(name) {}
    virtual void handle() { std::cout << "Handling Graduate" << '\n'; }
};

class UnderGraduate
: public Student
{
public:
    UnderGraduate(const std::string& name): Student(name) {}
    virtual void handle() { std::cout << "Handling UnderGraduate" << '\n'; }
};

int main()
{
    Student** students = new Student*[3];

    students[0] = new Graduate("Wendy");
    students[1] = new UnderGraduate("Bob");
    students[2] = new Graduate("Rachel");

    for(int i = 0; i < 3; ++i)
    {
        std::cout << students[i]->get_name() << '\n';
        students[i]->handle(); // polymorphic function (virtual)
    }

    delete[] students; // clean up or (better) use a smart pointer
}

Output:

Wendy
Handling Graduate
Bob
Handling UnderGraduate
Rachel
Handling Graduate

Change your array to be an array of Student pointers instead of Student objects.

 Student **arr = new Student*[size];

When you do that your objects will live on the heap and the pointer size is always the same, so the issue you are having will go away.

Since you can write that I assume that it's possible to instantiate a Student (non-derived). That's bad, since you're going to store derived objects in the array your students will be sliced (poor students!). To counter that you're doing weird things with the size of the instance types. Just don't.

Make an array of student pointers instead, Student **arr . Pointers have fixed size and they will point to any of the derived types of student so you don't have to worry about their sizes.


Student **student = new Student*[size];
...
student[i] = new Graduate(...);

This should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM