Solved

function chaining

Posted on 1998-11-01
2
254 Views
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.
0
Comment
Question by:strmtrpr
2 Comments
 
LVL 3

Expert Comment

by:Norbert
Comment Utility
Your problem is not clear for me
0
 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
Comment Utility
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;
    ++x;

Hope it helps.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now