Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Operator Cast () Reference? (http://www.velocityreviews.com/forums/t725172-operator-cast-reference.html)

Immortal Nephi 06-10-2010 01:25 AM

Operator Cast () Reference?
 
Someone posted his Byte class code on the previous thread. I have a
question about operator cast (). Please explain the difference
between local object and reference object. Why do you need reference
object?
The object is still the same if you remove ampersand between operator
unsigned char and ().

class Byte {
public:
Byte( unsigned char& c ) : c_( c ) {
}

Byte& operator=( unsigned char val ) {
c_ = val;
return *this;
}

operator unsigned char&() { // ???? reference ????
return c_;
}

// operator unsigned char() { // ???? local ????
// return c_;
// }

private:
unsigned char& c_;
};

int main() {
unsigned char data = 0x41, data2 = 0x23;
Byte byte( data );
data2 = byte; // data2 is overwritten from 0x23 to 0x41

return 0;
}

Francesco S. Carta 06-10-2010 01:45 AM

Re: Operator Cast () Reference?
 
Immortal Nephi <Immortal_Ne...@hotmail.com> wrote:
> * * * * Someone posted his Byte class code on the previous thread.. *I have a
> question about operator cast (). *Please explain the difference
> between local object and reference object. *Why do you need reference
> object?
> * * * * The object is still the same if you remove ampersand between operator
> unsigned char and ().
>
> class Byte {
> public:
> * * * * Byte( unsigned char& c ) : c_( c ) {
> * * * * }
>
> * * * * Byte& operator=( unsigned char val ) {
> * * * * * * * * c_ = val;
> * * * * * * * * return *this;
> * * * * }
>
> * * * * operator unsigned char&() { // ???? reference ????
> * * * * * * * * return c_;
> * * * * }
>
> // * * *operator unsigned char() { // ???? local ????
> // * * * * * * *return c_;
> // * * *}
>
> private:
> * * * * unsigned char& c_;
>
> };
>
> int main() {
> * * * * unsigned char data = 0x41, data2 = 0x23;
> * * * * Byte byte( data );
> * * * * data2 = byte; // data2 is overwritten from 0x23 to 0x41
>
> * * * * return 0;
>
> }
>
>


Eh, nice question... somebody here would eventually be able to explain
why my code gives such an output:

-------
#include <iostream>

using namespace std;

class IntByRef {
public:
IntByRef(int i) : datum(i) {};
operator int&() {
return datum;
}
private:
int datum;
};

class IntByVal {
public:
IntByVal(int i) : datum(i) {};
operator int() {
return datum;
}
private:
int datum;
};

int main()
{
int one = 1;
int two = 2;

IntByRef intbyref(one);
IntByVal intbyval(two);

cout << intbyref << endl;
cout << intbyval << endl;

intbyref = 42;
intbyval = 42;

cout << intbyref << endl;
cout << intbyval << endl;

return 0;
}
-------

Output:
-------
1
2
42
42
-------

I would have expected the compiler to choke on the assignment to
intbyval, or, at least, to print "2" as last line of output...

(just for the sake of learning something new...)

Twixer Xev 06-10-2010 02:03 AM

Re: Operator Cast () Reference?
 
the reference is a pointer to the var passed to the ctor, so modifying
the object modifies the original var passed through. remove the
reference and you would not change the original values by assigning to
those objects.

Francesco S. Carta 06-10-2010 02:11 AM

Re: Operator Cast () Reference?
 
Twixer Xev <twixer_...@hotmail.com> wrote:
> the reference is a pointer to the var passed to the ctor, so modifying
> the object modifies the original var passed through. remove the
> reference and you would not change the original values by assigning to
> those objects.


That's what puzzles me: the IntByVal returns an int, not a reference
to an int, still, the datum variable gets modified.

Compiler's glitch? GCC 4.4.0 here...

--
FSC
http://userscripts.org/scripts/show/59948

Francesco S. Carta 06-10-2010 02:19 AM

Re: Operator Cast () Reference?
 
"Francesco S. Carta" <entul...@gmail.com> wrote:
> Twixer Xev <twixer_...@hotmail.com> wrote:
> > the reference is a pointer to the var passed to the ctor, so modifying
> > the object modifies the original var passed through. remove the
> > reference and you would not change the original values by assigning to
> > those objects.

>
> That's what puzzles me: the IntByVal returns an int, not a reference
> to an int, still, the datum variable gets modified.
>
> Compiler's glitch? GCC 4.4.0 here...
>


Wait... maybe you were replying to the OP... I'm passing no reference
to my constructors...

Still puzzled.

--
FSC
http://userscripts.org/scripts/show/59948

Twixer Xev 06-10-2010 02:20 AM

Re: Operator Cast () Reference?
 
Ah, I see the error. the ctor for the reference version should take a
reference parameter. look at the OP's posting. the ctor takes a ref and
that is the behavior they were asking about.

Your code, as it is, runs correctly.

Twixer Xev 06-10-2010 02:24 AM

Re: Operator Cast () Reference?
 
I should clarify. Your assignments never invoke the cast operator. Put a
trace in there and you'll see what I mean. Assignment is covered by the
ctor's, which happen to be identicle for each class.

Francesco S. Carta 06-10-2010 02:35 AM

Re: Operator Cast () Reference?
 
Twixer Xev <twixer_...@hotmail.com> wrote:
> I should clarify. Your assignments never invoke the cast operator. Put a
> trace in there and you'll see what I mean. Assignment is covered by the
> ctor's, which happen to be identicle for each class.


Ah, I see what you mean (silly me). In fact, if I declare the
constructors as explicit both assignments halt the compiler. Thanks
for the pointer :-)

