猫眼电影 > 识骨寻踪 第四季
识骨寻踪 第四季海报封面图

识骨寻踪 第四季

Bones Season 4
电视剧 / 剧情 / 警匪 / 悬疑 / 爱情 / 犯罪
2008-09-03美国开播 / 40分钟
logo猫眼综合评分
8.2
星星星星星
IMDb 7.5
简介

[Solved] C++ - Why can't I call `this->foo()` in a constructor's member initializer list? David Asks: C++ - Why can't I call `this->foo()` in a constructor's member initializer list? I have a class that calls a member function to initialize some member variables. Code: class MyClass { public: MyClass() : x(foo()) {} private: int x; int foo() { return 42; } }; This compiles and works as expected. However, if I try to call the member function using this->foo() in the member initializer list, like this: Code: class MyClass { public: MyClass() : x(this->foo()) {} private: int x; int foo() { return 42; } }; I get a compilation error. Why is this the case? The error message I'm getting is something like “invalid use of 'this' at this point“. Code: #include <iostream> class MyClass { public: MyClass() : x(this->foo()) {} void printX() { std::cout << “x: “ << x << std::endl; } private: int x; int foo() { return 42; } }; int main() { MyClass obj; obj.printX(); return 0; } Code: error: invalid use of 'this' at this point MyClass() : x(this->foo()) {} ^~~~ Why can't I use this->foo() in the member initializer list, even though the member function foo() is defined within the same class? Is there a way to use this explicitly in the member initializer list to call a member function? Code: MyClass() : x(this->foo()) {} The reason you're getting the “invalid use of 'this' at this point“ error is that, in the member initializer list, the object is not yet fully constructed. The this pointer refers to the object being constructed, but at the time the member initializer list is being evaluated, the object's construction is still in progress. The member initializer list is executed before the body of the constructor. At this stage, the object's members may not have been initialized yet, and using the `this` pointer to call a member function can lead to undefined behavior. For example, if the member function `foo()` accesses other members of the class that are not yet initialized, it can cause issues. In your case, although the `foo()` function is simple and does not depend on other members, the compiler still enforces the rule that the `this` pointer cannot be used in the member initializer list in this way. ### Solution You can simply call the member function without using the `this` pointer in the member initializer list, as the member function is implicitly called on the object being constructed: ```cpp class MyClass { public: MyClass() : x(foo()) {} void printX() { std::cout << “x: “ << x << std::endl; } private: int x; int foo() { return 42; } }; ``` In this code, the `foo()` function is called without explicitly using the `this` pointer, and the code compiles and works correctly. The compiler understands that the member function is being called on the object being constructed, so there is no need to use `this` explicitly. So, in summary, avoid using the `this` pointer in the member initializer list to call member functions, as the object is not fully constructed at that point.

影视行业信息《免责声明》I 违法和不良信息举报电话:4006018900