this

Access protected member of a class in a derived class

落花浮王杯 提交于 2019-11-27 06:47:01
问题 i have an old codebase here, where they used protected member variables. Whether or not this is a good idea can be discussed. However, the code must have compiled fine with gcc3. I have a derived template class Bar that uses protected member x from class template Foo like so template <class Something> class Foo { public: // stuff... protected: some::type x; } template <class Something> Bar : Foo<Something> { public: void cleanup(); } And in the method declaration of cleanup() there is

How does require() in node.js work?

纵然是瞬间 提交于 2019-11-27 06:39:28
I tried this: // mod.js var a = 1; this.b = 2; exports.c = 3; // test.js var mod = require('./mod.js'); console.log(mod.a); // undefined console.log(mod.b); // 2 console.log(mod.c); // 3, so this === exports? So I image that require() may be implement like this: var require = function (file) { var exports = {}; var run = function (file) { // include "file" here and run }; run.apply(exports, [file]); return exports; } Is that right? Please help me to understand require(), or where can I find the source code. Thanks! Source code is here . exports / require are not keywords, but global variables.

std::shared_ptr of this

馋奶兔 提交于 2019-11-27 06:31:32
I am currently trying to learn how to use smart pointers. However while doing some experiments I discovered the following situation for which I could not find a satifying solution: Imagine you have an object of class A being parent of an object of class B (the child), but both should know each other: class A; class B; class A { public: void addChild(std::shared_ptr<B> child) { children->push_back(child); // How to do pass the pointer correctly? // child->setParent(this); // wrong // ^^^^ } private: std::list<std::shared_ptr<B>> children; }; class B { public: setParent(std::shared_ptr<A> parent

Confusion with “this” object in JavaScript anonymous functions

我的梦境 提交于 2019-11-27 06:28:48
问题 Hi I have following JavaScript code that I am trying to run. My aim is to grasp the meaning of this in different scopes and different types of invocations in JavaScript. If you look in code below: I have a inner anonymous function, which is getting assigned to innerStuff variable. In that anonymous function as such this points to window object and not the outer function object or anything else. Event though it still has access to out function's variables. Anyway, I am not sure, why that would

Java: Class.this

陌路散爱 提交于 2019-11-27 06:19:56
I have a Java program that looks like this. public class LocalScreen { public void onMake() { aFuncCall(LocalScreen.this, oneString, twoString); } } What does LocalScreen.this means in aFuncCall ? LocalScreen.this refers to this of the enclosing class. This example should explain it: public class LocalScreen { public void method() { new Runnable() { public void run() { // Prints "An anonymous Runnable" System.out.println(this.toString()); // Prints "A LocalScreen object" System.out.println(LocalScreen.this.toString()); // Won't compile! 'this' is a Runnable! //onMake(this); // Compiles! Refers

Use of “this” keyword in C++ [duplicate]

牧云@^-^@ 提交于 2019-11-27 06:08:34
Possible Duplicate: Is excessive use of this in C++ a code smell When should you use the "this" keyword in C++? Is there any reason to use this-> In C++, is the keyword this usually omitted? For example: Person::Person(int age) { _age = age; } As opposed to: Person::Person(int age) { this->_age = age; } orlp Yes, it is not required and is usually omitted. It might be required for accessing variables after they have been overridden in the scope though: Person::Person() { int age; this->age = 1; } Also, this: Person::Person(int _age) { age = _age; } It is pretty bad style; if you need an

Using “this” with class name

强颜欢笑 提交于 2019-11-27 06:02:07
I am doing Android programming and was learning about Intents, when I saw a constructor that, to my C# trained mind, seemed funky. The call was: Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class); Both of the parameters are new to me. How is there a static ".this" off of a Class Name? Is this a Java thing or an Android thing? I am assuming that it is the same as just saying "this", since I am in the context of CurrentActivity , but I don't get how the "this" can be called off of the Class name itself. Also. The ".class" looks like it is used for reflection, which I am

what is 'this' constructor, what is it for

一个人想着一个人 提交于 2019-11-27 05:38:41
I'm in the learning process and I have a question I havent been able to find a satisfactory answer for. this I need a rundown on it. I keep seeing it and people have suggested fixes for my code that use it. I really have no idea what exactly it does. If someone would be so kind as to give me a basic rundown on it I would be really happy. It's used to refer to another constructor in the same class. You use it to "inherit" another constructor: public MyClass() {} public MyClass(string something) : this() {} In the above, when the second constructor is invoked, it executes the parameterless

$(this) doesn't work in a function

你离开我真会死。 提交于 2019-11-27 05:30:15
The following code loads html content from a file (i used this thread ) <script> $.fn.loadWithoutCache = function (){ $.ajax({ url: arguments[0], cache: false, dataType: "html", success: function(data) { $(this).html(data); // This is not working //$('#result').html(data); //THIS WORKS!!! alert(data); // This alerts the contents of page.html } }); } $('#result').loadWithoutCache('page.html'); </script> Please let me know what the problem is? I hope it's something stupid :) Edit: CORRECT CODE <script> $(document).ready(function() { $.fn.loadWithoutCache = function (){ var $el = $(this); $.ajax(

Javascript lost context when assigned to other variable

房东的猫 提交于 2019-11-27 05:21:19
Why in javascript if you reference objects method to some variable it loses that objects context. Can't find any link with explanation what happens under the hood. Except this one which states: ‘this’ refers to the object which ‘owns’ the method which doesn't seam to be true. var Class = function() { this.property = 1 } Class.prototype.method = function() { return this.property; } var obj = new Class(); console.log(obj.method() === 1); var refToMethod = obj.method; // why refToMethod 'this' is window console.log(refToMethod() !== 1) // why this is true? var property = 1; console.log