Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

muthuveerappan

macrumors newbie
Original poster
Aug 17, 2008
15
0
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
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
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
Code:
double BankAccount::getAccountBalance()
{
    return m_balance;
}

and all you have to do is change this to

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

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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.

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

b e n
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.