c++ - 从函数 C++ 正确传递指针

标签 c++ pointers inheritance recursion multiple-inheritance

它在 s_r->info 上崩溃,因为 s_r 没有指向 obj2。函数 search_recurs() 应该找到指针并返回结果。它应该找到正确的指针,因为结果指向 obj2

但返回结果时会发生一些事情,因为 s_r(其中 s_r=search_recurs() 返回结果)没有指向与结果相同的对象,这应该是从搜索中返回。

输出:

W Funkcji search:
Name: zmiana2d
Parameter_a : 5
Parameter_d : 6
adres rzutowanie: 00AFFC50
adres result: 00AFFC50

main:
adres obj2: 00AFFC50
adres s_r: 00AFFB04           //<======= Why is it not 00AFFC50 ? 

代码:

#include "stdafx.h"

using namespace std;


class Node {
private:

public:
    string name;
    Node *parent;
    vector <Node*> children;


    Node() { name = "noname", parent = NULL; }

    Node(string _name) { name = _name, parent = NULL; }

    Node(Node *_parent) { parent = _parent; }

    Node(string _name, Node *_parent) { name = _name, parent = _parent; }

    Node(Node *_parent, vector <Node*> _children) { parent = _parent, children = _children; }

    Node(string _name, Node *_parent, vector <Node*> _children) { name = _name, parent = _parent, children = _children; }

    virtual ~Node() { cout << "Base Destructor called\n"; }


    void add_parent(Node *wsk) {
        parent = wsk;
    }

    void add_children(Node *child) {
        children.push_back(child);
    }

    void info_node() {
        cout << "Name: " << name << endl;
        cout << "Address: " << this << endl;
        cout << "Parent: " << parent << endl;
    }

};


class A { 
private:
    string name;
    int parameter_a;
protected:

    A() { name = "untitled"; parameter_a = 1; }
    A(string _name) { name = _name, parameter_a = 1; }
    A(string _name, int _parameter_a) : name(_name), parameter_a(_parameter_a) {};

    string info_name() {
        return name;
    }

    int info_parameter_a()
    {
        return parameter_a;
    }

public:

    char info_type() {
        return 'A';
    }

    friend class Leaf;
    friend A* search_recurs_node(Node* root, string name);
    virtual void info() = 0;    
};


class Leaf : public Node { 
private:

public:
    vector<A*> objects;
    Leaf() { name = "noname", parent = NULL; }

    Leaf(string _name) { name = _name, parent = NULL; }

    Leaf(Node *_parent) { parent = _parent; }

    Leaf(string _name, Node *_parent) { name = _name, parent = _parent; }

    Leaf(Node *_parent, vector <Node*> _children) { parent = _parent, children = _children; }

    Leaf(string _name, Node *_parent, vector <Node*> _children) { name = _name, parent = _parent, children = _children; }

    void add_objects_leaf(A* obj) {
        objects.push_back(obj);
    }

};


class X : public A, public Leaf {
private:
    int parameter_x;
public:
    X() : A("dziedziczone_w_X_z_A", 98), parameter_x(99) {};
    X(string _name_x, int _parameter_a, int _parameter_x) : A(_name_x, _parameter_a), parameter_x(_parameter_x) {};


    char info_type() {
        return 'X';
    }


    void info() {
        cout << "Name: " << A::info_name() << endl;
        cout << "Parameter_a : " << A::info_parameter_a() << endl;
        cout << "Parameter_d : " << parameter_x << endl;
    }



};



A* search_recurs_node(Node* root, string name) {


    A* result;
    Leaf* rzutowanie;

    if ((root->children.size()) > 0) {
        for (int i = 0; i < (root->children.size()); ++i) {

            search_recurs_node(root->children[i], name);

        }    
    }
    else if (rzutowanie = dynamic_cast<Leaf*>(root)) {
        for (int i = 0; i < rzutowanie->objects.size();++i) {
            if (rzutowanie->objects[i]->info_name() == name) {
                cout << "W Funkcji search: " << endl;
                rzutowanie->objects[i]->info();
                cout << endl << "adres rzutowanie: " << rzutowanie->objects[i] << endl;
                result = (rzutowanie->objects[i]);
                cout << "adres result: " << result << endl;
                cout << endl;
                return result;
            }
        }
    }


    //return NULL;

};

int main()
{
//

    Node A_node("node_A");
    Leaf X_node("node_X", &A_node);

    A_node.add_children(&X_node);

    X obj1("name d1", 1, 2), obj2("zmiana2d", 5, 6);

    X_node.add_objects_leaf(&obj1);
    X_node.add_objects_leaf(&obj2);


    A* s_r;
    s_r = search_recurs_node(&A_node, "zmiana2d");

    cout << "main: " << endl;
    cout << "adres obj2: " << &obj2 << endl;
    cout << "adres s_r: " << s_r << endl;

    s_r->info();

    cout << endl << "The cause of 90% of programming errors sits in front of the screen" << endl;

    return 0;
}

最佳答案

初步评论

obj2X 类型,它继承自 ALeaf

您返回一个指向 A 的指针。但是 A 子对象是用 Leaf 嵌入到 X 中的;这就是为什么您可以获得另一个地址的原因。您应该将其转换为 X* 以确保具有完全相同的地址 whatever the layout of your class :

 X* x_r = dynamic_cast<X*>(s_r);   // you can, because there's a virtual fct
 if (x_r) 
    cout << "adres x_r: "<<x_r<<endl; 
 else cout << "couldn't cast"<<endl; 

顺便说一句,想为任何具有虚函数的类添加一个虚析构函数。

问题的根本原因

你的递归搜索函数不起作用:你递归地调用它,但你没有对结果做任何事情。修改如下:

...
if ((root->children.size()) > 0) {
    for (int i = 0; i < (root->children.size()); ++i) {
        auto a= search_recurs_node(root->children[i], name);  // <== keep response returned
        if (a)                                    // <== if value already found
           return a;                              // <== return it
    }    
}
...

在你的函数结束时返回 nullptr 是明智的(你为什么要注释掉它?)

Online demo

关于c++ - 从函数 C++ 正确传递指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40344409/

相关文章:

c++ - set_difference并不总是返回正确的答案

c++ - 编写一个从类型列表返回类型的元函数,该类型列表具有 C++11 中给定类型的 typedef

改变指针数组的内容

C - 使用结构成员值的段错误

asp.net-mvc - ASP.NET MVC : Ignore custom attribute in a base controller class

java - 使用@Inheritance JPA属性时插入顺序无效

java - 方法返回类型来实现多个接口(interface) - 再次

c++ - "usual arithmetic conversions"和 "integer promotions"是一回事吗?

将指针转换为整数

c++ - 在 C++ 中使用::每次