Overloading unary minus for use in function calls

M

Matthew Cook

I would like to overload the unary minus operator so that I can negate
an instance of a class and pass that instance to a function without
creating an explicit temporary variable. Here is an example:

#include <iostream>
using namespace std;

class Object
{
public:
int number;
Object(int value);
const Object operator- ();
//friend const Object operator- (const Object& o);

};

Object::Object(int value) :
number (value)
{}

const Object Object::eek:perator- ()
{
cout << "using class minus" << endl;
number = -number;
return *this;
}
/*
const Object operator- (const Object& o)
{
cout << "using friend minus" << endl;
return Object(-o.number);
}*/

void useAnObject(Object& o)
{
cout << "using object with number: " << o.number << endl;
}

int main(int argc, char* argv[])
{
Object a(4);
useAnObject(-a); // does not work but would like it to
//Object b = -a; // works, but I don't like it
//useAnObject(b);
}

When I compile this, I get the following errors (gcc 4.0.1):
c++ unaryminus.cpp -o unaryminus
unaryminus.cpp: In function 'int main(int, char**)':
unaryminus.cpp:39: error: invalid initialization of non-const reference
of type 'Object&' from a temporary of type 'const Object'
unaryminus.cpp:31: error: in passing argument 1 of 'void
useAnObject(Object&)'

I have tried various combinations of returning void, returning const
Object, returning non-const Object, returning reference to Object, etc,
but i get similar errors. Is there a way to do this?

-Matthew
 
I

IR

Matthew said:
I would like to overload the unary minus operator so that I can
negate an instance of a class and pass that instance to a function
without creating an explicit temporary variable. Here is an
example:

#include <iostream>
using namespace std;

class Object
{
public:
int number;
Object(int value);
const Object operator- ();

operator -() should not modify the object, so it should be declared
const.
Also, are you sure you are willing to return a const Object?

I'd rather declare it:

Object operator -() const;

//friend const Object operator- (const Object& o);

};

Object::Object(int value) :
number (value)
{}

const Object Object::eek:perator- ()
{
cout << "using class minus" << endl;
number = -number;
return *this;
}

Object Object::eek:perator -() const
{
cout << "Object::eek:perator -()" << endl;
return Object(-number);
}
/*
const Object operator- (const Object& o)
{
cout << "using friend minus" << endl;
return Object(-o.number);
}*/

void useAnObject(Object& o)
{
cout << "using object with number: " << o.number << endl;
}

int main(int argc, char* argv[])
{
Object a(4);
useAnObject(-a); // does not work but would like it to

It works now :p
//Object b = -a; // works, but I don't like it
//useAnObject(b);
}

When I compile this, I get the following errors (gcc 4.0.1):
c++ unaryminus.cpp -o unaryminus
unaryminus.cpp: In function 'int main(int, char**)':
unaryminus.cpp:39: error: invalid initialization of non-const
reference of type 'Object&' from a temporary of type 'const
Object' unaryminus.cpp:31: error: in passing argument 1 of 'void
useAnObject(Object&)'

The compiler said it: your operator -() returned a const Object,
while useAnObject expected an Object&. How could the compiler cast
the const away?


Cheers,
 
I

Ian Collins

Matthew said:
void useAnObject(Object& o)
{
cout << "using object with number: " << o.number << endl;
}

int main(int argc, char* argv[])
{
Object a(4);
useAnObject(-a); // does not work but would like it to
//Object b = -a; // works, but I don't like it
//useAnObject(b);
}

When I compile this, I get the following errors (gcc 4.0.1):
c++ unaryminus.cpp -o unaryminus
unaryminus.cpp: In function 'int main(int, char**)':
unaryminus.cpp:39: error: invalid initialization of non-const reference
of type 'Object&' from a temporary of type 'const Object'
unaryminus.cpp:31: error: in passing argument 1 of 'void
useAnObject(Object&)'

I have tried various combinations of returning void, returning const
Object, returning non-const Object, returning reference to Object, etc,
but i get similar errors. Is there a way to do this?
The compiler is telling you that you can't bind a temporary object to a
non const reference. You are also attempting to assign a cost reference
to a reference.

void useAnObject( const Object& o)

will see you right.
 
G

Gianni Mariani

Matthew said:
I would like to overload the unary minus operator so that I can negate
an instance of a class and pass that instance to a function without
creating an explicit temporary variable. Here is an example:

#include <iostream>
using namespace std;

class Object
{
public:
int number;
Object(int value);
const Object operator- ();

Why are you returning a "const Object" ? You may as well return an Object.

I also suspect that "- OBJ" should not change OBJ. So operator- should
be a const function.

i.e.
Object operator- () const;
//friend const Object operator- (const Object& o);

};

