简体   繁体   中英

Accessing another classes member

I have the following

class book
{
    friend class linkedList;
private:
    class student
    {
        friend class book;
        string name;
        string number;
        string gpa;
        student *left;
        student *right;

        student(string name1, string number1, string gpa1,
            student *left1 = NULL, student *right1 = NULL)
        {
            name = name1;
            number = number1;
            gpa = gpa1;
            left = left1;
            right = right1;
        }
    };

    int count;
    student *root;
    ofstream recordBook;

    void _add(student *&, string, string, string);
    void _display_book(student *);
    bool _search_for_name(string, string&, string&);
    bool _edit_entry(string &name, string &edited_number);
    void _del_person(student *&, string);
    void _save(student *root);
    void _del_Tree(student *);

public:
    student *currentRoot;
    book(void); //Constructor
    ~book(void);//Destructor
    void add(string entry_name, string telephone_number, string gpa);
    void display_book();
    bool search_for_name(string find_name);
    bool edit_entry(string entered_name, string edited_number);
    void del_person(string entry_name);
    void save();
    void load_data();
};

class linkedList 
{
    friend class book;
    int someInt;
    struct node 
    {
    public:
        string key;
        node *link;
        node *link2;
    } *pointer;
public:
    student book::*currentRoot = &book::currentRoot;
    linkedList();
    ~linkedList();
    void append(string &str);
    void del(string &str);
    void display();
};

And I need to make a pointer to "student *currentRoot" from my linkedList classes function.

void linkedList::append(string &str)
{
    node *q, *t;
    if(pointer == NULL)
    {

        pointer = new node;
        pointer->key = str;
        pointer->link = NULL;
        pointer->link2 = currentRoot;
        someInt += 1;
    }
    else
    {
        q = pointer;
        while(q->link != NULL)
        {
            q = q->link;
        }
        t = new node;
        t->key = str;
        t->link = NULL;
        q->link = t;
        someInt += 1;
    }
}

In linkedList::append I need to make link2 point to where currentRoot is pointing to. How can I do this? (currentRoot is already set to point at a node in a binary tree. Just gotta get my hash table to also point there.) Thanks for any help.

In comments you said:

In the simplest terms I can think of... I am trying to get a pointer in one class to point to another pointer in a different class.

To answer that question: an inner class has no special relationship to the outer class. For example, a student instance within the book class has no direct way to access its “containing” class.

To do this you would have to pass a pointer to the containing book class into student 's constructor and store it in an instance variable within student .

However, this has a big caveat: when the student instance holds a pointer to the containing book class, that pointer could become invalid. For example, if you are storing book instances in a container like a std::vector , the vector may reallocate memory, invalidating any pointers to book s held within the vector .

If you can guarantee that the book will not be stored in an STL container (or anywhere else where it can get moved) then this approach can work.

I would still reconsider the overall approach as having instances store pointers in this way seems fragile. There could be a composition-based approach that would work.

The use of friend is suspect.

Typical object-oriented programming dictates that a linked list container class should only deal with pointers to class objects, and should not know about or deal with anything within the class objects themselves.

If the contained class does need to expose information about itself (ie, any of its members), it should provide public accessor functions for doing so.

I agree with what others commented about code, so I won't repeat it and just point to invalid syntax in your code:

public:
    student book::*currentRoot = &book::currentRoot;

1) the pointer to member is not what you wanted, it should have been:

public:
    book::student* book::*currentRoot;

2) you cannot assign to non-static member in class definition. It's only allowed for static members of integral type. But you can assign to it somewhere where you have an object:

void foo()
{
   linkedList l;
   l.currentRoot = &book::currentRoot;
}

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