Quick const-by-ref question

I get compiler warnings such as:
" warning: passing `const genericRecord' as `this' argument of `virtual bool genericRecord::equals(const genericRecord&)' discards qualifiers"

I want the object I am passing to NOT be modified, but it looks like I am using it incorrectly, what do I need to change?
bool ethInterfaceRecord::equals(const genericRecord &rec)
{
// Does a logical comparison of two records soley based on the ETH_NUM.
string recEth;
recEth = rec.getConfValue(ETH_NUM);
string thisEth = this->getConfValue(ETH_NUM);

if (recEth == thisEth)
{
return true;
}
return false;
}
...
bool configServerData::isNewRecord(const genericRecord *rec,
const vector<genericRecord*> &vec,
unsigned int &idx)
{
for (unsigned int i = 0; i < vec.size(); i++)
{
if (rec->equals(*vec.at(i)) == true) // COMPLAINS here
{
idx = i;
return false;
}
}
return true;
}
[1083 byte] By [mcmancsu] at [2007-11-20 11:27:23]
# 1 Re: Quick const-by-ref question
bool ethInterfaceRecord::equals(const genericRecord &rec) const
{
// Does a logical comparison of two records soley based on the ETH_NUM.

// change the body of the function to this simple line:

return getConfValue( ETH_NUM ) == rec.getConfValue( ETH_NUM );
}
...
bool configServerData::isNewRecord
(
const genericRecord &rec, // reference not pointer
const vector<genericRecord*> &vec,
unsigned int &idx
) const
{
// find_if would be ideal but a loop should be done like this:

size_t vecSize = vec.size();

for (unsigned int i = 0; i < vecSize; ++i)
{
if (rec.equals( vec[i] ) )
{
idx = i;
return false;
}
}
return true;
}
NMTop40 at 2007-11-9 1:25:40 >
# 2 Re: Quick const-by-ref question
const does seem to confuse people.

While it's not the direct focus of your question, this may be of help.

First, about const and pointers. 'const' can be applied before and after the * in a pointer declaration, and the meaning is different depending on placement.

const someclass *p;

This means the pointer itself is constant, but not the object it points to.

someclass * const p;

this means the pointer isn't constant, but the object it points to is.

const someclass * const p;

This means both the pointer and the object it points to are constant.

This applies to your parameter in isNewRecord. You've declared the pointer is const, but not the object to which it points. Is that what you intend?

Now, equals is a function that I doubt modifies the object upon which it's called (rec in the isNewRecord function). If that's true, then guarantee that it will be true, and therefore can be called even if the OBJECT is constant by making the function itself a const function (placing the const keyword after the function close paren).

Only const functions can be called for a const object, so if you have a const object, and equals SHOULD be available for such a const object, make the function const (applicable to all functions as a decision one way or another).

Make this a policy in your development. Once you start using const, go all the way, no partway. You'll realize why if you've developed without it, then try to impose it later. You'll end up in a maze of satisfying complaints that relate to calls of non const functions on const objects that really don't modify the object, and therefore COULD be const functions.

As an example, you'll discover, that if you make equals const, then the call to function getConfValue will issue an error unless you make IT const, too.

Work along those lines and see if you don't end up with a solution.
JVene at 2007-11-9 1:26:40 >
# 3 Re: Quick const-by-ref question
const someclass *p;

This means the pointer itself is constant, but not the object it points to.

someclass * const p;

this means the pointer isn't constant, but the object it points to is.Other way around ;)
treuss at 2007-11-9 1:27:44 >
# 4 Re: Quick const-by-ref question
:D Ah, you're right!
JVene at 2007-11-9 1:28:39 >
# 5 Re: Quick const-by-ref question
This is probably THE best link to explain const correctness.
C++ faq lite on const correctness ( http://plg.uwaterloo.ca/%7Ergesteve/c++-faq/const-correctness.html)
angelorohit at 2007-11-9 1:29:40 >
# 6 Re: Quick const-by-ref question
This is probably THE best link to explain const correctness.
C++ faq lite on const correctness ( http://plg.uwaterloo.ca/%7Ergesteve/c++-faq/const-correctness.html)How about this one : const correctness ( http://www.gotw.ca/gotw/006.htm)? ;)
exterminator at 2007-11-9 1:30:39 >
# 7 Re: Quick const-by-ref question
Yeah, that too. :D
angelorohit at 2007-11-9 1:31:43 >
# 8 Re: Quick const-by-ref question
Awesome! Got it now. I had to add const to the end of some function definitions as well as use a const_iterator vs a plain iterator. :)
mcmancsu at 2007-11-9 1:32:44 >