PDA

View Full Version : C++ Passing Class as a const parameter




muthuveerappan
Feb 18, 2009, 12:37 AM
Hi,

I am a newbie and just starting to learn C++.

I read that call by reference parameter is more efficient than a call by value parameter especially when the parameter passed was a class.

So I tried passing a class as a const call by reference parameter to a function.
When I did so, while compiling I got an error.
This error vanished when I removed the const parameter

Could you help me with this, I have given the details below:

Note - I use the command g++ on my mac to compile.

I have given below the definition of the function:

bool RatingAgency :: biggerBalance(const BankAccount& pBankAccount1, const BankAccount& pBankAccount2)
{
return(pBankAccount1.getAccountBalance() > pBankAccount2.getAccountBalance());
}


Note
- RatingAgency is the name of a class
- BankAccount is the name of a class, and it is being passed a parameter
- biggerBalance is the name of the function which belongs to the class RatingAgency
- getAccountBalance is a public function that belonged to the class BankAccount

Error
------
test.cpp:92: error: passing 'const BankAccount' as 'this' argument of 'double BankAccount::getAccountBalance()' discards qualifiers
test.cpp:92: error: passing 'const BankAccount' as 'this' argument of 'double BankAccount::getAccountBalance()' discards qualifiers

Line number 92 corresponds to the return statement in the function biggerBalance

Let me know if you need more information.

Thanks,
Muthu



autorelease
Feb 18, 2009, 03:32 AM
Your method getAccountBalance() is not declared const. Only const methods can be called on const references to objects.

http://en.wikipedia.org/wiki/Const_correctness

Sander
Feb 18, 2009, 03:55 AM
Executive summary: when a function receives a const reference parameter, it is not allowed to change it (obviously). However, your function calls member functions on these parameters which, as far as the compiler knows, might change the objects. You should declare these member functions "const" as well, by which you promise that they don't change the objects they are called on (in fact, the compiler will make sure this is the case).

You probably have a member function

double BankAccount::getAccountBalance()
{
return m_balance;
}


and all you have to do is change this to


double BankAccount::getAccountBalance() const
{
return m_balance;
}

muthuveerappan
Feb 18, 2009, 05:54 AM
Thank you so much!!!

works great and thanks for the explanation

lazydog
Feb 18, 2009, 10:06 AM
Hi

I'm not sure whether this is relevant, and I hope you don't mind, but looking at your code I was thinking it might be better to implement your biggerBalance() method as a function friend of BankAccount rather than a method of RatingAgency.


class BankAccount
{
...
...
friend bool biggerBalance(const BankAccount& pBankAccount1, const BankAccount& pBankAccount2)
{ return pBankAccount1.balance_ > pBankAccount2.balance_ ; }
} ;


b e n

gnasher729
Feb 18, 2009, 11:07 AM
Hi

I'm not sure whether this is relevant, and I hope you don't mind, but looking at your code I was thinking it might be better to implement your biggerBalance() method as a function friend of BankAccount rather than a method of RatingAgency.


You should also try to get into a habit of using method names that indicate clearly and unambiguous what the method does.

I would have expected biggerBalance () to return the balance of the account with more money in it. Others would expect biggerBalance () to return a reference to the object with the bigger balance. Add a few letters and call it "hasBiggerBalanceThan" and everyone will know exactly what it does without having to read the documentation.