const function ??

hi, I'm having dificulties understanding this function:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

const int const f() {
      cout << "hello" << endl;
      return 0;
}
int main() {
	const int a = f();
	return 0;
}


in this example No.2 (same as above) the same is achived (one "const is missing")

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

const int f() {
      cout << "hello" << endl;
      return 0;
}
int main() {
	const int a = f();
	return 0;
}


is that constant function or what??
I cant find in my book answer to this lol.

EDIT:

LOL LOOK AT THIS CODE:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
const const const const int const const const const const f() {
     cout << "hello" << endl; 
     return 0;
}
int main() {
	const int a = f();
	system("pause");
	return 0;
}


it compiles just fine ??
except warnings saying more same qualifers used"
Last edited on
const is just an added part to make sure you can't change the memory location of the variable or the output of a function.

 
const int x = 5;


The memory that x is stored at is not allowed to be accessed by x, x is not off limits to it. It can read it but can not write to it. Can you change it still, yes, but not using x. You wouldhave to use pointers to do this. But no matter how many times you put in const, it won'r change anything. Only 1 is needed but you can put in as many as you like.
tnx for reply,
I was just confused why compiler allows passing 10 const's lol
so I thought that I'm missing a chaper aoubt const functions ^^D
Nah, it just tells the compiler to put a lock on the space or the output should be in a space that is locked. It was a very good question to ask because most people don't fully understand const even though it is very import. Like when you declare

 
char Buffer[BUFFER_MAX];


Because all array lengths MUST BE CONSTANTS when declared, you need to use const to declare it but you can get around a fixed size if you use functions to create a constant variable. C/C++ always has tricks to get around everything, just have to play around to find them.
Last edited on
OP, if you are having trouble with a lot of key words and pointers in a declaration of anything, try reading it from right to left.

for example:

 
const char * const pSomething = NULL;


pSomething is a (const pointer) to a (char that is const)
@ceruleus
thank you, I have been reading about that in my book :)

tnx William for clarification!
Topic archived. No new replies allowed.