T
Thomi Richards
Hi,
I've been working with C++ for a bit now, and I've hit a snag; I'm
hoping you can help me out with this problem:
In python, the following code is valid:
<code>
class baseOperation:
def __init__(self, num1, num2):
pass
class opAdd(baseOperation):
def __init__(self, num1, num2):
print num1 + num2
class opMultiply(baseOperation):
def __init__(self, num1, num2):
print num1 * num2
</code>
And is also trivial to implement in C++. In python, the following
function works too:
<code>
def Execute(num1, num2, operation):
operation(num1, num2)
</code>
this way, I can type "Execute(3,2,opAdd)", and "5" will be printed.
The reason for doing this is so I can add operations at a later point
in time, and I can still use the same old Execute() function (thereby
reducing the number of new lines of code needed).
I'm trying to do something very similar in C++ - I have a number of
classes all derived from a pure abstract class. I wantto be able to
pass those classes (not instances of the classes, the classes
themselves) to a function, and have that function create an instance
of the class, and manipulate it as need be.
It's very important that The programmer never has to deal with the
actual construction of the class - instead, that is left to the
builder function (in the python example, the "Execute" function
created an instance of the class - imagine a situation 100 times more
complicated than that
)
Another way to do this would be using a template function for the
builder function. One problem with this approach is thatthe builder
function will be called multiple times with different classes; using a
template function would waste code space, whereas a class-pointer (or
something similar) would probably be more compact.
So, I hope I've explained myself reasonably well... I know I've seen
it before, I'm just not sure about the syntax. Specifically, what is
the type of the class being passed in (something akin to a function
pointer?) ? Does the syntax for using the class within the builder
function vary at all? Any other pitfalls, or better ways to accomplish
this?
Thanks..
I've been working with C++ for a bit now, and I've hit a snag; I'm
hoping you can help me out with this problem:
In python, the following code is valid:
<code>
class baseOperation:
def __init__(self, num1, num2):
pass
class opAdd(baseOperation):
def __init__(self, num1, num2):
print num1 + num2
class opMultiply(baseOperation):
def __init__(self, num1, num2):
print num1 * num2
</code>
And is also trivial to implement in C++. In python, the following
function works too:
<code>
def Execute(num1, num2, operation):
operation(num1, num2)
</code>
this way, I can type "Execute(3,2,opAdd)", and "5" will be printed.
The reason for doing this is so I can add operations at a later point
in time, and I can still use the same old Execute() function (thereby
reducing the number of new lines of code needed).
I'm trying to do something very similar in C++ - I have a number of
classes all derived from a pure abstract class. I wantto be able to
pass those classes (not instances of the classes, the classes
themselves) to a function, and have that function create an instance
of the class, and manipulate it as need be.
It's very important that The programmer never has to deal with the
actual construction of the class - instead, that is left to the
builder function (in the python example, the "Execute" function
created an instance of the class - imagine a situation 100 times more
complicated than that
Another way to do this would be using a template function for the
builder function. One problem with this approach is thatthe builder
function will be called multiple times with different classes; using a
template function would waste code space, whereas a class-pointer (or
something similar) would probably be more compact.
So, I hope I've explained myself reasonably well... I know I've seen
it before, I'm just not sure about the syntax. Specifically, what is
the type of the class being passed in (something akin to a function
pointer?) ? Does the syntax for using the class within the builder
function vary at all? Any other pitfalls, or better ways to accomplish
this?
Thanks..