using templates: how can i do a parameter for accept a function name?

Recommended Answers

All 6 Replies

I'm afraid your question is too unclear. Please give an example of what you would like to achieve.

imgaine that you create a function, 1 parameter must recive another function name. how can i create that parameter?
i have 1 property template class(i think you that), but i'm update it. so, like you know for a property we have 2 functions: getfunctionname and setfunction name. what i need to know is how i can create the parameter for recive the function name

Do you mean something like this?

    template <typename T>
    double func1(const T & f, double x) 
    {
      return f(x);
    }

    double square(double x)
    {
      return x*x;
    }

    struct cube
    {
      double operator()(double x) const 
      {
        return x*x*x;
      }
    };

    int main()
    {
      double x = 2;
      cube c;

      cout << func1(square, x) << endl;
      cout << func1(cube(), x) << endl;
      cout << func1(c, x) << endl;

      return 0;
    }

sorry not that:(
see my tamplate class:

template <typename Container, typename ValueType, int nPropType=3>
class property
{
public:
    property()
    {
        m_cObject = NULL;

            Set = NULL;

            Get = NULL;
    }


    //-- This to set a pointer to the class that contain the
    //   property --
    void setContainer(Container* cObject)
    {
        m_cObject = cObject;
    }


    //-- Set the set member function that will change the value --
    void setter(void (Container::*pSet)(ValueType value))
    {
        if((nPropType == WRITE_ONLY) || (nPropType == READ_WRITE))
            Set = pSet;
        else
            Set = NULL;
    }


    //-- Set the get member function that will retrieve the value --
    void getter(ValueType (Container::*pGet)())
    {
        if((nPropType == READ_ONLY) || (nPropType == READ_WRITE))
            Get = pGet;
        else
            Get = NULL;
    }


    //-- Overload the '=' sign to set the value using the set
    //   member --
    ValueType operator =(const ValueType& value)
    {
        assert(m_cObject != NULL);
        assert(Set != NULL);
        (m_cObject->*Set)(value);
        return value;
    }

    //-- To make possible to cast the property class to the
    //   internal type --
    operator ValueType()
    {
        assert(m_cObject != NULL);
        assert(Get != NULL);
        return (m_cObject->*Get)();
    }
    friend istream &operator>>( istream  &input, property &d )
    {
        ValueType v;
        input >>v;
        d = v;
        return input;
    }


    friend istream& getline (istream&  is, property& str)
    {
        ValueType v;
        getline(is,v);
        str = v;
        return is;
    }

private:
  Container* m_cObject;  //-- Pointer to the module that
                         //   contains the property --
  void (Container::*Set)(ValueType value);
                         //-- Pointer to set member function --
  ValueType (Container::*Get)();
                         //-- Pointer to get member function --
};

like you see, i have getter() and setter() functions that accept functions names. but i need is put them in header:

template <typename Container, typename ValueType,typename getfunction, typename sefunction, int nPropType=3>
    class property
    {
    ................

why??? for avoid that 2 functions(i what more than these, but i think you understand what i mean)

heres how we can do a function parameter:

void (Container::*setptr)(ValueType t)

or

ValueType (Container::*getptr)()

(remember these way only use the function name)

sample:

&classname::setfuntionname

i have 1 question: how can i make it optional? or igual a NULL\nullptr?

the only way is to use the argument(not parameter) 'nullptr':
instead use the functioname, we use the 'nullptr' and works fine(i have tested).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.