Solved

const question

Posted on 1998-12-08
2
221 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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 100
Header of docx file 17 129
Why isn't object file created? 6 86
Finding Divisors 5 35
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

740 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