Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the type of this* in C++

it might sound stupid.in C++prime 5th edition P258,it says:

by default, the type of this is a const pointer to the nonconst version of the class type.for example,by default, the type of this in a Sales_data member function is Sales_data *const.

i can understand that for this* is a const pointer which means the object it points once initialized cannot change.but then it says:

although this is implicit, it follows the normal initialization rules,which means that(by default)we cannot bind this to a const object.

but i wrote the following codes,it was still compiled fine:

class Test{
public:
    Test() = default;
    Test(const string &s): teststr(" ") {};
    Test(int a) : testint(a) {};
    Test(const string &s, int a): teststr(s), testint(a) {};
    string getstr() const { return teststr; };
    int getint() { return testint; };   //there is no const here
private:
    string teststr;
    int testint = 0;
};

int main(){
    Test a("abc",2);

    cout << a.getint() << " ";
    cout << a.getstr() << endl;
    cout << endl;

    return 0;
}

so my question is : if the compiler can compile it fine whether there is a 'const' or not,why does it matter? and then the book says:

after all,the body of isbn doesn't change the object to which this points, so our function would be more flexible if this were a pointer to const.

and i'm wondering what is the flexiblity is?would you show me some examples?

like image 739
ItsJingran Avatar asked Dec 22 '14 16:12

ItsJingran


2 Answers

For beginners, this is often depicted as a constant pointer.

However, this is actually a prvalue (pure rvalue) of pointer type. You can't assign anything to prvalues of fundamental type, which implies the "const-ness" of this.

The exact type of this depends on the cv-qualification of the method. A rule of thumb is that the cv-qualification is simply prepended to the usual pointer type - i.e., if a method of Class is marked const, then the type is const Class*.

if the compiler can compile it fine whether there is a 'const' or not,why does it matter?

If (and only if) the pointee type of this is const, you can't modify the members of the class.

Class const* ptr; // ptr->data is also const, not modifiable through this pointer

Class* ptr; // ptr->data isn't const - can be modified.

The const-qualifier on methods allows you to distinguish between methods for const objects and methods for non-const ones, which is often a necessity.

like image 77
Columbo Avatar answered Sep 30 '22 17:09

Columbo


According to the C++ Standard (9.3.2 The this pointer)

1 In the body of a non-static (9.3) member function, the keyword this is a prvalue expression whose value is the address of the object for which the function is called. The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*.

As you see there is nothing said that this has type ClassTYpe * const of ClassType const * const. It is a prvalue that may not be modified as any prvalue except that for a prvalue of a class type you may call non-const member functions..

As for you then you mix two types a constant pointer and a pointer that points to a constant data. For example this declaration

const ClassType *p;

does not declare a constant pointer. So the pointer itself may be not initialized. On the other hand thsi declaration

ClassTYpe * const p = new ClassTYpe;

declares a constant pointer and the pointer itself shall be initialized as any other constant.

As for this quote from your book

after all,the body of isbn doesn't change the object to which this points, so our function would be more flexible if this were a pointer to const

Then it means that it would be better to define the function with qualifier const. In this case it could be called for constant and non-constant objects. Otherwise it may be called only for-non constant objects because within the function the pointer type of this is not const ClassTYpe *.

like image 29
Vlad from Moscow Avatar answered Sep 30 '22 17:09

Vlad from Moscow