member

How do you use the non-default constructor for a member?

纵然是瞬间 提交于 2019-11-26 05:38:40
问题 I have two classes class a { public: a(int i); }; class b { public: b(); //Gives me an error here, because it tries to find constructor a::a() a aInstance; } How can I get it so that aInstance is instantiated with a(int i) instead of trying to search for a default constructor? Basically, I want to control the calling of a\'s constructor from within b\'s constructor. 回答1: You need to call a(int) explicitly in the constructor initializer list: b() : aInstance(3) {} Where 3 is the initial value

Why can't we initialize members inside a structure?

别等时光非礼了梦想. 提交于 2019-11-26 05:27:31
问题 Why can\'t we initialize members inside a structure ? example: struct s { int i = 10; }; 回答1: If you want to initialize non-static members in struct declaration : In C++ (not C), structs are almost synonymous to classes and can have members initialized in the constructor. struct s { int i; s(): i(10) { } }; If you want to initialize an instance : In C or C++: struct s { int i; }; ... struct s s_instance = { 10 }; C99 also has a feature called designated initializers: struct s { int i; }; ...

C++ inline member function in .cpp file

十年热恋 提交于 2019-11-26 04:47:55
问题 I know that inline member functions by definition should go into the header. But what if it\'s not possible to put the implementation of the function into the header? Let\'s take this situation: File A.h #pragma once #include \"B.h\" class A{ B b; }; File B.h #pragma once class A; //forward declaration class B{ inline A getA(); }; Due to the circular include I have to put the implementation of getA into B.cpp #include \"B.h\" #include \"A.h\" inline A B::getA(){ return A(); } Will the

Static nested class in Java, why?

假装没事ソ 提交于 2019-11-26 02:34:10
I was looking at the Java code for LinkedList and noticed that it made use of a static nested class, Entry . public class LinkedList<E> ... { ... private static class Entry<E> { ... } } What is the reason for using a static nested class, rather than an normal inner class? The only reason I could think of, was that Entry doesn't have access to instance variables, so from an OOP point of view it has better encapsulation. But I thought there might be other reasons, maybe performance. What might it be? Note. I hope I have got my terms correct, I would have called it a static inner class, but I

How do you pass a member function pointer?

谁说我不能喝 提交于 2019-11-26 02:03:35
I am trying to pass a member function within a class to a function that takes a member function class pointer. The problem I am having is that I am not sure how to properly do this within the class using the this pointer. Does anyone have suggestions? Here is a copy of the class that is passing the member function: class testMenu : public MenuScreen{ public: bool draw; MenuButton<testMenu> x; testMenu():MenuScreen("testMenu"){ x.SetButton(100,100,TEXT("buttonNormal.png"),TEXT("buttonHover.png"),TEXT("buttonPressed.png"),100,40,&this->test2); draw = false; } void test2(){ draw = true; } }; The

How do you pass a member function pointer?

可紊 提交于 2019-11-26 01:49:58
问题 I am trying to pass a member function within a class to a function that takes a member function class pointer. The problem I am having is that I am not sure how to properly do this within the class using the this pointer. Does anyone have suggestions? Here is a copy of the class that is passing the member function: class testMenu : public MenuScreen{ public: bool draw; MenuButton<testMenu> x; testMenu():MenuScreen(\"testMenu\"){ x.SetButton(100,100,TEXT(\"buttonNormal.png\"),TEXT(\

C++ callback using class member

本秂侑毒 提交于 2019-11-26 00:46:07
问题 I know this has been asked so many times, and because of that it\'s difficult to dig through the cruft and find a simple example of what works. I\'ve got this, it\'s simple and it works for MyClass ... #include <iostream> using std::cout; using std::endl; class MyClass { public: MyClass(); static void Callback(MyClass* instance, int x); private: int private_x; }; class EventHandler { public: void addHandler(MyClass* owner) { cout << \"Handler added...\" << endl; //Let\'s pretend an event just

C++ callback using class member

扶醉桌前 提交于 2019-11-25 23:53:38
I know this has been asked so many times, and because of that it's difficult to dig through the cruft and find a simple example of what works. I've got this, it's simple and it works for MyClass ... #include <iostream> using std::cout; using std::endl; class MyClass { public: MyClass(); static void Callback(MyClass* instance, int x); private: int private_x; }; class EventHandler { public: void addHandler(MyClass* owner) { cout << "Handler added..." << endl; //Let's pretend an event just occured owner->Callback(owner,1); } }; EventHandler* handler; MyClass::MyClass() { private_x = 5; handler-

Instance variables vs. class variables in Python

时间秒杀一切 提交于 2019-11-25 22:46:04
问题 I have Python classes, of which I need only one instance at runtime, so it would be sufficient to have the attributes only once per class and not per instance. If there would be more than one instance (which won\'t happen), all instance should have the same configuration. I wonder which of the following options would be better or more \"idiomatic\" Python. Class variables: class MyController(Controller): path = \"something/\" children = [AController, BController] def action(self, request):