member

The member variable of a static struct in C

允我心安 提交于 2019-11-30 09:59:35
I have a question about the member variables of static struct in C language. Someone said we can declare a static struct , but in C, struct do not have the static members like class in C++, what does this mean? If I declare a static struct, what is the status of the members variable? can some one help me on this? Note that a static struct itself is different from a static member of a struct. While you can declare a static struct variable: static struct MyStruct s; you can't define a struct type with a static member: struct MyStruct { static int i; // <- compiler error }; The reason for this is

std::find Object by Member

南笙酒味 提交于 2019-11-30 09:25:03
问题 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

How can I Initialize a div_t Object?

白昼怎懂夜的黑 提交于 2019-11-30 09:21:12
问题 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? 回答1: EDIT: I think the VS

Why doesn't C++ have a pointer to member function type?

妖精的绣舞 提交于 2019-11-30 07:01:05
I could be totally wrong here, but as I understand it, C++ doesn't really have a native "pointer to member function" type. I know you can do tricks with Boost and mem_fun etc. But why did the designers of C++ decide not to have a 64-bit pointer containing a pointer to the function and a pointer to the object, for example? What I mean specifically is a pointer to a member function of a particular object of unknown type . I.E. something you can use for a callback . This would be a type which contains two values. The first value being a pointer to the function , and the second value being a

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

我只是一个虾纸丫 提交于 2019-11-30 05:14:40
This question already has an answer here: Start thread with member function 5 answers 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()() { cout << "I'm the operator(). Hi there!\n"; } }; void hello1() { cout << "Hello from outside class A\n"; } int main() { A

Hold any kind of C++ template class in member variable

六眼飞鱼酱① 提交于 2019-11-30 04:56:59
问题 I have got two classes. The first class (A) is builded with an template. template <class T> class A { public: T value; }; The second class (B) should have an object of class A as member variable. Like this: class B { public: A<int> value; }; But now i want to use any kind of template-class in class A. Not only int . Apparent I can't declare a (member-)variable which contains any kind of a class. So, I need something like this: class B { public: A<*> value; }; Is there any (clean) solution for

Nested Class Definition in source file

天大地大妈咪最大 提交于 2019-11-30 04:18:37
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 idea of why this happens but I would like a good explanation)? What about structures? sje397 You can.

Pointer to class member as template parameter

我的未来我决定 提交于 2019-11-30 01:30:52
Is it possible to have non-type template parameter which is actually a pointer to a class member? What I'm looking to do is something like the following: struct Person { Dog dog; }; template <?? ptr> struct Strange { // ... }; typedef Strange<&Person::dog> weird; My work so far leads me to believe that nothing of the sort is possible, but I'm curious if anyone has can say otherwise. From the standard: A non-type template-parameter shall have one of the following (optionally cv-qualified) types: integral or enumeration type, pointer to object or pointer to function, reference to object or

invalid use of non-static member function [duplicate]

谁都会走 提交于 2019-11-30 00:37:55
问题 This question already has answers here : problem sorting using member function as comparator (8 answers) Closed 4 years ago . I have something like this: class Bar { public: pair<string,string> one; std::vector<string> cars; Bar(string one, string two, string car); }; class Car { public: string rz; Bar* owner; Car(string car, Bar* p); }; class Foo { public: Foo ( void ); ~Foo ( void ); int Count ( const string & one, const string & two) const; int comparator (const Bar & first, const Bar &

Callback of member functions through 3d party library

∥☆過路亽.° 提交于 2019-11-29 17:51:32
I'm working with a particle simulation library. The way interactions are added to particles it through the following library function: AddInteraction(ParticleSet particleSet, void(*interaction)(xyz* p, xyz* v)) I now want to pass a member function to AddInteraction. I've understood that this is impossible to do without changing the library function. Changing the library is something I'd like to avoid, but if the change is small I can mail the author of the library and ask for it to be implemented. I want to write a simple example of how it can be done. Different solutions are possible: Lambda