3

For example, I have the following Java code:

public class Main {
  public static void main(String[] args) {
    System.out.println(maker(Employee::new));
  }

  private static Employee maker(Supplier<Employee> fx) {
    return fx.get();
  }
}

class Employee {
  @Override
  public String toString() {
    return "A EMPLOYEE";
  }
}

What will be the C++ equivalent?

DuffyChan
  • 119
  • 1
  • 9

2 Answers2

4

Supplier is a function taking no arguments and returning some type: you can represent that with std::function:

#include <iostream>
#include <functional>
#include <memory>

// the class Employee with a "print" operator

class Employee
{
    friend std::ostream& operator<<(std::ostream& os, const Employee& e);
};

std::ostream& operator<<(std::ostream& os, const Employee& e)
{
    os << "A EMPLOYEE";
    return os;
}

// maker take the supplier as argument through std::function

Employee maker(std::function<Employee(void)> fx)
{
    return fx();
}

// usage

int main()
{
    std::cout << maker(
        []() { return Employee(); }
            // I use a lambda here, I could have used function, functor, method...
    );

    return 0;
}

I didn't use pointers here nor the operator new to allocate Employee: if you want to use it, you should consider managed pointers like std::unique_ptr:

std::unique_ptr<Employee> maker(std::function<std::unique_ptr<Employee>(void)> fx)
{
    return fx();
}

// ...

maker(
    []()
    {
        return std::make_unique<Employee>();
    }
);

Note: the call to operator<< should then be modified because maker will return a pointer instead of an object.

rocambille
  • 15,398
  • 12
  • 50
  • 68
2

You can use C++ template class to achieve the goal:

template<typename TR> class Supplier
{
  private:
    TR (*calcFuncPtr)();

  public:
    Supplier(TR(*F)())
    {
      calcFuncPtr = F;
    }

    TR get() const
    {
      return calcFuncPtr();
    }
};

Usage sample:

#include <string>
#include <iostream>

struct Employee
{
  public:
    std::string ToString()
    {
      return "A EMPLOYEE";
    }
};

Employee maker(const Supplier<Employee>& fx) 
{
  return fx.get();
}


Employee GetInstanceFunc()
{
  return Employee();
}

int _tmain(int argc, _TCHAR* argv[])
{
  Employee employee(maker(GetInstanceFunc));
  std::cout << employee.ToString();
  return 0;
}

Implicit type conversion at line maker(GetInstanceFunc) allows to use Supplier class without explicit template instantiation.

Nikita
  • 6,270
  • 2
  • 24
  • 37
  • Ofc supplier being an interface means passing it by value will slice the implementing object, better to pass it by reference, incase the derived class implements the get. – 1stCLord Aug 09 '16 at 07:53
  • @TheSombreroKid Thnx, fixed. – Nikita Aug 09 '16 at 08:06