Вы находитесь на странице: 1из 3

Const in C++ .

Function declarations can put const in a number of places, as can be seen from the following function that accepts and returns an integer: int functionName(int x)const; //(1) int functionName(const int x); //(2) const int functionName(int x); //(3) Number (1) - When declaring a member function, if it is declared in this form then that function cannot change any of the member variables belonging to that class. It is basically like all of the member variables become const for the duration of the function. So the following will not compile: class SomeClass{ public: void changeValue(int x)const; private: int someValue; }; void SomeClass::changeValue(int x)const { this->someValue = x; //Error - someValue is const for this function } This is very useful for member functions like 'getters' which should only read member variables and not write to them. Number(2): This says that the function cannot change the value x that is passed to the function cannot be changed. This is useful if you are passing in a value and you want to be sure that it will not be changed by mistake. See the following code: void someFunction(const int x) { int y = x; //Fine - const->non-const copying is fine x = x + 1; //Error - cannot assign to a variable that is const } Finally, there is Number(3): The first time I saw this I thought - "This must mean that when you return the const int from functionName(), that the return value cannot be changed". But this is wrong, wrong, wrong.... and when I thought about it it makes sense. What is actually happening when you return a variable from a method by value (as we are doing here)? Well, we are essentially copying the value from within the function to the value outside it. And copying a const value to a not const value is fine. const int someFunction() { const int x = 5; return(x); } int main(int argc, char* argv[]) { int y = someFunction(); y++; //fine.

class C { int i; public: int Get() const // Note the "const" tag { return i; } void Set(int j) // Note the lack of "const" { i = j; } }; void Foo(C& nonConstC, const C& constC) { int y = nonConstC.Get(); int x = constC.Get(); nonConstC.Set(10); constC.Set(10); } Why does the following program generate an error

Note
1)Pointer to constant points to a constant. We may declare pointer ptr as const int *ptr = & j ; In this declaration, const gets attached to int (not to pointer). Hence, the protection is applied to variable j as referred by *ptr. The compiler does not allow us to change *ptr. Note the following: Compiler allows us to change the ptr. Compiler allows us to change j. (but not *ptr which also means j only)

2)

const Class object

It is possible to declare an object as constant. Assume we have class Point suitably defined. Now we are allowed to declare const Point pt1; It declares a constant object named pt1 of type Point. You will ask me what the use of this declaration is. Well, now compiler will not allow us to modify this object. It means you cannot modify even its public data members! Well we may say that we have ac hieved read only objects.

1.

True/False: The data members of the class have to be only variables. They cannot be declared as constants.
Answer: False

2. 3.

True/False: Pre-processor directives are terminated with semicolon.


Answer: False

True/False: If a method of a class is declared as const, attempting to modify a data member by this method results in compile time error.
Answer: True

4.

True/False: If we declare a constant pointer as


int const * ptr = &j ; the pointer cannot be reassigned.

Answer: True

5.

True/False: We cannot invoke a function with different number of arguments at different times except function with default arguments.
Answer: False

6.
a. b. c. d.

A function with variable number of arguments


has only one prototype declaration should have as many as prototype declarations to be used to actual functions used with different parameters does not require prototype of the function none of the above

Answer: (a)

Вам также может понравиться