C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Passing a const object by reference.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Sat Jan 28, 2006 7:15 pm    Post subject: Passing a const object by reference. Reply with quote



Hey all.

I just changed from this:

mat4& operator = (mat4 & mymat4);

to this:

mat4& operator = (mat4 const& mymat4);

all over my different classes, as some compilers compiled without the const, but I learned that it is necessary.

Now, I get this error:

binary '[' : no operator found which takes a left-hand operand of type 'const mat4' (or there is no acceptable conversion)

Here is the function implementation:

mat4& mat4::operator = (mat4 const& mymat4)
{
m[0] = mymat4[0]; m[4] = mymat4[4]; m[8] = mymat4[8]; m[12] = mymat4[12];
m[1] = mymat4[1]; m[5] = mymat4[5]; m[9] = mymat4[9]; m[13] = mymat4[13];
m[2] = mymat4[2]; m[6] = mymat4[6]; m[10] = mymat4[10]; m[14] = mymat4[14];
m[3] = mymat4[3]; m[7] = mymat4[7]; m[11] = mymat4[11]; m[15] = mymat4[15];
return *this;
}

And here is the definition of the [] operator:

float& mat4::operator [] (int subscript)
{
return m[subscript];
}

Anyone understand this error?


Thanks!
Back to top
Pete C
Guest





PostPosted: Sat Jan 28, 2006 8:00 pm    Post subject: Re: Passing a const object by reference. Reply with quote



<dontspam (AT) _dylan_ (DOT) gov> wrote:
Quote:
And here is the definition of the [] operator:
float& mat4::operator [] (int subscript)
{
return m[subscript];
}

You need to define another operator[], but const - like this:

const float& mat4::operator [] (int subscript) const
{
return m[subscript];
}

This version of the function will be called by the (const)
right-hand-side of all your assignments.
This may or may not compile - I don't know, because you haven't told us
the type of the 'm' member variable. But give it a try.
Back to top
Guest






PostPosted: Tue Jan 31, 2006 3:37 am    Post subject: Re: Passing a const object by reference. Reply with quote



This actually works fine as well, since I don't need a reference for this copy of the function -- just a copy of the float value (m was an array of floats):

float operator [] (int subscript) const;

So, to be sure, for this one:

const float& mat4::operator [] (int subscript) const

The reason that you need a const at the end of this is b/c when you pass in a reference to a const object, all functions that that object calls should also be const functions?
And the reason for the const at the beginning is b/c a reference is being returned, and you could otherwise alter the object contents.


Thanks for the help!!


<dontspam (AT) _dylan_ (DOT) gov> wrote:
Quote:
And here is the definition of the [] operator:
float& mat4::operator [] (int subscript)
{
return m[subscript];
}

You need to define another operator[], but const - like this:

const float& mat4::operator [] (int subscript) const
{
return m[subscript];
}

This version of the function will be called by the (const)
right-hand-side of all your assignments.

This may or may not compile - I don't know, because you haven't told us
the type of the 'm' member variable. But give it a try.
Back to top
Bob Hairgrove
Guest





PostPosted: Tue Jan 31, 2006 10:00 am    Post subject: Re: Passing a const object by reference. Reply with quote

On Tue, 31 Jan 2006 03:37:51 +0000 (UTC), <dontspam (AT) _dylan_ (DOT) gov>
wrote:

Quote:
float operator [] (int subscript) const;
const float& mat4::operator [] (int subscript) const;

[snip]

To be consistent with the normal behavior of operator[], the index
should probably be unsigned, e.g.:

float operator [] (size_t subscript) const;
const float& mat4::operator [] (size_t subscript) const;

--
Bob Hairgrove
NoSpamPlease (AT) Home (DOT) com
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.