this-pointer

Assigning C++ function pointers to member functions of the same object

廉价感情. 提交于 2019-12-12 11:05:58
问题 How do I get the function pointer assignments (and maybe the rest) in test.calculate to work? #include <iostream> class test { int a; int b; int add (){ return a + b; } int multiply (){ return a*b; } public: int calculate (char operatr, int operand1, int operand2){ int (*opPtr)() = NULL; a = operand1; b = operand2; if (operatr == '+') opPtr = this.*add; if (operatr == '*') opPtr = this.*multiply; return opPtr(); } }; int main(){ test t; std::cout << t.calculate ('+', 2, 3); } 回答1: There are

C++ Assigning this pointer of a class to either a unique_ptr or a shared_ptr

﹥>﹥吖頭↗ 提交于 2019-12-12 03:56:45
问题 I have a base class that I want to inherit from and before any of its derived classes can be declared at least 1 instance of the base class must be declared first. I was thinking about storing the this pointer of the base class into its own member variable of a unique_ptr instead of using a static_ptr . Also the base class will keep track of all instances of its derived classes until another base class is declared. Here is what my class declarations look like: #include <vector> #include <map>

When to use THIS keyword when working with controls on form in C#

走远了吗. 提交于 2019-12-10 14:12:21
问题 I am still far away from mastering C#, but the child in me is pushing me to continue improving my programming day by day. When I make a WinForms application I want to change and use lot of controls pragmatically. What I do not understand is when I need to use the this.control keyword and when I should use just control . Sample: If I want to change the text of my label I can write mylabel.text = "Text for label" or this.mylabel.tex = "Text for label" Which of these is the right way? Is there a

Does this pointer adjustment occur for non-polymorphic inheritance?

﹥>﹥吖頭↗ 提交于 2019-12-06 08:10:18
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 pointer adjustment? Derived d; d.b2(); In this case the this pointer adjustment would clearly be

Replacing std::function from within itself (by move-assignment to *this?)

喜欢而已 提交于 2019-12-06 05:13:47
问题 Is it possible to replace one std::function from within itself with another std::function ? The following code does not compile: #include <iostream> #include <functional> int main() { std::function<void()> func = []() { std::cout << "a\n"; *this = std::move([]() { std::cout << "b\n"; }); }; func(); func(); func(); } Can it be modified to compile? The error message right now is: 'this' was not captured for this lambda function - which I completely understand. I don't know, however, how I could

compiler's detail of this pointer, virtual function and multiple-inheritance

余生长醉 提交于 2019-12-06 02:58:39
问题 I'm reading Bjarne's paper: Multiple Inheritance for C++. In section 3, page 370, Bjarne said that "The compiler turns a call of a member function into an "ordinary" function call with an "extra" argument; that "extra" argument is a pointer to the object for which the member function is called." I'm confused by the extra this argument. Please see the following two examples: Example 1 :(page 372) class A { int a; virtual void f(int); virtual void g(int); virtual void h(int); }; class B : A

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

北城以北 提交于 2019-12-05 17:42:21
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 resone is i need to be able to modify the data in GameLayer from GameController (GUI stuff) First, the

How to get this pointer from std::function?

我与影子孤独终老i 提交于 2019-12-05 00:02:39
问题 Since std::function can hold member functions, so it must store a pointer to the object instance somewhere. How can I fetch the this pointer from a std::function that holds a member function? 回答1: An object of type std::function holds a callable object . A pointer to member function is a kind of callable object; it can be called with an argument of the appropriate class type, plus any additional arguments that it needs. For example: struct S { void f(int); }; std::function<void(S, int)> g(&S:

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

懵懂的女人 提交于 2019-12-04 18:39:34
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* getParent(){return parent;} }; I am confused that this line // how to assign parent parent = new City

Replacing std::function from within itself (by move-assignment to *this?)

给你一囗甜甜゛ 提交于 2019-12-04 10:28:44
Is it possible to replace one std::function from within itself with another std::function ? The following code does not compile: #include <iostream> #include <functional> int main() { std::function<void()> func = []() { std::cout << "a\n"; *this = std::move([]() { std::cout << "b\n"; }); }; func(); func(); func(); } Can it be modified to compile? The error message right now is: 'this' was not captured for this lambda function - which I completely understand. I don't know, however, how I could capture func 's this -pointer. I guess, it is not even a std::function inside the lambda, yet?! How