this-pointer

restrict qualifier on member functions (restrict this pointer)

旧时模样 提交于 2019-12-01 20:16:12
问题 Note: To clarify, the question is not about the use of the restrict keyword in general, but specifically about applying it to member functions as described here. gcc allows you to use the __restrict__ (the GNU++ equivalent to C99's restrict ) qualifier on a member function, effectively making this a restrict qualified pointer within the function's scope. Where is the beef? Most member functions work on other members, accessing them via this , which is a T* const (and normally unaliased). For

restrict qualifier on member functions (restrict this pointer)

浪尽此生 提交于 2019-12-01 18:20:17
Note: To clarify, the question is not about the use of the restrict keyword in general, but specifically about applying it to member functions as described here . gcc allows you to use the __restrict__ (the GNU++ equivalent to C99's restrict ) qualifier on a member function, effectively making this a restrict qualified pointer within the function's scope. Where is the beef? Most member functions work on other members, accessing them via this , which is a T* const (and normally unaliased). For this to be possibly aliased, there would need to be a second pointer-to-type in use within the member

This-pointer capture in lambda wrapper around recursive function

给你一囗甜甜゛ 提交于 2019-12-01 09:58:13
I have a class template Wrap<T> with a recursive member function test(int) that I want to pass to an STL algorithm with a lambda ( std::accumulate in the code below). If I use a default capture list of = , and make my recursive meber function static , all is fine and get the result I want. However, if I make it a non-static member function, both Visual C++ and gcc 4.7.2 complain about an unitialized this -pointer, unless I qualify my recursive call as this->test() . #include <algorithm> #include <iostream> #include <vector> template<typename T> struct Wrap { static int test1(int depth) { std:

This-pointer capture in lambda wrapper around recursive function

旧时模样 提交于 2019-12-01 06:14:42
问题 I have a class template Wrap<T> with a recursive member function test(int) that I want to pass to an STL algorithm with a lambda ( std::accumulate in the code below). If I use a default capture list of = , and make my recursive meber function static , all is fine and get the result I want. However, if I make it a non-static member function, both Visual C++ and gcc 4.7.2 complain about an unitialized this -pointer, unless I qualify my recursive call as this->test() . #include <algorithm>

virtual method table for multiple-inheritance

六眼飞鱼酱① 提交于 2019-12-01 04:06:58
I'm reading this article " Virtual method table " Example in the above article: class B1 { public: void f0() {} virtual void f1() {} int int_in_b1; }; class B2 { public: virtual void f2() {} int int_in_b2; }; class D : public B1, public B2 { public: void d() {} void f2() {} // override B2::f2() int int_in_d; }; B2 *b2 = new B2(); D *d = new D(); In the article, the author introduces that the memory layout of object d is like this: d: D* d--> +0: pointer to virtual method table of D (for B1) +4: value of int_in_b1 B2* b2--> +8: pointer to virtual method table of D (for B2) +12: value of int_in

ES6: this within static method

眉间皱痕 提交于 2019-11-29 12:09:25
Let's say I have two ES6 classes like this: class Base { static something() { console.log(this); } } class Derived extends Base { } And then I make a call like this: Derived.something(); Note that I am making a call to a static method defined on the super class via sub class. This does not give me errors. It prints [Function: Derived] So accessing this within a static method seems to work here. I need a common static method for all sub-classes of a super class and I need to be able to know what sub-class is calling this method. Now my question is whether using this within a static method is

Not possible: this pointer as a default argument. Why?

瘦欲@ 提交于 2019-11-29 07:11:32
The following code won't compile. Why? class A { int j; void f( int i = this->j ); } Edit, for clarity. This is what I was trying to do, using less lines of code... class A { void f( int i ){}; void f( ); int j; }; void A::f() { f( j ); } Eric Default argument values are bound at compile time. "this" is only defined at run time, so can't be used. See here for a fuller explanation: Must default function parameters be constant in C++? Others have already commented on the reason this doesn't work. From one of the comments: "...The expression can combine functions that are visible in the current

After using 'delete this' in a member function I am able to access other member functions. Why?

徘徊边缘 提交于 2019-11-28 14:19:58
I just wrote a sample program to see the behaviour of delete this class A { ~A() {cout << "In destructor \n ";} public: int a; A() {cout << "In constructor \n ";} void fun() { cout << "In fun \n"; delete this; cout << this->a << "\n"; // output is 0 this->fun_2(); // how m able to call fun_2, if delete this is called first ?? } void fun_2() { cout << "In fun_2 \n"; } main() { A *ptr = new A; ptr->a = 100; ptr->fun(); //delete this will be executed here ptr->fun_2(); //how m able to execute fun_2 when this pointer is deleted ?? cout<< ptr->a << "\n"; //prints 0 return 0; } > Output In

ES6: this within static method

╄→尐↘猪︶ㄣ 提交于 2019-11-28 05:13:47
问题 Let's say I have two ES6 classes like this: class Base { static something() { console.log(this); } } class Derived extends Base { } And then I make a call like this: Derived.something(); Note that I am making a call to a static method defined on the super class via sub class. This does not give me errors. It prints [Function: Derived] So accessing this within a static method seems to work here. I need a common static method for all sub-classes of a super class and I need to be able to know

Where is the 'this' pointer stored in computer memory?

做~自己de王妃 提交于 2019-11-28 04:49:01
Where exactly is the 'this' pointer stored in memory? Is it allocated on the stack, in the heap, or in the data segment? #include <iostream> using namespace std; class ClassA { int a, b; public: void add() { a = 10; b = 20; cout << a << b << endl; } }; int main() { ClassA obj; obj.add(); return 0; } In the above code I am calling the member function add() and the receiver object is passed implicitly as the 'this' pointer. Where is this stored in memory? Other answers have done a very good job explaining how a typical compiler implements this (by passing it as an implicit first parameter to the