Problems with Polymorphism

Started by
4 comments, last by jmau0438 14 years ago
OK, this is a loaded question, so here I go. Objective: Up-cast a base object into a derived one Break-Down: Each object is a function object, meaning an object containing a single member function with a single argument. The hierarchy is as follows: 1)Function Object: Template Interface, contains a single virtual method called execute
template < typename TYPE, typename T > __interface iFunctionObject {

		public:

			virtual TYPE Execute ( T ) = 0;

	};

2)Strategy Object: An Abstract Interface derived from Function Object, member function remains virtual as it is not defined. Its purpose is primarily cosmetic as it helps in defining a common set of terminology.
template < typename TYPE, typename T > 
	__interface iStrategy : public iFunctionObject < TYPE, T > {};

3)Context Specific Definition: From iStrategy one last derived class is defined. Member function remains virtual with the only real change being an alias for the data to referenced within the member function and a default implementation that simply returns zero.
template < typename TYPE, typename T >
		class Operation : public iStrategy < TYPE, T > {

			public:

				virtual TYPE Execute ( T Stuff) { return 0; };

		};

4)Implementation: From this point I derive from "Operation" and overload the virtual function in each child object in order to perform a collection of algorithms, each require a single argument and return a value as specified in the template parameters. 5)Control Object: The controlling object possesses a data member that is an Operation Pointer. This pointer is meant to be Up-Casted into a specified Operation derivative. I'll spare you the details, but it involves a collection of enumerated values and a switch. PROBLEM: When I attempt to Up-Cast like so:
NewOperation < TYPE, T > *Ref;

						this->Op_ptr = dynamic_cast < Operation < TYPE, T > * > ( Ref );

This code compiles but crashes at run-time when invoking the "Execute" member function because it cannot locate it within the newly casted operation pointer. Can anyone help me with this?
Advertisement
Quote:Up-cast a base object into a derived one


This is same as saying: Extend hand forward and backward multiple times.

While the objective is: "Slice a loaf of bread".


Because honestly, between a bunch of non-descript interfaces, there is no clue to figure out what is going on or why.

Is it supposed to do something like boost::function?
Yes
I noticed that I did not initialize it, so I did. Error persists...

NewOperation < TYPE, T > *Ref = new NewOperation < TYPE, T >;this->Op_ptr = dynamic_cast < Operation < TYPE, T > * > ( Ref );
Quote:Original post by jmau0438
Yes


In that case there is a simpler solution.
struct Base {  virtual void execute() = 0;};template < class T, class P > struct Concrete : Base {  Concrete(T * pointer, P parameter) : ptr(pointer), p(parameter) {}  virtual void execute() {    ptr->foo(p);  }private:  T * ptr; // already typed  P p;};


The base class is concrete, invocations don't require templates. Parameter is stored during creation.

P can obviously be moved to Base, but that doesn't really add much. Same for T. The main problem is defining how to call the function, and that simply cannot be done without function pointer uglyness.

If both T and P are moved to base class, then there is no need for complex inheritance anymore, since everything is strongly typed again:
template < class T, class P > struct TypedBase {  virtual void execute(P param) = 0;protected:  T * ptr;};
Just extend this, and use ptr directly - it's strongly typed.

Quote:I noticed that I did not initialize it


I really don't see any need for any casts. Everything is strongly typed.
Nice solution, thanks. I'll give that a shot

This topic is closed to new replies.

Advertisement