const and pointers and type declerations

Ok so I'm learning so much every day and trying to remember it all is difficult, I'm sure I've seen some info about this somewhere but I cant find any definitive explanations about the following query;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// What is the difference of where all these const/pointers are??

typedef int(*tfunc)(int);
typedef const int(*tfunc)(int);
typedef int(* const tfunc)(int);
typedef int(*tfunc)(const int);
typedef int(tfunc)(int) const;
typedef int(const tfunc)(int);
typedef const int(const tfunc)(const int);

class A {};
const class A {};
class A {} const;
class A {
	const int i;
	int const i;
	const int const i;

	int func1(int i);
	const int func2(int i);
	tfunc * const func3(int i);
	tfunc const * func4(int i);
	const tfunc const * const func4(const int i) const; // ????? mind blown
};


If anyone ahs the time I would love each line to be commented with a specific explanation of what each pointer/const combination would mean.

I could guess a few of them, and know that const at end of func decl means that method doesn't change the state of its own instance or change any members?

I guess the const at the start (before retn type decl) would mean the returned value is immutable?

Anyway I would just love to have a definitive "cheat sheet" so to speak, so I can easily revise and hopefully stick to my flaky memory :).

Thanks!!
Last edited on
Ok so I found some material that was useful, here is what i got from it so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// What is the difference of where all these const/pointers are??

typedef int(*tfunc)(int);					// 1. Says tFunc-type value must be a pointer
typedef const int(*tfunc)(int);				// 2. Says return var is constant
typedef int(*const tfunc)(int);				// 3. Says the pointer (pointing to tFunc-type obj) is const
typedef int(*tfunc)(const int);				// 4. Says the parameter is const
typedef int(tfunc)(int) const;				// 5. for methods? Says this method cannot modify the state of the object (also tFunc is value-type now? Does this actually make a difference?)
typedef int(const tfunc)(int);				// 6. tfunc itself is const (function cant be overidden?) tFunc-type value is not pointer?
typedef const int(const tfunc)(const int);	// 7. return is const, tfunc value is const, parameter is const
typedef const int const (const * const tfunc const)(const int const) const;	// 8. Some consts are redundant, can be written as 9:
typedef const int(const * tfunc const)(const int) const; // 9. return/param/pointer and function is const, same as:
typedef int const (*const const tfunc)(int const) const; // 10. same as 9

class A {};				// 11. normal class
const class A {};		// 12. const class, class cannot be modified? Could do with a better exaplanation of this one.
class A {} const;		// 13. same as 12 maybe? hmmmmm
class A {
	const int i;		// 14. i is const
	int const i;		// 15. same as 14?
	const int const i;	// 16. same again?

	int func1(int i);	// 17. standard method
	const int func2(int i);	// 18. return value is const
	tfunc * const func3(int i);	// 19. return value (pointer-type) is const
	tfunc const * func4(int i);	// 20. return value is const (pointer can be changed?)
	const tfunc const * const func4(const int i) const; // 21. so this is basically below:
	const tfunc const * func4(const int i) const; // 22. const return value, const pointer, const param and cannot change object state?
};


Is this right? Anyone have any more insight or more definite definitions of these use cases?

thanks again.
Last edited on
Topic archived. No new replies allowed.