Click here to Skip to main content
15,894,646 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to set display resolution Pin
vibindia2-Mar-07 4:18
vibindia2-Mar-07 4:18 
Questionconst keyword Pin
Syamlal S Nair2-Mar-07 0:54
Syamlal S Nair2-Mar-07 0:54 
AnswerRe: const keyword Pin
Nibu babu thomas2-Mar-07 0:57
Nibu babu thomas2-Mar-07 0:57 
GeneralRe: const keyword Pin
Syamlal S Nair2-Mar-07 1:01
Syamlal S Nair2-Mar-07 1:01 
GeneralRe: const keyword Pin
Dominik Reichl2-Mar-07 1:09
Dominik Reichl2-Mar-07 1:09 
GeneralRe: const keyword Pin
Stephen Hewitt4-Mar-07 13:12
Stephen Hewitt4-Mar-07 13:12 
AnswerRe: const keyword Pin
prasad_som2-Mar-07 0:58
prasad_som2-Mar-07 0:58 
AnswerRe: const keyword Pin
Roger Stoltz2-Mar-07 1:55
Roger Stoltz2-Mar-07 1:55 
In addition to what Prasad and Nibu babu thomas already said...

Consider the const keyword as a promise to the compiler that you won't change the value of the variable. If you don't honour this promise the compiler will remind you of it. This applies in al situations for the use of the const keyword.

SyamlalS wrote:
CString GetItemText(int nRow, int nCol) const;


If you try to alter member values inside a member function declared as const, you'll get a compiler error.

So what is the benefit of the const keyword in this situation?
The answer is that you can call such functions on a const declared object.
Explanation in code:
class CMyClass
{
public:
   int GetValue() { return m_nValue; }
   int GetValueConst() const { return m_nValue; }
};

// Try to use the class declared above...
void UseClass( const CMyClass theClass ) // You promise not to change the object
{
    int nTheValue;
    nTheValue = theClass.GetValue();       // Compiler error
    nTheValue = theClass.GetValueConst();  // OK
}


It's considered good practice to declare member functions that don't alter any member variables as const.


"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown

QuestionRe: const keyword Pin
David Crow2-Mar-07 2:51
David Crow2-Mar-07 2:51 
AnswerRe: const keyword Pin
Roger Stoltz2-Mar-07 3:11
Roger Stoltz2-Mar-07 3:11 
AnswerRe: const keyword Pin
toxcct2-Mar-07 1:55
toxcct2-Mar-07 1:55 
AnswerRe: const keyword Pin
vibindia2-Mar-07 4:07
vibindia2-Mar-07 4:07 
GeneralRe: const keyword Pin
John R. Shaw2-Mar-07 20:25
John R. Shaw2-Mar-07 20:25 
QuestionInfo about CListCtrl ? Pin
sakthimuthu2-Mar-07 0:16
sakthimuthu2-Mar-07 0:16 
AnswerRe: Info about CListCtrl ? Pin
Hamid_RT2-Mar-07 0:20
Hamid_RT2-Mar-07 0:20 
QuestionAny error in my code! [modified] Pin
ShaluMalu2-Mar-07 0:16
ShaluMalu2-Mar-07 0:16 
AnswerRe: Any error in my code! Pin
prasad_som2-Mar-07 1:18
prasad_som2-Mar-07 1:18 
QuestionThread proble. Pin
david bagaturia2-Mar-07 0:14
david bagaturia2-Mar-07 0:14 
AnswerRe: Thread proble. Pin
_anil_2-Mar-07 0:23
_anil_2-Mar-07 0:23 
AnswerRe: Thread proble. Pin
User 5838522-Mar-07 0:36
User 5838522-Mar-07 0:36 
AnswerRe: Thread proble. [modified] Pin
prasad_som2-Mar-07 1:34
prasad_som2-Mar-07 1:34 
GeneralRe: Thread proble. Pin
David Crow2-Mar-07 3:02
David Crow2-Mar-07 3:02 
GeneralRe: Thread proble. Pin
prasad_som2-Mar-07 3:06
prasad_som2-Mar-07 3:06 
QuestionMDIChild not getting WM_CLOSE Pin
_anil_2-Mar-07 0:11
_anil_2-Mar-07 0:11 
AnswerRe: MDIChild not getting WM_CLOSE Pin
prasad_som2-Mar-07 1:28
prasad_som2-Mar-07 1:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.