this

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

How to refer to enclosing instance from C++ inner class?

谁说我不能喝 提交于 2019-11-29 06:42:49
In C++, an object refers to itself via this . But how does an instance of an inner class refer to the instance of its enclosing class? class Zoo { class Bear { void runAway() { EscapeService::helpEscapeFrom ( this, /* the Bear */ ??? /* I need a pointer to the Bear's Zoo here */); } }; }; EDIT My understanding of how non-static inner classes work is that Bear can access the members of its Zoo , therefore it has an implicit pointer to Zoo . I don't want to access the members in this case; I'm trying to get that implicit pointer. BЈовић Unlike Java, inner classes in C++ do not have an implicit

Should I use `this` within a class?

对着背影说爱祢 提交于 2019-11-29 06:22:33
问题 Within a member function of a class in C++, does it make a difference, if I use this->dataMember or just dataMember ? What is considered better style? Is there any performance difference? (I am not talking about the case where a local variable has the same name as the data member, in which case you must, to my knowledge, use this-> to distinguish between them.) 回答1: As a general rule, it's a question of local conventions. Most of the places I've seen do not use this-> except when necessary,

Difference between this and base [closed]

与世无争的帅哥 提交于 2019-11-29 05:43:27
I am interested to know the difference between this and base object in C# . What is the best practice when using them? this represents the current class instance while base the parent. Example of usage: public class Parent { public virtual void Foo() { } } public class Child : Parent { // call constructor in the current type public Child() : this("abc") { } public Child(string id) { } public override void Foo() { // call parent method base.Foo(); } } The two keywords are very different. this refers to the current instance (not the “current class”). It can only be used in non-static methods

Usage of Java this keyword

荒凉一梦 提交于 2019-11-29 04:29:27
In a class constructor, I am trying to use: if(theObject != null) this = theObject; I search the database and if the record exists, I use theObject generated by Hibernate Query. Why can't I use this ? this is not a variable, but a value. You cannot have this as the lvalue in an expression. It's because 'this' is not a variable. It refers to the current reference. If you were allowed to reassign 'this', it would no longer remain 'this', it would become 'that'. You cannot do this. Because you can't assign to this . this represents the current object instance, i.e. yourself. You can consider this

Using this within a promise in AngularJS

て烟熏妆下的殇ゞ 提交于 2019-11-29 03:11:57
问题 Is there a best-practice solution to be able to use within in promise this? In jQuery i can bind my object to use it in my promise/callback - but in angularJS? Are there best-practice solutions? The way "var service = this;" i don't prefer ... app.service('exampleService', ['Restangular', function(Restangular) { this._myVariable = null; this.myFunction = function() { Restangular.one('me').get().then(function(response) { this._myVariable = true; // undefined }); } }]; Are there solutions for

C++ using this pointer in constructors

旧城冷巷雨未停 提交于 2019-11-29 02:57:01
In C++ , during a class constructor, I started a new thread with this pointer as a parameter which will be used in the thread extensively (say, calling member functions). Is that a bad thing to do? Why and what are the consequences? My thread start process is at the end of the constructor. The consequence is that the thread can start and code will start executing a not yet fully initialized object. Which is bad enough in itself. If you are considering that 'well, it will be the last sentence in the constructor, it will be just about as constructed as it gets...' think again: you might derive

Can I change the context of javascript “this”?

删除回忆录丶 提交于 2019-11-29 02:56:43
var UI$Contract$ddlForm_change = function() { //'this' is currently the drop down that fires the event // My question is can I change the context so "this" represents another object? this = SomeObject; // then call methods on the new "this" this.someMethod(someParam); }; is this possible? James No, it's not possible. You can call a method with a specified value for this (using method.apply() / method.call() ) but you cannot re-assign the keyword, this . You can't change what this refers to from inside the function. However, you can call a function in a specific context - so that this refers to

Why can I not use “super” variable from a static context, even though “super” refers to the parent class and NOT a class instance, unlike “this”?

亡梦爱人 提交于 2019-11-29 01:21:12
问题 I'm talking java language. Variable "this", when used inside a class, refers to the current instance of that class, which means you cannot use "this" inside a static method. But "super", when used inside a class, refers to the superclass of that class, not an instance of the superclass, which should mean that you can use "super" inside a static method. But it turns out you cannot. A possible explanation would be to say that "super" also refers to an instance of the superclass, but I can't see

Is there a name for “this” in Java?

泪湿孤枕 提交于 2019-11-29 01:06:36
Eclipse will give an error, "The left-hand side of an assignment must be a variable", when I try something like: public class Thing{ String a1; int a2; public void meth(){ Thing A = new Thing(); this = A; } } I had to assign each variable ( this.a1 = A.a1; this.a2 = A.a2; ) as a work around. Are there other ways to do this without going through each variable field? And if this is not a variable what is it called? this is a pseudo-variable that points to the current instance of the object, it can not be reassigned. It's also considered a keyword in the language, according to section §3.9 of the