C++ Passing Class as a const parameter

Discussion in 'Mac Programming' started by muthuveerappan, Feb 17, 2009.

  1. macrumors newbie

    Joined:
    Aug 17, 2008
    #1
    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
     
  2. macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #2
  3. macrumors 6502

    Joined:
    Apr 24, 2008
    #3
    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;
    }
    
     
  4. thread starter macrumors newbie

    Joined:
    Aug 17, 2008
    #4
    Thank you so much!!!

    Thank you so much!!!

    works great and thanks for the explanation
     
  5. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #5
    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
     
  6. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    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.
     

Share This Page