Solved

const question

Posted on 1998-12-08
2
217 Views
Last Modified: 2010-04-02
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
Comment
Question by:strmtrpr
2 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 20 total points
ID: 1179634
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
 

Author Comment

by:strmtrpr
ID: 1179635
thanks for the help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now