[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

# function chaining

Posted on 1998-11-01
Medium Priority
295 Views
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.
the function inc() is supposed to add 1 to the instance.
Thank you for any help.
0
Question by:strmtrpr
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 3

Expert Comment

ID: 1176772
Your problem is not clear for me
0

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)
{
}

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

{
// Do something to add x to *this...
return *this;
}

fraction& fraction::inc()
{
}

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
###### Suggested Courses
Course of the Month13 days, 15 hours left to enroll