A
Anthony Jones
Just a bit of background: I'm one of a group of FORTRAN programmers, looking
to switch to C++. We are trying to write a few simple examples to
demonstrate the power of the language to our manager, so he will send us all
on a conversion course.
One of many reasons is that our code is littered with examples of:
SUBROUTINE PRINT_ITEM(ITEM, ITEM_TYPE)
IF (ITEM_TYPE .EQ. SQUARE) THEN
CALL PRINT_SQUARE(ITEM)
ELSEIF (ITEM_TYPE .EQ. CIRCLE) THEN
CALL PRINT_CIRCLE(ITEM)
ELSEIF ...
(lots more item types)
ENDIF
END
(with apologies for sullying the group with FORTRAN code).
Obviously We need to find and modify all these blocks whenever we add a new
object type, or operation.
I want to write a C++ equivalent, using classes and member functions so that
I can print (or draw, or interrogate, or whatever...) an object without
knowing its type at runtime.
The latest of several attempts is shown below - the compiler complains about
the void* in the PrintObject function, though I thought I'd read that void*
could be used to mean "pointer to something, but I don't know what".
Can this code be modifed to get the effect I want? I'd like to avoid using
pointers to functions if possible.
Thanks!
#include <iostream.h>
// Class declarations
// ------------------
class Square{
public:
void Print();
};
void Square:rint(){
cout << "This is a square";
}
class Circle{
public:
void Print();
};
void Circle:rint(){
cout << "This is a circle";
}
// Print object function
// ---------------------
void PrintObject(void* object){
object->Print();
}
// Main Program
// ------------
int main(){
Square* square;
Circle* circle;
square = new Square;
circle = new Circle;
// Call member functions directly
circle->Print();
square->Print();
// Call member functions through PrintObject function
PrintObject(circle);
PrintObject(square);
return 0;
}
to switch to C++. We are trying to write a few simple examples to
demonstrate the power of the language to our manager, so he will send us all
on a conversion course.
One of many reasons is that our code is littered with examples of:
SUBROUTINE PRINT_ITEM(ITEM, ITEM_TYPE)
IF (ITEM_TYPE .EQ. SQUARE) THEN
CALL PRINT_SQUARE(ITEM)
ELSEIF (ITEM_TYPE .EQ. CIRCLE) THEN
CALL PRINT_CIRCLE(ITEM)
ELSEIF ...
(lots more item types)
ENDIF
END
(with apologies for sullying the group with FORTRAN code).
Obviously We need to find and modify all these blocks whenever we add a new
object type, or operation.
I want to write a C++ equivalent, using classes and member functions so that
I can print (or draw, or interrogate, or whatever...) an object without
knowing its type at runtime.
The latest of several attempts is shown below - the compiler complains about
the void* in the PrintObject function, though I thought I'd read that void*
could be used to mean "pointer to something, but I don't know what".
Can this code be modifed to get the effect I want? I'd like to avoid using
pointers to functions if possible.
Thanks!
#include <iostream.h>
// Class declarations
// ------------------
class Square{
public:
void Print();
};
void Square:rint(){
cout << "This is a square";
}
class Circle{
public:
void Print();
};
void Circle:rint(){
cout << "This is a circle";
}
// Print object function
// ---------------------
void PrintObject(void* object){
object->Print();
}
// Main Program
// ------------
int main(){
Square* square;
Circle* circle;
square = new Square;
circle = new Circle;
// Call member functions directly
circle->Print();
square->Print();
// Call member functions through PrintObject function
PrintObject(circle);
PrintObject(square);
return 0;
}