J
Joseph Turian
Fellow hackers,
I have a class BuildNode that inherits from class Node.
Similarly, I have a class BuildTree that inherits from class Tree.
Tree includes a member variable:
vector<Node> nodes; // For clarity, let this be "orig_nodes"
BuildTree includes a member variable:
vector<BuildNode> nodes; // For clarity, let this be "build_nodes"
So as far as I can tell, this overloads "nodes", defining two different
"nodes" variables instead of having build_nodes clobber orig_nodes (as
was my intention).
Now, let's say I have a Tree method defined:
bool Tree::is_degenerate const { return nodes.empty(); }
If I have a BuildTree object build_tree, and I call
build_tree.is_degenerate(), it returns "orig_nodes.empty()", not
"build_nodes.empty()" as is desired.
So the question is:
Is there any way to define methods in Tree that reference "nodes", and
get those methods to act upon "build_nodes" if the calling object is a
BuildTree and "orig_nodes" if the calling object is a Tree, without
duplicating the code in BuildTree? [I was thinking maybe make a virtual
method "get_nodes()", which in a Tree object returns orig_nodes and in
a BuildTree object returns "build_nodes", and then call get_nodes()
whenever I would normally use "nodes" in Tree methods. Would this
work?]
I don't mind refactoring my code to get this right, if anyone can
suggest a good way to redesign these objects.
Sub-question: Having two "nodes" objects with the same name in a
BuildNodes object is a really good way to shoot oneself in the foot.
(I'm surprised the code worked for so long before I found this issue
earlier today.) Is there a solution in which I can do away with this
name clash entirely?
Best,
JOSEPH
I have a class BuildNode that inherits from class Node.
Similarly, I have a class BuildTree that inherits from class Tree.
Tree includes a member variable:
vector<Node> nodes; // For clarity, let this be "orig_nodes"
BuildTree includes a member variable:
vector<BuildNode> nodes; // For clarity, let this be "build_nodes"
So as far as I can tell, this overloads "nodes", defining two different
"nodes" variables instead of having build_nodes clobber orig_nodes (as
was my intention).
Now, let's say I have a Tree method defined:
bool Tree::is_degenerate const { return nodes.empty(); }
If I have a BuildTree object build_tree, and I call
build_tree.is_degenerate(), it returns "orig_nodes.empty()", not
"build_nodes.empty()" as is desired.
So the question is:
Is there any way to define methods in Tree that reference "nodes", and
get those methods to act upon "build_nodes" if the calling object is a
BuildTree and "orig_nodes" if the calling object is a Tree, without
duplicating the code in BuildTree? [I was thinking maybe make a virtual
method "get_nodes()", which in a Tree object returns orig_nodes and in
a BuildTree object returns "build_nodes", and then call get_nodes()
whenever I would normally use "nodes" in Tree methods. Would this
work?]
I don't mind refactoring my code to get this right, if anyone can
suggest a good way to redesign these objects.
Sub-question: Having two "nodes" objects with the same name in a
BuildNodes object is a really good way to shoot oneself in the foot.
(I'm surprised the code worked for so long before I found this issue
earlier today.) Is there a solution in which I can do away with this
name clash entirely?
Best,
JOSEPH