function chaining

Posted on 1998-11-01
Medium Priority
Last Modified: 2010-04-02
I have a inline function that is to allow for function chaining. using an add function that is  already written.
this is what I have so far.
fraction & inc(){*this=add(*this,*this);return *this;}
the function inc() is supposed to add 1 to the instance.
Thank you for any help.
Question by:strmtrpr

Expert Comment

ID: 1176772
Your problem is not clear for me
LVL 11

Accepted Solution

alexo earned 200 total points
ID: 1176773
Hmmm...  Details missing.

First I assume you have a way of converting a constant number to a 'fraction' object.  E.g., via a constructor.

There are two possible cases:

(1) Let's assume add() is written like this:

    fraction add(const fraction& a, const fraction& b)
        fraction result;
        // Do something...
        return result;

Note that add returns BY VALUE and not BY REFERENCE.  That is because it creates a new object instead of modifying an existing one.  Therefore, inc() should not be implemented in terms of add().

Now, consider the other case:

(2) Let's assume add() is written like this:

    fraction& add(fraction& obj, const fraction& x)
        // Do something...
        return obj;

In this case, I assume modifies the first argument (and also returns the result BY REFERENCE, since no new object is created).  If that is the case, inc() could be written like this:

    fraction& inc(const fraction& obj)
        return add(obj, 1);

However, if inc() and add() modify the object, it will be much better to implement them as member functions:

    fraction& fraction::add(const fraction& x)
        // Do something to add x to *this...
        return *this;

    fraction& fraction::inc()
        return add(1);

But if that's the case, why not use operators?

    fraction& fraction::operator+=(const fraction& x)
        // Do something to add x to *this...
        return *this;

    fraction& fraction::operator++(int) // postfix increment
        return operator+=(1);

Now you can write:

    fraction x, y;
    // ...
    x += y;

Hope it helps.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

586 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question