Understanding char* - C++

Viewed 187

I am writing a code in C++ right now, and I have a dilemma.

EDIT: I added the class relevant definition


  • _fullName is defined in the class as a private dm(char _fullName)

my class contains the getter method below:

char* getFullName() const { return this->_fullName; }

However, I had in the past cases in which I returned char* with const(const char*)
I can change the getter method to this one:

const char* getFullName() const { return this->_fullName; }

Both versions are compiling but I don't know how to choose. I would take the second version, but I wonder
Why even the version without the const is compiling? shouldn't it give an error when I remove the const keyword, because the function is a CONST member function and therefore the dms are const as well and cannot be returned without the const keyword???

This is the class relevant definition:

class Professional
{
private:
    char* _ID;
    char* _fullName;
    int _age;
    char* _profession;
}
2 Answers

First understand what const attached to a member function means. It determines the const-ness of the implicit this used for the member. Given this:

struct S
{
    char value[10];

    const char *mfn() const { return value; }
};

within the confines of mfn the implicit this is const S *. Furthermore, all references to members obtained therein are also const. In this case, that means value as an expression representation is const char (&)[10] before any further conversion are attached therein.

That's important.

In the correct posted example above there is an automatic conversion to temporary pointer, since const char(&)[10] converts to const char* without objection. However, the following will fail:

struct S
{
    char value[10];

    char *mfn() const { return value; }
};

The reason this will fail is simply because there is no conversion possible. value, remember, is const char (&)[10], and can only implicitly convert to const char*.

But how could it succeed? Well, consider this:

struct S
{
    char *ptr;

    char *mfn() const { return ptr; }
};

This will work. The reason is subtle, but obvious once you think about it. ptr is a char*. The attachment of const to this makes that char * const&, not const char *& (there is a difference; the former is a reference to a non-mutable pointer to mutable data, the latter is a reference to a mutable pointer to non-mutable data). Since return ptr is not mutating ptr (or anything else in the object), it is viable as a result.

In short, in your case it works because the pointer you are returning isn't via some implicit conversion from a const underlayment; it's just a value.

You have a few things going on here, in your example of the int num member this is not the same as a char * member.

lets expand your example a bit:

class Professional
{

    int* func() const
    {
        return &myInt; // Fails because it returns a reference to a member variable
    } 

    char* func() const
    {
        return &myChar; // Fails because it returns a reference to a member variable
    } 

    int* func() const
    {
        return pInt; // ok because it returns a COPY of the member pointer
    } 

    char* func() const
    {
        return pChar; // ok because it returns a COPY of the member pointer
    } 

    int myInt;
    char myChar;
    int *pInt;
    char *pChar;

Note: the two pointers getting returned by copy are ok. Its the pointers that are copies not the thing being pointed to. The memory that is pointed to is nothing to do with this class.

As to an answer to your question: there is a rule of thumb that is to apply const as much as possible - this helps to show other users your intent and gains benefits from compiler warnings / errors if a user tries to use your class in a way you did not intend. So in this case pass back a const char *.

Note: See this answer for more specific details here: What is the difference between const int*, const int * const, and int const *?

Related