The same member function for 100 classes

Hello all.

Is it possible to declare and define a function once:

1
2
3
4
5
6
int MyFunc (int a, int b)
{
  int c;
  c=a+b;
  return c;
}


And use it in classes, say MyClass1 to MyClass100, without defining again?

1
2
3
4
5
6
class MyClass1 {
    // ...
  public:
    int MyFunc(int,int);
   // ...
}


Compiler doesn't see MyFunc from within the class as expected. Is there a way to make it work?
Last edited on
You can define a function outside of a class, and then simply call it from any other function, whether in a class or not. You don't need to re-define the function 100 times.
Last edited on
"100 times"
I have an eerie feeling ...

Would you tell how MyClass2 and MyClass3 differ from each other?
100 very similar classes implies a design problem. I would post more words here to see if someone can give you a better approach. Its doable, depending on what you want, in a number of ways, but you are throwing up all kinds of red flags that something isnt quite right.
line 4 in your class snippet.....

friend int MyFunc(int,int);

@Ganado Thank you very much. It compiled and the exe worked. But it does not print anything. Am I missing something?

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
34
35
36
37
38
39
40
41
42
43
44
45
using namespace std;


int MyFunc(int a, int b)
{
	int c;
	c = a + b;
	return c;
}



class MyClass1 {
	// ...
public:
	int aa, bb;
	int cc;
	MyClass1(int, int);
	int sum(void);
	// ...
};

   MyClass1::MyClass1(int a0, int b0)
{
	aa = a0;
	bb = b0;
	cc = 0;
}

int MyClass1::sum(void)
{
	cc = MyFunc(aa, bb);
}




int main()
{
	MyClass1 mc1(2,3);
	mc1.sum();
	cout << "Print Test " << endl;
	cout << "Sum= " << mc1.cc << endl;
	return 0;
}



@keskiverto, @jonnin
Sorry about the number 100. I wrote it to emphasize my aim. But it will be definitely more than one. The function is coordinate transformation. It will transform coordinates in a class and transform displacements, forces, moment forces in other corresponding classes. It may increase.
Turn on compiler warnings. Your sum function requires an int to be returned, but you don't return anything. But beyond that, it #worksforme, because I see 5 printed. But it's undefined behavior, so anything could happen.

Also, small tip: Next time, if you post a compilable example, also post the #includes, as it makes it so we don't have to type them ourselves.
It may increase.
design as if it will, then.
you may want to use inheritance to re-use some code blocks, for example ... put ALL the reused stuff in one, and pull it into the others.... same idea just design it in by intent going forward.
Last edited on
@ Ganado:
It works now. Thank you. This is my first post, I will.

@jonnin:
I don't want to use classes because it will be multi multi inheritance which freaks me out. And there will be variable adjustments. May be I am thinking function oriented still because I just switched to object oriented.




And the working code is:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

using namespace std;


int MyFunc(int a, int b)
{
	int c;
	c = a + b;
	return c;
}



class MyClass1 {
	// ...
public:
	int aa, bb;
	int cc;
	MyClass1(int, int);
	void sum(void);
	// ...
};

MyClass1::MyClass1(int a0, int b0)
{
	aa = a0;
	bb = b0;
	cc = 0;
}

void MyClass1::sum(void)
{
	cc = MyFunc(aa, bb);
}




int main()
{
	MyClass1 mc1(2, 3);
	mc1.sum();
	cout << "Print Test " << endl;
	cout << "Sum= " << mc1.cc << endl;
	return 0;
}


(Edit: Visual Studio 2019 with clang 10.0)
Last edited on
Without headers that code does not compile.
@Furry Guy:
I got it now. Thanks. I added the .h content.
Topic archived. No new replies allowed.