this

C++ equivalent to Java this

空扰寡人 提交于 2019-11-30 06:03:45
In Java you can refer to the current object by doing: this.x = x . How do you do this in C++? Assume that each of these code examples are part of a class called Shape . Java: public void setX(int x) { this.x = x; } C++: public: void setX(int x) { //? } Same word: this Only difference is it is a pointer, so you need to use the -> operator: void setX(int x) { this->x = x; } The C++ equivalent is this , but there are a few differences. This is a pointer to the object in question, not a reference; so, you must use pointer dereferencing operators before accessing fields or methods. (*this).method(.

“this” cannot be used in typescript function (Angular)

眉间皱痕 提交于 2019-11-30 06:03:10
问题 I want to reference the keyword "this" in a typescript class in my Angular project. But it cannot be used. I always get the error that the variable I want to change is not defined. Here is my implementation: export class ContactComponent implements OnInit { contactForm: FormGroup; errorMsg:string = ''; redirect = ""; loggedIn(): void { this.redirect = "dashboard"; console.log("success"); in my HTML the redirect variable is connected to a routerLink like this: <a [routerLink]="redirect"></a> I

Java implicit “this” parameter in method?

拟墨画扇 提交于 2019-11-30 05:40:57
问题 Within the programming language Java do method invocations on an object, work by implicitly passing a reference to the object to act on and working as static methods? 回答1: Details on how method invocation works can be found in the Java SE 7 JVM specification, section 3.7. For an instance method the this reference is passed as the first parameter. This reference is also used to select which method to invoke, since it might be overridden in a subclass, so it is a bit more complicated than a

How to use $this in closure in php

断了今生、忘了曾经 提交于 2019-11-30 02:46:09
问题 I have function like this: class Service { function delete_user($username) { ... $sessions = $this->config->sessions; $this->config->sessions = array_filter($sessions, function($session) use ($this){ return $this->get_username($session->token) != $username; }); } } but this don't work because you can't use $this inside use , is it possible to execute function which is member of class Service inside a callback? Or do I need to use for or foreach loop? 回答1: $this is always available in (non

React: this is null in event handler

*爱你&永不变心* 提交于 2019-11-30 02:43:49
I have a LoginForm component. I want to check before submit, that both loginName and password is set. I tried with this code (a lot of stuff omitted): class LoginForm extends Component { constructor() { super(); this.state = { error: "", loginName: "", password: "", remember: true }; } submit(e) { e.preventDefault(); if(!this.state.loginName || !this.state.password) { //this is null this.setState({ error: "Fill in both fields" }); } else { console.log("submitting form"); } } render() { return ( <div className="col-xs-12 col-sm-6 col-md-4"> <form className="login" onSubmit={this.submit}>

Call static method from instance in PHP, future deprecation?

泄露秘密 提交于 2019-11-30 01:08:51
问题 While I understand the $this variable is not available when a method is called in a static context, to assist in decoupling my application components from one-another I figured it would make sense to call static methods from an instance. For example: class MyExample{ private static $_data = array(); public static function setData($key, $value){ self::$_data[$key] = $value; } // other non-static methods, using self::$_data } // to decouple, another class or something has been passed an

React.js - Implementing sorting of components

一个人想着一个人 提交于 2019-11-30 01:00:35
I'm trying to learn React concepts, especially re: state and dynamic UIs, by coding a small sports roster-like UI. I've included the code below and the whole app + visual is at http://codepen.io/emkk/pen/dGYXJO . This app basically creates player cards from an array of player objects I've defined earlier. I would like to implement sorting of the player cards upon button click. I've created a <Sort/> component that renders the said buttons. I'd attach event listeners but don't know how to have that reflected in my <Roster/> component. I have tried many different approaches with this.state but

Jquery UI Sortable - Get the item being sorted

自闭症网瘾萝莉.ら 提交于 2019-11-30 00:45:17
问题 When using Jquery UI Sortable (which is great by the way) how do you get the item that is currently being sorted. When you use $(this); it return the actual sortable list, not the current sorted item. I want to do fancy-pants things with the widget when the user is dragging it around. E.g. Animate it when dragging between two lists. So how do I get the current item being sorted? There a little code below just to explain a little more... $(function() { $("#sortable_1").sortable({ start :

Difference between Python self and Java this

醉酒当歌 提交于 2019-11-29 23:11:38
I had done a bit of Python long back. I am however moving over to Java now. I wanted to know if there were any differences between the Python "self" method and Java "this". I know that "self" is not a keyword while "this" is. And that is pretty much what I could figure out. Am I missing anything else? About self in Python (here is the source: Python self explanation ): The reason you need to use self . is because Python does not use the @ syntax to refer to instance attributes. Python decided to do methods in a way that makes the instance to which the method belongs be passed automatically,

Why must I use the “this” keyword for forward references?

守給你的承諾、 提交于 2019-11-29 22:46:56
When I use the this keyword for accessing a non-static variable in a class, Java doesn't give any error. But when I don't use it, Java gives an error. Why must I use this ? I know when should normally I use this , but this example is very different from normal usages. Example: class Foo { // int a = b; // gives error. why ? int a = this.b; // no error. why ? int b; int c = b; int var1 = this.var2; // very interesting int var2 = this.var1; // very interesting } Jason Variables are declared first and then assigned. That class is the same as this: class Foo { int a; int b; int c = b; int var1;