Click here to Skip to main content
16,008,010 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Any good references? Pin
Lhenno Ferrari31-Jan-03 1:01
Lhenno Ferrari31-Jan-03 1:01 
GeneralToolbar Pin
satyavasu30-Jan-03 19:52
satyavasu30-Jan-03 19:52 
GeneralRe: Toolbar Pin
dsuratman30-Jan-03 20:59
dsuratman30-Jan-03 20:59 
Generalsdi app starts minimized Pin
bryce30-Jan-03 19:00
bryce30-Jan-03 19:00 
GeneralRe: sdi app starts minimized Pin
ROK_RShadow30-Jan-03 19:17
ROK_RShadow30-Jan-03 19:17 
GeneralRe: sdi app starts minimized Pin
sulaxan31-Jan-03 20:07
sulaxan31-Jan-03 20:07 
GeneralRe: sdi app starts minimized Pin
bryce1-Feb-03 13:28
bryce1-Feb-03 13:28 
Generalconst help Pin
ROK_RShadow30-Jan-03 18:03
ROK_RShadow30-Jan-03 18:03 
Hello. This is a general C++ question. Can somebody please shed some light on the const modifier? I have been programming for about a year.. and I never use const because I really don't understand it... so I am trying to gain some ground, and I just can't understand this.

Please look at the class declaration below, and can somebody explain to me why the functions draw(), and getx() allow me to modify the private data members. they are declared const, so they shouldn't allow me to modify those variables.. but they do.

according to MSDN help library, you declare a member function const by placing the keyword after the parameter list.. this is what I did.. but it still lets me modify them.

I know if I declare the variables const it will work.. but shouldn't I be able to declare a function const so the compiler won't let me modify?

class cRectangle
{
public:

// constructors
cRectangle();
cRectangle(int nL, int nW);

// Destructor
~cRectangle();

// public accesors
void SetLength(int nL);
void SetWidth(int nW);
int GetLength() const { return *nLength;}
int GetWidth() const { return *nWidth;}

// public methods
void Draw() const;


private:

// Data Members
int* nLength; // pointers because they will be stored on the heap
int* nWidth;

};


// implementation file
// striped but draw() function
// Drawing functions
void cRectangle::Draw() const
{
// try and change a variable
*nWidth = 42; // why its this allowed????
// function is const? should not let me change
// the object.
}


If anybody could explain this to me. It would be greatly appreciated. Thank you.
GeneralRe: const help Pin
Chris Losinger30-Jan-03 18:11
professionalChris Losinger30-Jan-03 18:11 
GeneralRe: const help Pin
Christian Graus30-Jan-03 18:13
protectorChristian Graus30-Jan-03 18:13 
GeneralRe: const help Pin
Chris Losinger30-Jan-03 18:17
professionalChris Losinger30-Jan-03 18:17 
GeneralRe: const help Pin
Christian Graus30-Jan-03 18:22
protectorChristian Graus30-Jan-03 18:22 
GeneralRe: const help Pin
Christian Graus30-Jan-03 18:11
protectorChristian Graus30-Jan-03 18:11 
GeneralRe: const help Pin
ROK_RShadow30-Jan-03 19:15
ROK_RShadow30-Jan-03 19:15 
GeneralRe: const help Pin
Christian Graus30-Jan-03 19:29
protectorChristian Graus30-Jan-03 19:29 
GeneralRe: const help Pin
ROK_RShadow30-Jan-03 19:36
ROK_RShadow30-Jan-03 19:36 
GeneralRe: const help Pin
Christian Graus30-Jan-03 19:55
protectorChristian Graus30-Jan-03 19:55 
GeneralRe: const help Pin
ROK_RShadow30-Jan-03 20:01
ROK_RShadow30-Jan-03 20:01 
GeneralRe: const help Pin
Taka Muraoka30-Jan-03 18:13
Taka Muraoka30-Jan-03 18:13 
GeneralRe: const help Pin
Mike Nordell30-Jan-03 20:41
Mike Nordell30-Jan-03 20:41 
GeneralFindWindow Pin
aguest30-Jan-03 14:56
aguest30-Jan-03 14:56 
GeneralRe: FindWindow Pin
Nick Parker30-Jan-03 16:32
protectorNick Parker30-Jan-03 16:32 
Generalneed encrypt and decrypt a fix length small block of data. Pin
Anonymous30-Jan-03 12:53
Anonymous30-Jan-03 12:53 
GeneralRe: need encrypt and decrypt a fix length small block of data. Pin
Chris Losinger30-Jan-03 13:19
professionalChris Losinger30-Jan-03 13:19 
GeneralRe: need encrypt and decrypt a fix length small block of data. Pin
karl_w30-Jan-03 21:00
karl_w30-Jan-03 21:00 

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.