Const pass by reference question....

This is a discussion on Const pass by reference question.... within the C++ Programming forums, part of the General Programming Boards category; I was hoping that somebody could tell me why this works the way it does. I have a class that ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Const pass by reference question....

    I was hoping that somebody could tell me why this works the way it does.

    I have a class that has the data members as private and in order to access them I have public functions that return this data, as such:

    Code:
    private:
    
    	string ISBN;
    	string title;
    	string author;
    	string copyright_date;
    	vector<Book> Books;
    
    public:
    
    string display_title()
    	{
    		return title;
    	}
    
    	string display_ISBN()
    	{
    		return ISBN;
    	}
    
    	string display_author()
    	{
    		return author;
    	}
    
    	string display_copydate()
    	{
    		return copyright_date;
    	}
    I have used an operator overload function to compare the ISBN numbers and return a bool. Effectively I want to pass in the two books without modifying them and do the comparison, however the function won't allow me to add const at the beginning of the parameter. This is the function I wrote:

    Code:
    bool operator==(const Book& a, const Book&b)
    {
    	return a.display_ISBN() == b.display_ISBN();
    }
    As you can see it is calling the display_ISBN function, which I think is where the problem lies, but I am not sure why. When I pass by non-const it works as expected.

    The error I get when passing by const reference is

    error C2662: 'Book::display_ISBN' : cannot convert 'this' pointer from 'const Book' to 'Book &'

    I would appreciate some advice as to why this is?

    Thanks.

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    When you define the class declare the methods that don't modify members as const like this
    Code:
    
    	string display_author() const
    	{
    		return author;
    	}
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by siavoshkc View Post
    When you define the class declare the methods that don't modify members as const like this
    Code:
    
    	string display_author() const
    	{
    		return author;
    	}
    That works perfectly, thank you.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,414
    The reason behind this is that when the compiler looked at DisplayISBN(), it sees that the function does not guarantee that it will not modify the instance, so it complains that you can't call it on a const object (this would violate the const attribute, after all).
    Therefore, you must explicitly tell the compiler which functions does not modify the instance. Do this by adding "const" after the parameter list of the function. All getters should be const.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Elysia View Post
    The reason behind this is that when the compiler looked at DisplayISBN(), it sees that the function does not guarantee that it will not modify the instance, so it complains that you can't call it on a const object (this would violate the const attribute, after all).
    Therefore, you must explicitly tell the compiler which functions does not modify the instance. Do this by adding "const" after the parameter list of the function. All getters should be const.
    Thanks for that. I understand now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stdio.h?
    By kiros88 in forum C Programming
    Replies: 5
    Last Post: 05-21-2010, 07:09 PM
  2. Undefined Reference to functions, that CAN be found
    By Carola in forum C++ Programming
    Replies: 8
    Last Post: 08-27-2009, 05:59 AM
  3. template overloading question
    By plutino in forum C++ Programming
    Replies: 14
    Last Post: 02-27-2009, 01:10 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Problem with Template Function and overloaded equality operator
    By silk.odyssey in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2004, 04:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21