简体   繁体   English

访问另一个班级成员

[英]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. 我需要从我的linkedList类函数中创建一个指向“ student * currentRoot”的指针。

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. 在linkedList :: append中,我需要将link2指向currentRoot指向的位置。 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. (currentRoot已经设置为指向二叉树中的一个节点。只需要让我的哈希表也指向该树即可。)感谢您的帮助。

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. 例如, book类中的一个student实例无法直接访问其“包含”类。

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 . 为此,您必须将指向包含book类的指针传递到student的构造函数中,并将其存储在student的实例变量中。

However, this has a big caveat: when the student instance holds a pointer to the containing book class, that pointer could become invalid. 但是,这有一个很大的警告:当student实例持有指向包含book类的指针时,该指针可能变得无效。 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 . 例如,如果您将book实例存储在std::vector类的容器中,则该向量可能会重新分配内存,从而使指向vector保存的book的任何指针无效。

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. 如果您可以保证book不会存储在STL容器中(或其他可以移动的地方),则可以使用这种方法。

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. 怀疑使用friend

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: 1)指向成员的指针不是您想要的,应该是:

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

2) you cannot assign to non-static member in class definition. 2)您不能在类定义中分配给非静态成员。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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