Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

const question

Posted on 1998-12-08
2
220 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Getting IP address 8 87
Move constructor only called if marked noexcept? 6 108
Unresolved External Symbols 3 100
Using Diagnostic tools in VS2015: Unresoved allocations 19 102
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

792 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