Is this scenario possible

Hello guys, I have a weird question. Given this scenario:

we have a base class called letters;

and three derived classes, called A, B, and C that all share the same
functions.

Then lets say we have one more class called AddingThings. The AddingThings
class has a 9 static function that accepts two parameters (every combination that the three derived classes A,B, and C can make) and it runs the same set of commands for every class. The function looks similar to the line below except there are 9 of them, and the parameters change.

AddingThings::the_function(A A_object,B B_object)
AddingThings::the_function(A A_object,C C_object) .. ect

The question I am asking is, would it be possible to make just one function that could have the potential to accepts different classes as parameters (since they all are derived from a base class). That way, it would eliminate the need to have nine of these functions.

Sorry for my rambling, it's late where I am at, any input you guys may have would be appreciated, I'm doubting if this is even possible, but worth a try I suppose.
Why bother with polymorphism when you have templates? As long as you know what methods any object passed to the function should have, you can safely go ahead and use a template method instead of accepting the base class type and risk object slicing.

1
2
3
4
5
6
7
8
9
10
class AddThings
{
	public:
	template <typename A, typename B>
	static void the_function( const A &A_object, const B &B_object )
	{
		A_object.DoSomething;
		B_object.DoSomething;
	}
};


Full implementation can be found here:
http://coliru.stacked-crooked.com/a/5fa5b4c6c9fe3ebf
> would it be possible to make just one function that could have the potential to accepts different classes as parameters
> (since they all are derived from a base class).
> That way, it would eliminate the need to have nine of these functions.

If your design involves an object-oriented hierarchy of base and derived classes:
Write one function which accepts a pair of references (or pointers) to the base class.
(Which then calls virtual functions on the objects passed to it).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

struct letter
{
    virtual ~letter() = default ;
    virtual void foo() const = 0 ;
    virtual void bar() const = 0 ;
};

template < char TAG > struct canned_impl : letter
{
    virtual void foo() const override { std::cout << TAG << "::foo " ; }
    virtual void bar() const override { std::cout << TAG << "::bar " ; }
};

struct A : canned_impl<'A'> {} ;
struct B : canned_impl<'B'> {} ;
struct C : canned_impl<'C'> {} ;
struct D : canned_impl<'D'> {} ;

struct add_things
{
    static void the_fun( letter& x, letter& y ) // reference to base class
    { std::cout << "add_things::the_fun: " ; x.foo() ; y.bar() ; std::cout << '\n' ; }
};

int main()
{
    A a ; B b ; C c ; D d ;
    letter* seq[] = { &a, &b, &c, &d } ;

    for( letter* px : seq ) for( letter* py : seq ) add_things::the_fun( *px, *py ) ;
}

http://coliru.stacked-crooked.com/a/d53270acbb0f2b23
Thanks you guys for the responses, now I have a good starting point.
Topic archived. No new replies allowed.