C++ class pointer (??) problem.

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

Alf P. Steinbach

* Thomi Richards:
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?

Well, you don't want the template solution, which is the cleanest one,
and may actually generate the least code, depending on your compiler.

C++ does not offer ready-made objects that represent classes. Such an
object would itself be of a meta-class, and C++ does not have meta-classes.
But all you need is an object factory.

In your example, where the constructors are responsible for the actions
(that's a pitfall, by the way; I'd suggest an operator() or better yet
a named member function):

class BaseOperation
{
protected:
BaseOperation() {};
public:
typedef std::auto_ptr<BaseOperation> AutoPtr;

struct Factory
{
virtual AutoPtr newObject( double a, double b ) const = 0;
};
};

class OpAdd: public BaseOperation
{
protected:
OpAdd( double a, double b ) { std::cout << a+b << std::endl; };
public:
typedef std::auto_ptr<OpAdd> AutoPtr;

struct Factory: BaseOperation::Factory
{
virtual AutoPtr newObject( double a, double b ) const
{
return AutoPtr( new OpAdd( a, b ) );
}
};
};

void execute(
double a,
double b,
BaseOperation::Factory const& opFactory
)
{
opFactory.newObject( a, b );
}

int main()
{
execute( 3, 5, OpAdd::Factory() );
}

Note 1: untested code.

Note 2: surely this can not be what you want, C++ is not Python.

Any other pitfalls, or better ways to accomplish this?

Assuming you just want to pass an operation as argument, do just that:

typedef double Op( double, double );

void execute( double a, double b, Op op )
{
std::cout << op( a, b ) << std::endl;
}

double add( double a, double b ){ return a+b; }

int main()
{
execute( 3, 5, add );
}

Note 3: untested code.
 
R

Rolf Magnus

Thomi said:
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.

Why don't you want to pass an instance? In C++, a class cannot be a function
parameter. In python, everything is an object. Even a class is just an
instance in python. Therefore, you can pass it as argument to a function.
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 ;) )

What exactly do you mean?
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.

Then you would need something like a class object. I'd say templates would
still be a lot more compact. Anyway, are you saying that out of experience
or are you just speculating?
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?) ?

An object pointer :)
 
T

Thomi Richards

Rolf Magnus said:
Why don't you want to pass an instance? In C++, a class cannot be a function
parameter. In python, everything is an object. Even a class is just an
instance in python. Therefore, you can pass it as argument to a function.

I'm constructing a Model / Control / View structure - THe model is an
internal class that should never be interfaced with directly. The
control is already constructed, and should be the main (read: only)
entry point into the system. THe idea is that third party programmers
can create their own "view" classes, and pass them to the control,
which will sort out their construction, and bind them to the model.
Then you would need something like a class object. I'd say templates would
still be a lot more compact. Anyway, are you saying that out of experience
or are you just speculating?

I'm speculating. Do you think that templatesa re the way to go?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,107
Latest member
AmeliaAmad

Latest Threads

Top