Object::Object(int value) :
number (value)
{}

const Object Object::eek:perator- () Object operator- () const
{
cout << "using class minus" << endl;
number = -number;
Can't be messing with number.
return *this;
Return a different Object.

return Object( -number );
}
/*
const Object operator- (const Object& o)
{
cout << "using friend minus" << endl;
return Object(-o.number);
}*/

void useAnObject(Object& o)

If you want to accept references to temporaries as arguments, you must
make them "const" temporaries.

void useAnObject(const Object& o)

{
cout << "using object with number: " << o.number << endl;
}

int main(int argc, char* argv[])
{
Object a(4);
useAnObject(-a); // does not work but would like it to
//Object b = -a; // works, but I don't like it
//useAnObject(b);
}

When I compile this, I get the following errors (gcc 4.0.1):
c++ unaryminus.cpp -o unaryminus
unaryminus.cpp: In function 'int main(int, char**)':
unaryminus.cpp:39: error: invalid initialization of non-const reference
of type 'Object&' from a temporary of type 'const Object'
unaryminus.cpp:31: error: in passing argument 1 of 'void
useAnObject(Object&)'

I have tried various combinations of returning void, returning const
Object, returning non-const Object, returning reference to Object, etc,
but i get similar errors. Is there a way to do this?

#include <iostream>
using namespace std;

class Object
{
public:
int number;
Object(int value);
Object operator- () const;
//friend const Object operator- (const Object& o);

};

Object::Object(int value) :
number (value)
{}

Object Object::eek:perator- () const
{
cout << "using class minus" << endl;
return Object( -number );
}
/*
const Object operator- (const Object& o)
{
cout << "using friend minus" << endl;
return Object(-o.number);
}*/

void useAnObject(const Object& o)
{
cout << "using object with number: " << o.number << endl;
}

int main(int argc, char* argv[])
{
Object a(4);
useAnObject(-a); // does not work but would like it to
//Object b = -a; // works, but I don't like it
//useAnObject(b);
}
 
I

IR

Ian said:
The compiler is telling you that you can't bind a temporary object
to a non const reference.

Damn, I missed this one... and VC8 didn't even blink on it :-/


Cheers,
 
M

Matthew Cook

Thanks to everyone for the quick reply.

I was returning const Objects on the advice of Effective C++'s Item 3
(top of pg 13, possibly over-liberally applied in this case).

I guess what I was really trying for was a function to negate the
object in place rather than making a copy. However the more I look at
things, the less this seems to fit with the expected behavior of
overloading this operator. I think an explicit negate function might
be more appropriate.

For those who may find their way across this post in the future, IR's
raises a good point in the prev post. This seem to only work when the
temporary object created is passed as constant regardless of the return
type of the operator-. It seems to be the case that implicit temporary
variables like this are always const (perhaps someone can quote chapter
and verse from the standard.) However, I was sure I'd done this on
VC++ in the past. It appears the case may have changed. Google
provides this tidbit from microsoft:

http://msdn2.microsoft.com/en-US/library/cfbk5ddc(vs.80).aspx

Many Thanks,
-Matthew
 
I

IR

Matthew said:
For those who may find their way across this post in the future,
IR's raises a good point in the prev post.

I did not raise that issue. Ian Collins and Gianni Mariani did. I
was only a victim of both my compiler and my thoughtlessness. ;-)
This seem to only work
when the temporary object created is passed as constant regardless
of the return type of the operator-. It seems to be the case that
implicit temporary variables like this are always const (perhaps
someone can quote chapter and verse from the standard.) However,
I was sure I'd done this on VC++ in the past. It appears the case
may have changed. Google provides this tidbit from microsoft:

http://msdn2.microsoft.com/en-US/library/cfbk5ddc(vs.80).aspx

Although I can't quote the relevant paragraph from the standard, the
link you provided is right (in theory, not in practice).

VC8 didn't generate an error (as it should, to my understanding)
either with the code I posted earlier, nor with the following code:

class C {};
void f(C & c) {}

int main()
{
f(C());
}

(of course, Comeau correctly identifies the error)


FWIW, I figured out that VC8 generates this error only when M$
language "extensions" are disabled. Otherwise, it only issues a
warning when at maximum warning level (4).

What got me wrong is that I always keep those extensions enabled
(because when disabled, Windows headers simply don't compile), and
that my test project wasn't at warning level 4 contrary to my habit.

I guess this will teach me to *always* double-check that my compiler
options are the strictest possible... :)


Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top