Solved

const question

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

746 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

12 Experts available now in Live!

Get 1:1 Help Now