I'm having trouble understanding overloaded operators

  • Thread starter firstlast1234567890
  • Start date
F

firstlast1234567890

Hello, I am trying to understand overloaded operators but am getting
confused as to how I can provide a conversion from my number class and
the scalar types?

If I create a class that represents a fictional number type, I
understand that I can create an overloaded assignment operator that
will accept long int for example. What I do not understand is how I
create an assignment operator to do the reverse, i.e. assign my
numeric class value to the scalar long int?

class myLong
{
public:
myLong()
: val(0)
{
};

myLong(long val)
: val(val)
{
};

myLong(const myLong& that)
: val(that.val)
{
};

myLong& operator=(const myLong& that)
{

if (this != &that)
{
this->val = const_cast<myLong&>(that).getVal();
}

return(*this);
};

long getVal()
{
return(val);
};

private:
long val;
};

So this works for something like

myLong mylong;
mylong = 42;
long scalarLong = mylong.getVal();
myLong newLong = mylong;
scalarLong++;
mylong = scalarLong;

But how do I do something like this

myLong myLongTest;
myLongTest = 1;
long scalarLong = myLongTest;

I do not know how to code this reverse assignment. I tried a few ways
but failed dismally.
 
T

Triple-DES

Hello, I am trying to understand overloaded operators but am getting
confused as to how I can provide a conversion from my number class and
the scalar types?

If I create a class that represents a fictional number type, I
understand that I can create an overloaded assignment operator that
will accept long int for example. What I do not understand is how I
create an assignment operator to do the reverse, i.e. assign my
numeric class value to the scalar long int?

class myLong
{
        public:
                myLong()
                        : val(0)
                {
                };

                myLong(long val)
                        : val(val)
                {
                };

                myLong(const myLong& that)
                        : val(that.val)
                {
                };

                myLong& operator=(const myLong& that)
                {

                        if (this != &that)
                        {
                                this->val = const_cast<myLong&>(that).getVal();
                        }

                        return(*this);
                };

                long getVal()
                {
                        return(val);
                };

        private:
                long val;

};

So this works for something like

myLong mylong;
mylong = 42;
long scalarLong = mylong.getVal();
myLong newLong = mylong;
scalarLong++;
mylong = scalarLong;

But how do I do something like this

myLong myLongTest;
myLongTest = 1;
long scalarLong = myLongTest;

I do not know how to code this reverse assignment. I tried a few ways
but failed dismally.

Add the following member:

operator long() { return val; }

But keep in mind that there are pitfalls associated with such user-
defined conversion operators.

DP
 
F

firstlast1234567890

Add the following member:

operator long() { return val; }

But keep in mind that there are pitfalls associated with such user-
defined conversion operators.

DP

That's silly, thank you so much, I just cannot see that in any
documentation. By "silly" I meant I really wanted to see a really
complex solution to justify my inability to work this out myself.
Could you recommend a good simple text that explains these sort of
things to someone who is just starting out? I have tried reading the
Kerningham reference that everyone alludes to but it is all Greek to
me.

Thank you.
 
F

firstlast1234567890

Triple-DES said:
Hello, I am trying to understand overloaded operators but am getting
[..]
Add the following member:
operator long() { return val; }

Nitpick:

operator long() const { return val; }

V

Now this is my point about needing a clear learning text in my
previous post to Triple-DES, I understand what you mean by the
addition of the const keyword here but I would not have thought to put
it there.
 
T

Triple-DES

That's silly, thank you so much, I just cannot see that in any
documentation. By "silly" I meant I really wanted to see a really
complex solution to justify my inability to work this out myself.
Could you recommend a good simple text that explains these sort of
things to someone who is just starting out? I have tried reading the
Kerningham reference that everyone alludes to but it is all Greek to
me.

Do you mean Kernighan & Ritchie's 'The C Programming Language'? That's
definitely not what you want, since it covers C, not C++. The
corresponding C++ book would be 'The C++ Programming Language' by
Bjarne Stroustrup.

DP
 
T

Triple-DES

Triple-DES said:
On 14 Mar, 16:03, (e-mail address removed) wrote:
Hello, I am trying to understand overloaded operators but am getting
[..]
Add the following member:
operator long() { return val; }

    operator long() const { return val; }

Now this is my point about needing a clear learning text in my
previous post to Triple-DES, I understand what you mean by the
addition of the const keyword here but I would not have thought to put
it there.

const-correctness should be covered in any introductory textbook worth
its money. To find a good book I highly recommend the book review
section at http://www.accu.org

DP
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top