Access a class as if it were POD

S

Simon Elliott

I've got a requirement to implement a C++ class which should be usable
as if it were an unsigned long:

Foo my_foo;

my_foo = 1;

if (my_foo < 1000)
{
// do this
}

etc.

Is there a simple way I can do this with references, or do I need to
overload every operator under the sun?
 
M

Mike Wahler

Simon Elliott said:
I've got a requirement to implement a C++ class which should be usable
as if it were an unsigned long:

Foo my_foo;

my_foo = 1;

if (my_foo < 1000)
{
// do this
}

etc.

Is there a simple way I can do this
Yes.

with references,

You don't need references (but you can use them if you like).
or do I need to
overload every operator under the sun?

No.

Hints:

Foo::Foo(unsigned long){}
Foo::eek:perator unsigned long(){}

This may or may not be 'cheating', depending upon more
specific defintion of your requirements.

-Mike
 
S

Simon Elliott

Hints:

Foo::Foo(unsigned long){}
Foo::eek:perator unsigned long(){}

This may or may not be 'cheating', depending upon more
specific defintion of your requirements.

Thanks for this. I'd expect I'll also need a
Foo::eek:perator=(unsigned long){}
 
M

Mike Wahler

Simon Elliott said:
Thanks for this. I'd expect I'll also need a
Foo::eek:perator=(unsigned long){}

Not necessarily. Remember what a default compiler synthesized
assignment operator will do. It's issues like this that caused
my 'possible cheat' remark above -- we're simply delegating the work
to a built-in type, and depending upon already defined operations.
IOW a 'wrapper'.

-Mike
 

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

No members online now.

Forum statistics

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

Latest Threads

Top