Properly passing pointer from function C++

catdoanything33

It crashes on s_r->info because s_r is not pointing on obj2. Function search_recurs() should find the pointer and return result. It should find the right pointer because result points on obj2.

But something happens when returning the result because s_r (where s_r=search_recurs() returns the result) does not point on the same object as result, which should be returned from search.

Output:

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 ? 

CODE:

#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;
}
Christophe

Preliminary remark

obj2 is of type X, which inherits from A and from Leaf.

You return a pointer to an A. But the A subobject is embedded in X with Leaf; This is why you could get another address. You should convert it to an X* to be sure to have exactly the same address, 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; 

By the way, think to add a virtual destructor for any class that has a virtual function.

The root cause of your problem

Your recursive search function doesn't work: you call it recursively, but you're not doing anything with the result. Change as follows:

...
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
    }    
}
...

It would be wise to return nullptr at the end of your function (why did you comment it out ?)

Online demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C - Passing pointer to function

From Dev

ANSI C - passing data from function to a CHAR pointer

From Dev

Trouble passing a function pointer to a method from main in C++

From Dev

Passing a pointer to an array in a function in C

From Dev

C++ passing function pointer

From Dev

Passing Function Pointer in C#

From Dev

Passing a pointer (string) to a C function

From Dev

confusion in passing a pointer to function in C

From Dev

Passing pointer of an array to a function in C

From Dev

passing a pointer through a function c

From Dev

C++: Passing lambda pointer as a function pointer

From Dev

passing function pointer in c with a pointer argument

From Dev

Passing single pointer and double pointer to a function in c

From Dev

Passing a pointer from a function to another function

From Dev

Passing function pointer from native to java

From Dev

Passing pointer/array to main() from a function

From Mysql

Value from php is not passing properly to javscript function

From Dev

Boost C++. Passing a member function pointer with signature pointer to function

From Dev

Passing Array of Structs to a Function as a Pointer (C)

From Dev

passing a pointer of different type to a function (C)

From Dev

C++ crashing when passing pointer to function

From Dev

Passing pointer to an array into a function (C++)

From Dev

Passing a C function pointer to emscripten callback

From

passing function pointer to the C code using cgo

From Dev

Passing pointer to function that accepts reference in c++

From Dev

Passing a safe rust function pointer to C

From Dev

Passing in an element in an array as a pointer in a function in C

From Dev

C - Passing a pointer to an array to a function to print the array

From Dev

c struct passing itself as an argument into a pointer function

Related Related

  1. 1

    C - Passing pointer to function

  2. 2

    ANSI C - passing data from function to a CHAR pointer

  3. 3

    Trouble passing a function pointer to a method from main in C++

  4. 4

    Passing a pointer to an array in a function in C

  5. 5

    C++ passing function pointer

  6. 6

    Passing Function Pointer in C#

  7. 7

    Passing a pointer (string) to a C function

  8. 8

    confusion in passing a pointer to function in C

  9. 9

    Passing pointer of an array to a function in C

  10. 10

    passing a pointer through a function c

  11. 11

    C++: Passing lambda pointer as a function pointer

  12. 12

    passing function pointer in c with a pointer argument

  13. 13

    Passing single pointer and double pointer to a function in c

  14. 14

    Passing a pointer from a function to another function

  15. 15

    Passing function pointer from native to java

  16. 16

    Passing pointer/array to main() from a function

  17. 17

    Value from php is not passing properly to javscript function

  18. 18

    Boost C++. Passing a member function pointer with signature pointer to function

  19. 19

    Passing Array of Structs to a Function as a Pointer (C)

  20. 20

    passing a pointer of different type to a function (C)

  21. 21

    C++ crashing when passing pointer to function

  22. 22

    Passing pointer to an array into a function (C++)

  23. 23

    Passing a C function pointer to emscripten callback

  24. 24

    passing function pointer to the C code using cgo

  25. 25

    Passing pointer to function that accepts reference in c++

  26. 26

    Passing a safe rust function pointer to C

  27. 27

    Passing in an element in an array as a pointer in a function in C

  28. 28

    C - Passing a pointer to an array to a function to print the array

  29. 29

    c struct passing itself as an argument into a pointer function

HotTag

Archive