this-pointer

Why is context different in these two event handlers

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-05 07:08:21
问题 This is a basics question but I cannot figure out why the context ( the 'this' pointer ) is correct in the second event handler and incorrect in the first one. I have this simple constructor function to create the object myNotifier: function Notifier ( message ) { this.message = message; this.saySomething = function () { alert( "I say:" + this.message); } } myNotifier = new Notifier(" HELLO!"); Then I use the myNotifier.saySomething() method as an event handler for CLICK on two buttons: $("

Does this pointer adjustment occur for non-polymorphic inheritance?

为君一笑 提交于 2020-01-02 10:01:14
问题 Does non-polymorphic inheritance require this pointer adjustment? In all the cases I've seen this pointer adjustment discussed the examples used involved polymorphic inheritance via keyword virtual . It's not clear to me if non-polymorphic inheritance would require this pointer adjustment. An extremely simple example would be: struct Base1 { void b1() {} }; struct Base2 { void b2() {} }; struct Derived : public Base1, Base2 { void derived() {} }; Would the following function call require this

Does this pointer adjustment occur for non-polymorphic inheritance?

隐身守侯 提交于 2020-01-02 10:01:11
问题 Does non-polymorphic inheritance require this pointer adjustment? In all the cases I've seen this pointer adjustment discussed the examples used involved polymorphic inheritance via keyword virtual . It's not clear to me if non-polymorphic inheritance would require this pointer adjustment. An extremely simple example would be: struct Base1 { void b1() {} }; struct Base2 { void b2() {} }; struct Derived : public Base1, Base2 { void derived() {} }; Would the following function call require this

Can you explain the concept of the this pointer? [closed]

∥☆過路亽.° 提交于 2020-01-02 01:11:07
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 9 years ago . I need to understand this pointer concept, preferably with an example. I am new to C++, so please use simple language, so that I can understand it better. 回答1: this is a pointer to an instance of its class and

copy constructor of a class which has self-pointer to itself in C++?

大兔子大兔子 提交于 2020-01-01 19:34:07
问题 I wanted to ask that how will we implement a copy constructor of a class which has self pointer to itself as its data member, i want to implement a deep copy, class City { string name; City* parent; public: City(string nam, double dcov); City(string nam, double dcov, City* c); City(const City& obj) { this-> name = obj.name; // how to assign parent parent = new City(??) } ~City(); void setName(string name1); void setDistanceCovered(int dist); string getName(); double getDistanceCovered(); City

Address of pointer “this” changed unexpectedly inside a pointer to member function call

感情迁移 提交于 2019-12-25 01:25:43
问题 I have problem with the pointer to member function call. The address of pointer "this" outside the function pointer call is different than inside the call thus all access to the class variables results wrong values. I include the code here. class ClassInterface { public: ClassInterface(void); ~ClassInterface(void); }; class ClassA:public ClassInterface { public: float _a; public: ClassA(void); ~ClassA(void); virtual void Update(); }; class ClassB:public ClassA { public: ClassB(void); ~ClassB

delete this pointer behaviour in g++

好久不见. 提交于 2019-12-24 01:44:09
问题 #include <stdio.h> class Foo { public: Foo(char x); Foo(char x, int y); ~Foo(); void abc(); void dev(); }; void Foo::dev() { printf("inside dev \n"); } void Foo::abc() { printf("inside abc \n"); delete this; dev(); } Foo::Foo(char x) { printf("inside 1 argu const---------------"); } Foo::~Foo() { printf("inside 1 argu dest---------------"); } #include "test.h" int main() { Foo *obj=new Foo('a'); printf("%u inside main\n", obj); obj->abc(); return 0; } After looking at the output of the

Return “this” as rvalue

人盡茶涼 提交于 2019-12-23 10:07:11
问题 The following code does, as expected, not compile #include <iostream> class A { public: A() = default; ~A() = default; A(const A&) = delete; A(A&&) = delete; A& operator=(const A&) = delete; A& operator=(A&&) = delete; A& operator<<(const int i) { std::cout << "operator<< called" << std::endl; return *this; } }; void foo(A&& a) { std::cout << "foo called" << std::endl; } int main() { A a; a << 14; foo(std::move(a)); // works fine foo(A() << 14); // does not compile return 0; } Changing the

C++ how to pass 'this' to pointer reference

爷,独闯天下 提交于 2019-12-22 09:06:56
问题 i have main class that i like to pass its pointer reference to on of the objects i creating but it gives me error : Error 1 error C2664: 'GameController::GameController(GameLayer *&)' : cannot convert parameter 1 from 'GameLayer *const ' to 'GameLayer *&' what i have in the GameLayer ( the main object ) m_pGameController = new GameController(this); and in the GameController i have this constructor GameController(GameLayer*& GameLayer) { setGameLayer(gameLayer); // to GameLayer memeber ) } the

virtual method table for multiple-inheritance

ぐ巨炮叔叔 提交于 2019-12-19 07:10:12
问题 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: