Your return value is the type of function that you declared.
For example if you make int sum(int x, int y) { return x+y; }
this function returns an integer.
Your return value is a set and that is a class type you defined.
It looks like you are just returning a value to say it did it. You could get away with declaring the function void and have no return value.
example
void multiply(const set &other)
but if you want it to return the value of the 2 sets multiplying then you want to return int for your num.
example
int multiply(const set &other)
{
return num*set.getNum();
}
I put the attribute getNum() in because num is declared private.
sfuo
Practically a Master Poster
656 posts since Jul 2009
Reputation Points: 164
Solved Threads: 99
Well it is definately wrong.
The reason is that the method say it will return an object of set, but -1, EXIT_SUCCESS etc is an integer so it will not compile.
The return from a function is based on what will happen later and how bad it is.
For example consider say set divide(const set& other)
in the above. [multiply is a bad choise since 0 * a number is ok.
What options do we have, well we could have[icode]this->num=5 other.num=2 [icode] that would be a slight problem, since 5/2 in the set of integers is 2. Maybe you need a warning? Or we could hvae other.num=0 and things have gone very wrong.
There are four main options:
(a) set an other variable : status and put the status of the result but always return a valid output.
(b) set the output (if it is a class) to some-predefined error status.
(c) throw an exception
(d) exit the code.
First off, (d) well if the whole thing has gone wrong nobody will want to be running the code, but it can be a bit drastic.
(c) This is a bit drastic still, because if there is no catch block then it is the same as (d), however it allows large objects to be propergated back, and clears up the memory properly in most cases [don't use in a constructor unless VERY careful, never use in a destructor.]
(b) Often acceptable, but beware that it is likely this should be tested for, that is often why exceptions are preferred since they force testing.
(a) Same as (b) but disjoint and that can cause some problems.
There are no hard rules, many many function return only their error status, and the other changes happen to function variables.
The STL uses both exceptions and valid but requiring checking objects, e.g. find returns the end() iterator when nothing is found.
It is a question of program architecture and likely user. E.g just you, some one using your library, etc.
StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
It depends : you have two options:
(a) leave the set unchanged. Simple do return *this; .
You will have to have an suitable copy constructor in you class but it is likely that you have one set(const set&)
(b) ok, big error, then throw RangeError; or some such exception. [Note you have two write a RangeError class or use a STL exception class] But be careful to ensure you catch it latter.
However, you cant return -1. That is not acceptable (unless you can cast -1 to a set??? -- you might have added a suitable operator -- doesn't seem like a good idea but .... )
StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138