Hello! Can anyone explain me why if I make the function get_fName a const function, it returns _fName only with the casting (char*)? Without casting, it not compiles.
On the other hand, if I remove the const, it returns _fName also without casting?
it's because you return a non-const pointer when declaring the method const.
someone could use get_fName, he gets a char*, and then alters it's content.
This means that your method get_fName is not really const because it allows the user to modify it's content outside of the class.
you could return a const char* if you don't want the name to be altered outside the class or you could not declare the method itself const so others are allowed to modify the name.
I think you're wrong, const function means that it can't change any of the member variables of this class. It shouldn't care what will be afterwards.
Besides, why should the casting solve this problem??
const function means that it can't change any of the member variables of this class. It shouldn't care what will be afterwards.
const functions should prefer any possible change to class through them. That includes returning mutable pointers/references.
why should the casting solve this problem??
Because you are using unsafe c-cast here. It is like saying: To hell with language rules, const correctness and stuff. Do as I said. I do not care about possible problems.
Try this with basic c++ cast: returnstatic_cast<char*>(_fName);
because you have a pointer member and returning it by value. There is no way you can change value of pointer itself.
data this pointer is pointing to is another thing. const qualifier are shallow, they protect only things which are inside class itself. You still can change values you are pointing to, just not pointer itself.
const function only prevents from changing inside it
And returning non-const references and pointers to its members.
The const-qualifier modifies the this-object to be const.
Your _fName is an array and therefore holds data inside the this-object and therefore is now const.
Your int* is a pointer, you can't modify that pointer with the const-qualifier.
With the const qualifier you aren't able to modify the pointer itself. returning a pointer however returns a copy of the value of the pointer so basically you don't modify the pointer itself so it's okey.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//Pointer example
#include <iostream>
void func(int* data)
{
data = 0;
std::cout << d << std::endl; // 0
}
int main()
{
int* d = newint(5);
func(d);
std::cout << d << std::endl; // NOT 0
}
and the reason that the outputs are different is because you changing d inside the function, actually you change data, not d. d remains the same all the time
What exactly is unappropriate? You are returning pointer by value from your method. A copy of pointer which cannot be used to affect your class (directly).
More proper example would be declare const pointer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
void func(int* data)
{
data[0] = 1;
data = 0;
std::cout << data << std::endl; // 0
}
int main()
{
int* const d = newint(5);
func(d); //Passing a copy here, d cannot be affected.
std::cout << d << std::endl; // NOT 0
}