Thomas said:
Hi there,
I've got a class with an array of pointers to objects of the same class.
This does not seem to be what you have at all, so either your
description is off or your code is. I'm not entirely sure which.
I
thought of doing it like that:
class test {
test **array_var;
This is, of course, not an array at all, but a pointer. Now, if I assume
the description you gave is accurate, then I don't think you want
'array_var' to be of type 'test **', but rather of type 'test *'.
...
};
void method::test(test object)
You haven't given any information about this 'method' class (or namespace?).
{
children = (Baum**) realloc(children, sizeof(Baum**));
A few problems here:
1) This is an unsafe way of using realloc. If the reallocation fails,
you lose the original memory. The only safe way to use realloc is to
store the resulting value in a new temporary pointer, then assign it to
the original pointer if and only if the allocation succeeds.
2) Please don't use realloc in C++. It's dangerous, error-prone, won't
work correctly for non-POD types, and there's a better alternative:
std::vector.
Also, having no idea what 'children' or 'Baum' are, I can't properly
diagnose problems with this code.
children[elements] = (Baum*) malloc(sizeof(Baum*));
children[elements] = &obj;
One of the problems here should be obvious. On one line you store a
pointer to some new memory. On the next you overwrite that pointer. You
have leaked the memory you allocated.
But again, I can't properly diagnose problems due to lack of information.
}
test a;
test b;
a.method(b);
More stuff I don't know about: the 'method' member of test.
I tried to access b like:
a.children[0].var_of_b;
And again: the 'children' member of test.
but it did not work. So how can I manage it?
I really don't know what you are doing, so I can't say. Honestly, I
suspect the code you posted is far from the code you are actually
attempting to use. It kind of looks like you've changed some variable
names and transposed some identifiers.
Post real code, a minimum complete program, and we can probably help.
With what you've given us... there's basically nothing we can do.
-Kevin