Use a non-function member into a class

Oct 21, 2013 at 6:02pm
Hello, friends

I need help.

I mount a function (parameter - numeric vector; returns a string). However, this same function is used in several classes. To avoid that I keep duplicating the same code within these classes there is a way to do that as the code below?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
std::string func( const vector<int> vec ) {
  //processamento
  return result;
}

class A
{
  public:
   void member() {
    //processamento
    std::string str = func( vec );
  }
}

class B
{
  public:
   void member1() {
    //processamento
    std::string str1 = func( vec );
   }
}


Thanks for the help.
Oct 21, 2013 at 6:04pm
Stick it in a super class. Or rethink your design if a super class doesn't make sense.
Oct 21, 2013 at 6:14pm
Thanks, Resident for the reply. But the use superclass don't make sense for my code. Indeed, the functions member1() and member() are very different. In fact, the class A and B are very different too. I just avoid duplicate my code.
Oct 21, 2013 at 6:29pm
Then your design is weird. Classes shouldn't be calling on these external entities to perform internal tasks.
Oct 21, 2013 at 7:31pm
closed account (D80DSL3A)
Give each class a static pointer to the function?
Working example:
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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class A
{
  public:
  static std::string (*pFunc)( const vector<int> );

   void member() {
    //processamento
    vector<int> vec;
    std::string str = pFunc( vec );
    cout << str << endl;
  }
};

std::string func( const vector<int> vec ) {
  //processamento
  std::string result("A function");
  return result;
}

std::string (*A::pFunc)( const vector<int> ) = func;

int main()
{
	A a;
	a.member();
	cout << endl;
	return 0;
}

Output:
A function

Oct 21, 2013 at 7:56pm
> this same function is used in several classes.
> I keep duplicating the same code within these classes

If there is just one implementation of the function, just write it as a non-member function.
Declare it (typically in a header) and call it from anywhere you please.

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
#include <vector>
#include <string>

namespace util
{
    std::string func( const std::vector<int>& seq ) ;
}

struct A
{
    void member()
    {
        const auto str = util::func(vec) ;
        // ...
    }

    private: std::vector<int> vec;
};

struct B
{
    void member()
    {
        const auto str = util::func(vec) ;
        // ...
    }

    private: std::vector<int> vec;
};

Last edited on Oct 21, 2013 at 8:02pm
Oct 21, 2013 at 10:56pm
I really don't understand the problem here.
@OP: ¿what are the issues with your snip?

@Resident Biscuit: ¿why is it wrong to use a function?
If you want your data to be sorted, you'll call `std::sort()' ¿right?
Last edited on Oct 21, 2013 at 10:56pm
Topic archived. No new replies allowed.