Why is C++ “this” poorly designed?

  1. For every a and b which are non-const pointers of the same type, you can do a = b;, right?

  2. Inside non-const member functions the this keyword exists, which is a non-const pointer. So logicaly if b is same type as this you can also do this = b; right?

Wrong.

You cannot do this = b;, because this uses pointer syntax but logically this is a reference!

But why on earth is this syntactically a pointer but logically reference?

Can this weird behavior be corrected in the next C++ standard, by introducing a new keyword, for example me which will be a reference not only logically but also syntactically?

(See also my attempt to solve this here: “Is it a good idea to “#define me (*this)”?“)

3

this is (like nullptr) a constant pointer; the pointed data is const if and only if this appears in the body of a const member function.

You cannot change a constant pointer, like you cannot change a constant literal like 23.

So assignment to this like

this = p;  // WRONG

is prohibited for the same reasons assignment to nullptr is forbidden:

nullptr = 0; // wrong

2

this is NOT poorly designed. You almost never have local variables with the same names as class member names. While this construction does occur in constructors for POD or nearly POD objects, the language has a better way of dealing with this. Behold:

#include <stdio.h>

class A {
    public:
        A(int m1, int m2) : m1(m1), m2(m2) {}
        int m1;
        int m2;
};

int main()
{
    A a(1, 2);
    printf("%d, %dn", a.m1, a.m2);
}

I’m sure we can agree this demonstration is full of horrors, but the point is obvious. The constructor has local variables with the same name as members and initializes the members with the same names as local variables. You can run this program and get the output “1, 2”.

Also, in C++ we expect that we may declare functions having good parameter names (for hinting) and bad names for the actual implementation. This can even be done for inline functions if we bother to declare them separately rather than bodily in the class definition.

For all of these reasons, a member function rarely needs to access its class explicitly but only its pointer, thus the this keyword is a pointer.

Historical artifact: this was once not const and could be assigned to. This was a bad idea.

One can make an argument that this is a bit clumsy.
Having a self-reference instead would probably feel a bit more natural nowadays:
Its type could then carry slightly more information in it than the type of this can, namely:

  1. Whether the member-function was guaranteed called on an rvalue-reference, and
  2. that it actually always refers to an object.

Still, when C++ finally got references (invented for operator-overloading, I think), the decision on this was already ancient history. And backwards-compatibility is no laughing matter.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật