1

Sorry for my ignorance, but my execuse is that I am a begginer with C++.
I find const using const and static keywords very confusing.
for example:

What is the difference between the following uses:

Class Food {

int const apples = 24;
int const oranges = 12; 
const void set_name(string food_name);
void display_name() const;

const char* fp1;
char const* fp2;
char* const fp3;
const char* const food_pointer;
};

I understand that when using const with variables means that I cannot modify the variables later in the program, but I am confused with other uses.
Any brief explainantion is greatly appreciated.

nobody
  • 19,814
  • 17
  • 56
  • 77
Pokemonz
  • 37
  • 4
  • Oops!!! I'm sorry this is duplicate. Thank you for your comment @AndyG – Pokemonz Nov 04 '14 at 21:29
  • @KarolyHorvath: Curious, please explain. – AndyG Nov 04 '14 at 21:38
  • 1
    apples and oranges can't be changed. For set_name the use of const is nonsense because the return type is void. That const would normally prevent you from modifying the value returned. display_name cannot change any members of an instance of Food. fp1 and fp2 both mean the same thing... that the thing they point to cannot be modified. fp3 won't compile because it must be assigned when declared because the pointer cannot be set to point to a new address. food_pointer cannot be reassigned and the thing it points to cannot be modified (also won't compile) – cppguy Nov 04 '14 at 22:04

0 Answers0