--
FSC
http://userscripts.org/scripts/show/59948

Francesco S. Carta 06-10-2010 02:56 AM

Re: Operator Cast () Reference?
 
"Francesco S. Carta" <entul...@gmail.com> wrote:
> Twixer Xev <twixer_...@hotmail.com> wrote:
> > I should clarify. Your assignments never invoke the cast operator. Put a
> > trace in there and you'll see what I mean. Assignment is covered by the
> > ctor's, which happen to be identicle for each class.

>
> Ah, I see what you mean (silly me). In fact, if I declare the
> constructors as explicit both assignments halt the compiler. Thanks
> for the pointer :-)
>


In addition, if I got it straight:

-------
#include <iostream>

using namespace std;

class IntByRef {
public:
explicit IntByRef(int i) : datum(i) {};
operator int&() {
return datum;
}
private:
int datum;
};

class IntByVal {
public:
explicit IntByVal(int i) : datum(i) {};
operator int() {
return datum;
}
private:
int datum;
};

int main()
{
int one = 1;
int two = 2;

IntByRef intbyref(one);

IntByVal intbyval(two);

cout << intbyref << endl; // prints 1

int& rint = intbyref;
rint = 42;
cout << intbyref << endl; // prints 42

// int& rint2 = intbyval; // chokes the compiler (*)

const int& crint = intbyval; // fine (**)
const int& crint2 = 78; // fine (**)

intbyval = IntByVal(10000);

cout << crint << endl; // prints 2
cout << crint2 << endl; // prints 78
cout << intbyval << endl; // prints 10000

return 0;
}
-------

(*) The compiler complains because it is not possible to create a non-
const reference to something that isn't modifiable.

(**) Fine? Seems so. Compiles and gives the expected result ;-)

--
FSC
http://userscripts.org/scripts/show/59948

Twixer Xev 06-10-2010 03:13 AM

Re: Operator Cast () Reference?
 
> (*) The compiler complains because it is not possible to create a non-
> const reference to something that isn't modifiable.
>
> (**) Fine? Seems so. Compiles and gives the expected result ;-)


Yes, looks like you got it.


All times are GMT. The time now is 09:54 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.