member

this pointer and member function address

丶灬走出姿态 提交于 2019-11-29 17:49:58
I'm trying to get the address of a member function, but I don't know how. I would appreciate if somebody could tell me what I'm doing wrong. As you can see in my example below, neither (long)&g nor (long)&this->g work and I can't figure out the correct syntax: /* Create a class that (redundantly) performs data member selection and a member function call using the this keyword (which refers to the address of the current object). */ #include<iostream> using namespace std; #define PR(STR) cout << #STR ": " << STR << endl; class test { public: int a, b; int c[10]; void g(); }; void f() { cout <<

How can I Initialize a div_t Object?

不羁的心 提交于 2019-11-29 15:23:13
So the order of the members returned from the div functions seems to be implementation defined. Is quot the 1 st member or is rem ? Let's say that I'm doing something like this: generate(begin(digits), end(digits), [i = div_t{ quot, 0 }]() mutable { i = div(i.quot, 10); return i.rem; }) Of course the problem here is that I don't know if I initialized i.quot or i.rem in my lambda capture. Is intializing i with div(quot, 1) the only cross platform way to do this? EDIT: I think the VS workaround could look like this: #include <cstdlib> #include <type_traits> template<class T> struct DTMaker {

std::find Object by Member

☆樱花仙子☆ 提交于 2019-11-29 15:09:26
Scenario I’ve run into a speedbump while using the STL with what seems like a normal scenario, simplified here: class Person { string Name; int Age; }; vector<Person> people; AddPeople(people); string s("Bob"); find(people.begin(), people.end(), s); Problem Unfortunately find wants to compare the entire class. Question Is there a better or more appropriate way to do this the “STL way”? The suggested questions weren’t helpful, but I managed to find a couple of related questions but no direct solution. Work-arounds/Tests There’s some potential work-arounds: Forgo find altogether (cluttered, but

Constant Member Functions

喜夏-厌秋 提交于 2019-11-29 13:27:42
After reading this , it is my understanding that declaring a method as const prevents it from accidentally modifying the class's member variables. Are const methods commonly used? Should they be used for everything that shouldn't modify member variables? Yes, const should always be used, when appropriate. It lets your compiler check your application logic, statically asserting const-correctness for free! Some people even say that const should be the default, and you should be forced to use mutable for what is non-constant. I use const extensively to communicate design intent. If I intended

static class member of class's own type [duplicate]

此生再无相见时 提交于 2019-11-29 07:14:38
Possible Duplicate: Do static members of a class occupy memory if no object of that class is created? Memory Allocation of Static Members in a Class "A class is not considered defined untill its class body is complete, a class can not have data members of its own type. A class can have data members that are pointers/reference to its own type." C++ Primer (Lippman Lajoie) Makes sense. But why is this allowed then ? class justAClass { public : justAClass(); private : static justAClass justAMember; } For pointers it is understandable. But how will this above thing work ? How will i ever decide

Singleton Class in C++

ⅰ亾dé卋堺 提交于 2019-11-29 05:15:54
I have used singleton calss following the example: singleton class But i get the error as "Unresolved external symbols" this is the code i tried out: #include<iostream> using namespace std; class singleton { int value; static singleton *instance; protected: singleton() { value=0; } public: static void initialize() { if(instance==NULL) singleton(); else cout<<"An instance of singleton already exist..."; } static singleton& getInstance() { return *instance; } int getValue() { return value; } }; void main() { singleton::initialize(); } A little bit explanation on Singleton classes would be really

C++ 11 Thread initialization with member functions compiling error [duplicate]

柔情痞子 提交于 2019-11-29 02:45:42
问题 This question already has answers here : Start thread with member function (5 answers) Closed 6 years ago . I'm just starting to use C++ 11 threads and I've been struggling on a (probably silly) error. This is my example program: #include <iostream> #include <thread> #include <future> using namespace std; class A { public: A() { cout << "A constructor\n"; } void foo() { cout << "I'm foo() and I greet you.\n"; } static void foo2() { cout << "I'm foo2() and I am static!\n"; } void operator()()

Nested Class Definition in source file

感情迁移 提交于 2019-11-29 02:14:24
问题 If I have a nested class like so: class MyClass { class NestedClass { public: // nested class members AND definitions here }; // main class members here }; Currently, the definitions of MyClass are in the CPP file but the definitions for NestedClass are in the header file, that is, I cannot declare the functions/constructors in the CPP file. So my question is, how do I define the functions of NestedClass in the cpp file? If I cannot, what is the reason (and if this is the case, I have a vague

What's the difference between the square bracket and dot notations in Python?

好久不见. 提交于 2019-11-29 01:08:30
I come from a Javascript background (where properties can be accessed through both . and [] notation), so please forgive me, but what, exactly, is the difference between the two in Python? From my experimentation it seeems that [] should always be used, both to get the index of a list or set and to get the value from a certain key in a dictionary . Is this correct, and, if not, when do you use a . in Python? The dot operator is used for accessing attributes of any object. For example, a complex number >>> c = 3+4j has (among others) the two attributes real and imag : >>> c.real 3.0 >>> c.imag

Member variables in ES6 classes

巧了我就是萌 提交于 2019-11-29 01:02:47
Is there any way to use the ECMAScript6 class notation to declare either a static class variable or a default value for an instance variable? Without class what I have in mind would be written as function MyClass(arg) { if(arg) this.arg = arg; } MyClass.classVariable = 42; MyClass.prototype.arg = "no arg specified"; The most obvious ES6-like notation in my opinion would have been class MyClass { constructor(arg) { if(arg) this.arg = arg; } static let classVariable = 42; let arg = "no arg specified"; } But this doesn't work, since according to the current spec draft the only productions of