Hello I am in need of information.

I have the following:

xx.func(1).func(2).func(3)

how should the func be written for it to work?

Thanks

Recommended Answers

All 4 Replies

func() must be a member function of the class type for xx and return an object or reference to itself so that the the same member function can be called on the result. One example might look like this:

#include <iostream>

using namespace std;

class Foo
{
    int _value;
public:
    Foo(): _value(0) { }

    int Value() { return _value; }

    Foo& func(int increment)
    {
        _value += increment;
        return *this;
    }
};

int main()
{
    Foo xx;

    xx.func(1).func(2).func(3);
    cout << xx.Value() << endl;
}

If it has the dots like that, then it's probably a templated class or a class that takes an int in the constructor..

class xx
{
    public:
        xx();
        ~xx();

        class func// : public xx
        {
            func(int Value){/*do something with value here*/}
            ~func(){}

            operator ()(int Value)
            {
                /*do something with value here*/
            }
        };
};

xx::xx(){}
xx::~xx(){}


int main()
{
    xx meh;
    meh.func(1);
    meh.func(2);
    meh.func(3);
}

I don't know if you can chain them like how you have, but this is a pretty good alternative. Maybe use inheritance and polymorphism if you want that behavior?

EDIT: NVM.. I see it's done already.

This is the function:

template <class T>
T& MinMaxVector<T>::push_back(const T &d)
{
    v[sp++]=d;
    return *this;
}

the error:

request for member 'push_back' in 'mc.MinMaxVector<T>::push_back [with T = char](((const char&)((const char*)(&'h'))))', which is of non-class type 'char'|

Why is that?

The result type of the function should be changed:

template <class T>
MinMaxVector<T>& MinMaxVector<T>::push_back(const T &d)
{
    v[sp++]=d;
    return *this;
}
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.