Passing methods to other methods

Hi, i have this data collection i which the nodes are linked by parent. meening multiple nodes can be connected to one parent. like in this http://i50.tinypic.com/2qtc1ad.png picture.

Each node has a method called foo(). now i want to call all foo()s through the root node. so i need to have the child node pass it's foo() to it's parents foo(), and the parent to pass his foo() to his parent foo() and so on and so forth until they all get to the root node's foo().

Is this possible in c++ and is it the right aproach?
Why not just iterate through each node and call foo()?
because i only have access to the root node let me put this in code for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
class ObjectNode {
	public:
		ObjectNode* parent;
		char* name;

		ObjectNode(char* n, ObjectNode* p) {
			name = n;
			parent = p;
		}
		~ObjectNode() {}
               
		void foo(...
};


then i wanna use it like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Collection {
	public:
		ObjectNode* root;

		Collection() {
			root = new ObjectNode("root", NULL);
		}
		
                void AddToObjectNode(ObjectNode* to, char* n) {
                       ObjectNode* temp = new ObjectNode(n, to);
                }

                void Add(char* n) {
                       ObjectNode* temp = new ObjectNode(n, root);
                }

                void RunCollection() {
                       root.foo();
                }
};


and have root.foo() call
Last edited on
This scenario doesn't make sense. What exactly would foo need to do? You're probably approaching this the wrong way...
i will add functionality to foo() later for name lets say it just prints out name.
so would it be possible to pass child's foo to it's parent's foo. like this maybe:

1
2
3
4
5
6
7
8
void foo([method parameter] f) {
      printf("%s", name);
     
     f();

      if(parent!=NULL)
           parent->foo(foo(f));
}

Edit: this is what i meant to write. sorry about confusion
Last edited on
You're making less sense now.

parent.foo(foo(f));
foo doesn't return anything, and it only takes a member function pointer, so I don't know what you want this to do.
Edit: *parent(foo)*
it means i want to pass the method foo as [method parameter] to parent.foo()
Ohh wait i see a logical error in my aproach. sorry bout' that, still thanks for your time. reverting back to the liked list aproach.
1
2
3
4
5
6
7
void AddToObjectNode(ObjectNode* to, char* n) {
                       ObjectNode* temp = new ObjectNode(n, to);
                }

                void Add(char* n) {
                       ObjectNode* temp = new ObjectNode(n, root);
                }


You know that you have memory leaks here.
Rather than the messy solution of passing function pointers around, just so that each instance can call the same method in your parent, would it not be much better to have a base class that contains the method you want to call and then create child classes each can then still call the same method in the parent as long as it isn't private. If you want this method to be only available to the base class and all the derived classes then make it protected otherwise leave it public and everyone can then call it, if that's what you need.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class ObjectNode
{
public:
	ObjectNode();
	ObjectNode(char* _name);
	virtual ~ObjectNode();

	void foo();

	char* name;
};

class ObjectChildNode : public ObjectNode
{
public:
	ObjectChildNode();
	ObjectChildNode(char* _name);
	virtual ~ObjectChildNode();

};

ObjectNode::ObjectNode()
	: name(NULL)
{
}

ObjectNode::ObjectNode(char* _name)
	: name(_name)
{
}

ObjectNode::~ObjectNode()
{
}

void ObjectNode::foo()
{

}

ObjectChildNode::ObjectChildNode()
	: ObjectNode()
{
}

ObjectChildNode::ObjectChildNode(char* _name)
	: ObjectNode(_name)
{
}

ObjectChildNode::~ObjectChildNode()
{
}

int main()
{	
	ObjectChildNode child1("Child_1"), child2("Child_2");
	child1.foo();
	child2.foo();

	return 0;
}
Topic archived. No new replies allowed.