member-variables

Sorting function for a vector of objects based on different fields

最后都变了- 提交于 2020-02-25 04:41:47
问题 I have a vector of objects: struct Student{ string name; string id; string major; int age; }; vector<Student> s; Is there any way to write ONE general (maybe template) function to sort this vector (or array) based on different fields instead of writing four different functions? 回答1: I think the previous comments all said that it is possible to write such a comparison function. But if I understand you correctly, you want one function for all 4 comparisons (maybe in a templated way). There is

C++ init-list: using non-initialized members to initialize others gives no warning

隐身守侯 提交于 2020-01-13 08:27:41
问题 Neither g++ (4.4 and 4.6) nor clang++ (3.2) nor coverity, with -Wall and -Wextra (+ some others) or -Weverything respectively gives me a warning for the following code snippet: class B { char *t2; char *t; public: B() : t2(t), t(new char[100]) {} }; I would at least expect a small warning about the usage of uninitialized (member-) variables. Is there something I'm missing? Is this a wanted "no-warning"-scenario. I have (now had) at least one bug in my software which was hard to find. EDIT :

Audiolib.js: Changing values of existing “objects”

徘徊边缘 提交于 2019-12-24 21:40:24
问题 so I looked into Audiolib.js and can make some basic stuff but what I don't know and can't figure out by searching through the internet. The question is how I can change specific values of some "objects" without "re-appending" it. For example: var osc; osc = audioLib.Oscillator(44100 /*or some other samplerate */ , 400 ); function changefrequency(freq) osc.frequency = freq; /* is there a way to do stuff like this? */ (Btw. with "objects" I mean oscillators, lfo's, filters and so on.) Since

mem_fn to function of member object

妖精的绣舞 提交于 2019-12-24 04:08:09
问题 I was tinkering around with the std::mem_fn and couldn't manage to bind it to data/functions of an member of a struct (one layer deeper). I hope that the code shows the problem better than I can describe it, because I'm not familiar with the terminology. #include <functional> struct Int { Int(int _x = 0) : x(_x) {} int GetInt() const { return x; } int x; }; struct IntWrapper { IntWrapper(int _x = 0) : test(_x) {} int GetWrappedInt() const { return test.GetInt(); } Int test; }; int main() {

Initialization of member array objects avoiding move constructor

末鹿安然 提交于 2019-12-24 01:07:27
问题 I'm trying to create and initialize a class that contains a member array of a non-trivial class, which contains some state and (around some corners) std::atomic_flag . As of C++11 one should be able to initialize member arrays. The code (stripped down to minimum) looks like this: class spinlock { std::atomic_flag flag; bool try_lock() { return !flag.test_and_set(std::memory_order_acquire); } public: spinlock() : flag(ATOMIC_FLAG_INIT){}; void lock() { while(!try_lock()) ; } void unlock() {

C++: Copy constructor: Use getters or access member vars directly?

陌路散爱 提交于 2019-12-24 00:58:23
问题 I have a simple container class with a copy constructor. Do you recommend using getters and setters, or accessing the member variables directly? public Container { public: Container() {} Container(const Container& cont) //option 1 { SetMyString(cont.GetMyString()); } //OR Container(const Container& cont) //option 2 { m_str1 = cont.m_str1; } public string GetMyString() { return m_str1;} public void SetMyString(string str) { m_str1 = str;} private: string m_str1; } In the example, all code is

Is it possible to have a static member variable in my templated class, without the class's user having to know about it?

房东的猫 提交于 2019-12-23 08:47:10
问题 I have a templated container class, something like this toy code: template <class ItemType> class MyVector { public: MyVector() : _numItems(0), _items(NULL) {/* empty */} /** Returns a reference to the first item in our array, * or a default-constructed item if the array is empty. */ const ItemType & GetFirstItemWithDefault() const { return (_numItems > 0) ? _items[0] : _defaultItem; } [other methods omitted because they aren't relevant] private: int _numItems; // how many valid items (_items

When and why to declare member variables on the heap C++

谁都会走 提交于 2019-12-18 12:18:42
问题 Ok, so I'm very new at C++ programming, and I've been looking around for a couple days for a decisive answer for this. WHEN should I declare member variables on the heap vs. the stack? Most of the answers that I've found have dealt with other issues, but I want to know when it is best to use the heap for member variables and why it is better to heap the members instead of stacking them. 回答1: There are two important concepts to grasp first: One should avoid thinking in terms of "heap" and

When and why to declare member variables on the heap C++

陌路散爱 提交于 2019-12-18 12:18:32
问题 Ok, so I'm very new at C++ programming, and I've been looking around for a couple days for a decisive answer for this. WHEN should I declare member variables on the heap vs. the stack? Most of the answers that I've found have dealt with other issues, but I want to know when it is best to use the heap for member variables and why it is better to heap the members instead of stacking them. 回答1: There are two important concepts to grasp first: One should avoid thinking in terms of "heap" and

How to invoke pointer to member function when it's a class data member?

拜拜、爱过 提交于 2019-12-17 16:18:24
问题 struct B { void (B::*pf)(int, int); // data member B () : pf(&B::foo) {} void foo (int i, int j) { cout<<"foo(int, int)\n"; } // target method }; int main () { B obj; // how to call foo() using obj.pf ? } In above test code, pf is a data member of B . What's the grammar rule to invoke it ? It should be straight forward, but I am not getting a proper match. e.g. If I try obj.*pf(0,0); then I get: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘pf (...)’, e.g. ‘(... ->* pf)