C++ Programming - OOPS Concepts - Discussion
Discussion Forum : OOPS Concepts - General Questions (Q.No. 31)
31.
Which of the following is the correct way of declaring a function as constant?
Discussion:
13 comments Page 1 of 2.
Dimpi said:
6 months ago
D option is the right one.
Because both A and B options correctly define the function, it returns a const int.
Because both A and B options correctly define the function, it returns a const int.
Amit Shahi said:
8 months ago
Here the question is how to declare the function as a constant that's why the C option is the right answer.
If the question is asking how to declare a function return as a constant then the D option is the right answer.
If the question is asking how to declare a function return as a constant then the D option is the right answer.
Vls said:
2 years ago
Here, It asks about a function, not a member function or method. Constness of a standalone function doesn't make sense.
Sai Aditya said:
4 years ago
I think option D is the right one.
Waqar Akram said:
5 years ago
Option C is Correct.
Example of Deepak Kumar is 100% correct.
Example of Deepak Kumar is 100% correct.
Chuck N said:
7 years ago
Exampe C is correct.
Examples A and B compile,but it seems the const keyword is just ignored, the function does NOT actually work as const. Below is a different example I found elsewhere. Since non-const functions can only be called by non-const objects, the below example will only compile if the function getValue is declared as in the example C.
If declared as in examples A and B, you will get a compiler error. The compiler error will go away if " Test t" in Main is not declared as a const, BUT the compiler is just ignoring the const keyword and the function is NOT const in these cases.
#include <iostream>
using namespace std;
class Test {
int value;
public:
Test(int v = 0) {value = v;}
int getValue() const {return value;}
};
int main() {
const Test t;
cout << t.getValue();
return 0;
}
Examples A and B compile,but it seems the const keyword is just ignored, the function does NOT actually work as const. Below is a different example I found elsewhere. Since non-const functions can only be called by non-const objects, the below example will only compile if the function getValue is declared as in the example C.
If declared as in examples A and B, you will get a compiler error. The compiler error will go away if " Test t" in Main is not declared as a const, BUT the compiler is just ignoring the const keyword and the function is NOT const in these cases.
#include <iostream>
using namespace std;
class Test {
int value;
public:
Test(int v = 0) {value = v;}
int getValue() const {return value;}
};
int main() {
const Test t;
cout << t.getValue();
return 0;
}
(1)
Sudeshna cnaudhuri said:
8 years ago
C is the wrong option. I checked it by running a program in Dev c++.
Both A and B works. D is the correct one.
Both A and B works. D is the correct one.
Govind kawde said:
8 years ago
Yes, I too think option D is Correct.
Navnath Dombale said:
1 decade ago
During object creation is there need to write const keyword like const date bdate();
Nazmul said:
1 decade ago
Is this ok that a const object can refer to/call only a const function?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers