a C++ problem

Howdy all,
This has nothing to do with blender, but I figured there would be some coders here who could help me out.

Lets say I have a class, with 2 pointers of type char for data members.
How can I dereference the data members to insert and get data from them?


#include <iostream>
using namespace std;
                                                                                                                                                             
class Student {
                                                                                                                                                             
    public:
        char* Student_Name;
        char* Student_idnumber;
                                                                                                                                                             
    private:
                                                                                                                                                             
};
                                                                                                                                                             
int main() {
                                                                                                                                                             
    int number;
    cout << "Number of students in database: ";
    cin >>  number;
                                                                                                                                                             
    Student* db = new Student[number];

    for( int i = 0; i<number; i++ ) {
        cin >> db[i].Student_Name*;      // syntax error
        cin >> db[i].Student_idnumber*; // syntax error
    }
                                                                                                                                                             
    delete[] db;
}

If I change the char* to string, and get rid of the * everything works fine as is.

Any insight would be appreciated.
Thanks,
Elam

        cin >> db[i].Student_Name*;      // syntax error
        cin >> db[i].Student_idnumber*; // syntax error

shouldn’t you be putting the derefrence operator (*) at the begining?

but your real problem is that db[i] is a derefrence (db[0] is the same as *db for example), you be derefrencing further

so you have one too many derefrences in the first place

Well, thats the point of my question. The * isn’t to dereference db.
Like you said, thats already been done. I’m trying to figure out how to dereference the class datatypes, name and idnumber.

Change char to string. C++ adds the string dat type for you so you don’t have to mess with char arrays.

For your question, you would dereference the strings like so:


char ch = *db[i].Student_Name;

// Note:  Same as:
char ch = db[i].Student_Name[0];

But, there are other problems with that.

  1. I don’t think you really want to dereference it. Dereferencing it gives you access to the value of a single char in the char array.

  2. Nowhere are you allocating memory for your char*. This is a BAD thing. (At least in the code you show there is no allocation here.)

To “correctly” use a char* in this case, you would have to do something like so…


db[i].Student_Name = new char[MAX_NAME_LENGTH];

cin >> db[i].Student_Name;

Instead of dynamically allocating memory like this, you might want to just make Student_Name and Student_idnumber arrays of some max length. So:


class Student
{
public:
    char Student_Name[MAX_NAME_LENGTH];
    char Student_idnumber[MAX_ID_LENGTH];
};

// Then in your function:
cin >> db[i].Student_Name;
cin >> db[i].Student_idnumber;


Either that, or you could make those private, and implement Set methods that do the allocation for you. From an OOD perspective, it’s generally best to have the class do the allocation of it’s primitive members rather than doing it outside the class. Then in the function where you read in the data, you would just do something like…


char buffer[MAX_BUFFER_SIZE];

cin >> buffer;
db[i].SetName(buffer);

Whew… this is getting longer than I had anticipated. I’ll shut up now. :slight_smile: