Skip to main content
GameDev.net gamedev.net
🔒 Locked

const question; // please respond.

Started by xstreme2000 Sep 27, 2000 at 3:57 AM 2 replies 200+ views
Original Post
xstreme2000
xstreme2000
Before I thought I knew what a const was but then I was reading Sam's Teach Yourself DirectX and I saw example with a use of const which I'd never seen before (the function was a member of a class if that makes any difference): BOOL SomeFunction() const; err.. What does this do? also why whould you want a const in BOOL SomeFunction(const char* szFileName); if anyone can take a moment to answer these questions I would be very greatful!!! Edited by - xstreme2000 on 9/27/00 4:02:22 AM
Cel
Cel
Hi, using const like that is part of this sort of thing:

''Const-correctness is an important aid in writing code. In effect it is an assertion that the compiler should check that an object will not have its values modified. It can also allow the compiler to apply some special o ptimizations, such as placing a const object in read-only memory.'' (Object Orients Programming using C++, Ira Pohl)

Basically (and someone correct me if I''m wrong) its just saying ''this function should not write to any variables, but it can read variables''. ie a read only function etc

Cel
Cel aka Razehttp://chopper2k.qgl.org
BitMaster
BitMaster
You can only declare const member functions (there are no const global functions, read on to know why). Let''s make a short example, perhaps that makes it all easier to understand:
    
class Example
{
public:
// ... constructor & stuff

void SetData(DWORD data) { dwData = data; }
DWORD GetData() const { return dwData; }
protected:
DWORD dwData;
};

/*
Some global functions
*/

void func1(Example & example)
{
example.SetData(15); // ok

cout << example.GetData(); // ok

}

void func2(const Example & example)
{
example.SetData(15); // NOT ok, example is const, so you can only call const member functions
cout << example.GetData(); // ok, GetData is const
}


Got it? And about the (const char * szFileName) pointer: This tells you (and the compiler) that you can read from the string pointed to, but not write to it. strcpy(szFileName, "dadidadum"); wouldn''t work. The compiler expects an char * as first argument and wouldn''t compile the code. You could use const_cast to remove the const but that might crash something because usually a const has a reason.
Stoffel
Stoffel
Or said another way:
A const member function is a function that has the const keyword after the parameter list. In this function, you cannot change the object (i.e. the contents of *this). Specifically, member variables of the object may only appear as rvalues (on the right-hand side of assignments), and you can only call other const functions of that class. If you break these rules, your program will not compile. In other words, making the function const forces you to abide by these rules.

(Side note: You can have const and non-const versions of the same function. C++ treats them as overloads of the same function name, just as if you had supplied a different parameter list. A common mistake is to declare a function const in your header file, but to accidently leave off the const in your implementation.)

Const parameters assure that the function will not change that value in its body. This applies to member and global functions. Note that it''s perfectly legal to write a function like this:
double square_root (const double num)
...even though it''s pretty useless. What this means is that in the function square_root, you cannot change the value of num. However, since num was passed by value, you''re just dealing with a copy of the value so you should be free to change it if you want to.

Const parameters are more often seen when pointers or references are passed to functions. For instance:
void set_name (const char* name)
...assures the client who calls set_name that the function will not change the string pointed to by name. Even though functions can do this, the const means it won''t.

There are ways to work around const-ness, and sometimes they''re necessary, but it should not be done in a way that violates the trust of the client.

I never used const in C (even though it was there for parameters), but since I learned C++ and started using it, I found that it made my life a lot easier, and my code got easier to read. const parameters are also a great way (some would say the only way, without reading the implementation and good comments) to tell which pointer or reference arguments to a function are "inputs" and which are "outputs". If it''s const, it''s gotta be an input. If it''s non-const, the function''s free to change it so it''s probably an output.

BTW, for member functions, you can create variables with the keyword "mutable" in your class. This allows you to change those variables (i.e. they can appear as lvalues) even in const member functions. This is useful if you have a const operation on a class, but the class collects some data of its own (i.e. a count of how many times method M is called) that it would like to increment. If you declare the counter mutable, it''s safe to change it in const functions (i.e. it will compile).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.