1

Is it possible to have two functions with the different function name but the same functionality share the function body? And how can we do it?

template<typename _T>
class array {
public:
    _T operator+(_T concatinate_operand); // concatinate to the array
    _T append(_T concatinate_operand);
};
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • 12
    You can just have one call other. – François Andrieux Jun 13 '18 at 18:54
  • 1
    But you need different `+` operator overload semantic. I.e. something like `array& operator+(const array& rhs) { append(rhs.data_,rsh.length_); return *this; };` – Victor Gubin Jun 13 '18 at 19:11
  • Utterly and totally unrelated: Show a wee bit of fear any time you precede an upper case letter with an underscore. For why, read [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – user4581301 Jun 13 '18 at 19:42

1 Answers1

5

Yes, this is quite easy to accomplish. You just call the function the you do the actual implementation in from the other one. That would look like

template<typename _T>
class array {
public:
    _T operator+(_T concatinate_operand) { return append(concatinate_operand); } // concatinate to the array
    _T append(_T concatinate_operand) { /*actual logic here*/ }
};

Do note that if T is large then passing it by value and getting a copy will hurt the performance. If you use references like

template<typename _T>
class array {
public:
    _T& operator+(const _T& concatinate_operand) { return append(concatinate_operand); } // concatinate to the array
    _T& append(const _T& concatinate_operand) { /*actual logic here*/ }
};

You will avoid unnecessary copies.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • It would be odd for `operator+` to return a `T&`. Or maybe the oddity is that `operator+` and `append` are expected to do the same thing. – François Andrieux Jun 13 '18 at 19:20
  • `_T& operator+(const _T& concatinate_operand)` is actually bad idea :) `char * foo = nullptr; if(some) { array arr("abcd",5); foo = arr + "12345"; } delete [] foo;` – Victor Gubin Jun 13 '18 at 19:20
  • @VictorGubin What is that code suppose to be/do? It wouldn't compile and it seems to be trying to do terrible things. – François Andrieux Jun 13 '18 at 19:21
  • @VictorGubin That code won't work in the first place. – NathanOliver Jun 13 '18 at 19:21
  • @NathanOliver Exactly like member operator `+` returning reference on member pointer. And `foo = arr + "12345"` may work if not `template<> array { explicit array(const char* cstr) {} ` – Victor Gubin Jun 13 '18 at 19:27
  • 1
    @VictorGubin I'm sorry but I don't understand the point you are trying to make. – NathanOliver Jun 13 '18 at 19:29
  • @NathanOliver Check operator '+' result type, it returns reference on on template parameter type, when it should return reference on array class type. i.e. something like `array c = array("abcd",5) + array("fig",4);` – Victor Gubin Jun 13 '18 at 19:34
  • 1
    @VictorGubin Why? That is not what the OP has. It looks like the want to use `+` or `append` to add an element to the end of the array. – NathanOliver Jun 13 '18 at 19:36