Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

const question

I have a class that has a method:
class map
{
friend ostream& operator<<(ostream&, const map&);
public:
int want_map()const;  // what is the purpose of this const
private:

};
the method is declared in the class
defined outside the the class:
int loan::want_bigmap()const
{                    

}
in this declaration I need to use an overloaded inseration
operator to display a map.
the friend function above should work right??
how?

thanks for any help.

 
0
strmtrpr
Asked:
strmtrpr
1 Solution
 
Answers2000Commented:
1. The const means that want_map won't change the state of any members variables of the class.  Actually this is not strictly true, const members can change members marked mutable (VC5 or later req'd to use mutable - but this is part of the standard)

e.g.
class X
{
private:
int a ;
mutable int b ;
public:
void some_func() const ;
} ;

This would mean that some_func could change b, but not a.

The reason for mutable is you might have some totally internal mechanism to the class which does at all effect external behaviour, which needs scratch variables (typical examples are caches, reference counting etc)


2. The friend looks okay to me.  Because the const in the friend declaration, the overloaded << can only call const members (like example 1) of the class.
0
 
strmtrprAuthor Commented:
thanks for the help
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now