const function doesn't return char*

Pages: 12
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?

1
2
3
4
5
6
7
8
9
10
11
  class Student
{
	int _id;
	char _fName [20];
        char* get_fName() const;
}
// implementation
  char* Student::get_fName () const
{
	return (char*)_fName;
}
1
2
3
4
5
6
7
8
9
10
11
  class Student
{
	int _id;
	char _fName [20];
        const char* get_fName() const;
}
// implementation
  const char* Student::get_fName () const
{
	return (char*)_fName;
};
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??
Last edited on
I think you're wrong
No, he is not.
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: return static_cast<char*>(_fName);
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.

Nah, what the const decleration does is it treats the whole object const inside the function so every member is also treated like they are const.

so as long as you are in a const method your variables are treated like they would be declared like this:
1
2
	const int _id;
	const char _fName [20];


and in your method you want to return a non-const char* which discards the const-qualifier of _fName.
Last edited on
Ok, so if you're right why does this work:

1
2
3
4
int Student::get_id () const
{
	return _id;
}


And this also works, if you declare _id as int* in the header:
1
2
3
4
int* Student::get_id () const
{
	return _id;
}


Why no any casting is needed in both these cases?
Last edited on
Ok, so if you're right why does this work:

1
2
3
4
int Student::get_id () const
{
	return _id;
}
because you return _id by value.

And this also works, if you declare _id as int* in the header:

1
2
3
4
int* Student::get_id () const
{
	return _id;
}
that should not compile...
Last edited on
it works, try it
what the hell is going on?
why does this work?

I don't even get a warning or anything like that...
That's what I'm telling you, const function only prevents from changing inside it. Also, it can't call non-const functions. That's it.
Last edited on
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.
Last edited on
figured it out.

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 = new int(5);
    func(d); 
    
    std::cout << d << std::endl; // NOT 0
}
Last edited on
your code doesn't compile, d is undefined inside func
maybe you mean "cout<< data" instead of "cout<< d"
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
Last edited on
yep

Is your problem solved?
of course not, your example is inappropriate. sorry.
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 = new int(5);
    func(d); //Passing a copy here, d cannot be affected.
    std::cout << d << std::endl; // NOT 0
}

Is func()returns any value? What's the connection to my question?
Last edited on
Pages: 12