Different classes/structures as function parameters?

Hi, programmers, I have question once again, this time it´s about classes and structures. So, I know it´s OK do something like this:
1
2
3
4
5
6
7
struct MyStruct{
	int a,b,c;
};

void MyFunc(MyStruct my_struct_object){
	// do something
}


However if I´d like to take different structures as parameters, I have to do something like this...
1
2
3
4
5
6
7
8
9
10
11
12
13
struct MyStruct{
	int a,b,c;
};
struct MyStruct2{
	int a,b,c;
}

void MyFunc(MyStruct my_struct_object){
	// do something
}
void MyFunc2(MyStruct2 my_struct_object2){
	// do something
}

Any easier way around?
I don't follow. Where's the difficulty?

you mean you don't want so many functions for each struct and only a single function shall do the work for you?
@writetonsharma

Yes, that is exactly what I ment. Less code, more efficency. That´s the way I like to code.

And Bazzy, I´m not sure about the templates, because you still should have to specialize them separately like I´ve separately defined some already.

Oh, and I forgot to mention one thing: structures I´d like to use are different yes, but they contain similiar functionality - let me demonstrate:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct MyStruct{
	int a,b,c;
};
struct MyStruct2{
	int a,b,c;
}

void MyFunc(MyStruct my_struct_object){
	my_struct_object.a = 10;

}
void MyFunc2(MyStruct2 my_struct_object2){
	my_struct_object.a = 10;
}

See? I have two objects, and I´d like to modify their content from one single function definition and declaration in a similiar way. It feels like a waste of code declare so many functions for each class or struct, unless there is some way to set struct itself as parameter... Hmm...
I hope you understand what I´m looking after. I need help!
You don't have to specialize templates if the interface is the same for both classes.
Unfortunately I don´t understand exactly what do you mean, Bazzy... Are you talking about inheritance and/or abstract classes? Please show me/us some code, I´m a visual person and I´ll get things better when I can see something. Thanks for your effort.
1
2
3
4
template < class T >
    void MyFunc ( T my_struct_object ){
        my_struct_object.a = 10;
}
Will work for any type with an integer-reference-compatible member called 'a'
OK, I got this thing this far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

template <class T>
    void MyFunc (T my_struct_object){
        my_struct_object.a = 10;
}

struct MyStruct{
	int a;
};

int main(){
	MyStruct my_struct;
	MyStruct &my_struct2 = my_struct;
	MyFunc<MyStruct>(my_struct2);
	cout << my_struct.a << endl;
	return 0;
}

But there´s one problem: the value it is supposed to print is invalid (1076296996) instead of 10 of yours. Man, my code has problems... I got the idea and learnt something cool (thanks Bazzy), but my reference may need some fixes yet. Any help, please?

That's fine. but all the structure's cannot have the same parameters. if the structure's change, your template class will be non usable. you may need a way to identify the objects.
Alright, I figured this one out; I was thinking too complexly albeit the solution would have been this simple - just add the reference operator right at the beginning of object parameter and cut off the other crap out of the main function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

template <typename T>
    void MyFunc (T &my_struct_object){
        my_struct_object.a = 10;
}

struct MyStruct{
	int a;
};

int main(){
	MyStruct my_struct;
	MyFunc<MyStruct>(my_struct);
	cout << my_struct.a << endl;
	return 0;
}

The power of reference operator (&) is ultimate! Print working fine.

Thanks, Bazzy, problem solved just as I wished.
IMHO, a more formal & elegant approach was to use a base class. I mean, you say that different structures have a common set of attributes. To me that just cries for a base class. But that's just me, I guess.
I understand what you mean - the inheritance, and yes, it is good idea indeed. Of course in code above it doesn´t work, but often times it really helps you out. Nice suggestion, webJose!
Topic archived. No new replies allowed.