member-functions

Why can't I call a non-const member function on an element in an std::set?

梦想的初衷 提交于 2019-12-11 06:58:14
问题 I hve the next code: set<Item> temp_items; set < Item >::iterator it; temp_items = user->second.get_items(); for (it = temp_items.begin(); it != temp_items.end(); it++) { if (counter == (choice - 1)) { it->get_count(); } } The item function i trying to call is: int Item::get_count() { return _count; } I don't have in here any const type that should prevent me from accessing the item object, and still, I get the next message: the object has type qualifiers that are not compatible with the

C++ lists and pointers

蹲街弑〆低调 提交于 2019-12-10 21:34:41
问题 I am working on homework and wanted to know what this is actually defined as: list < NAME > * m_ofList Where name comes from a struct like so: typedef struct name { int age; int height; } NAME; I want to know what it is so I know how to insert to it or access it: push_back , insert , etc . So I understand this now, but I am stuck because of some type of memory access: it produces a segmentation fault and I have been unable to figure this out. Where do I need to initialize my new list? it

CPP templated member function specialization

本小妞迷上赌 提交于 2019-12-10 19:42:22
问题 I'm trying to specialize the member function moment() only (not the hole class) like this: template<class Derived, class T> class AbstractWavelet { public: [...] template<bool useCache> const typename T::scalar moment(const int i, const int j) const { return abstractWaveletSpecialization<Derived, T, useCache>::moment(static_cast<const Derived*>(this), i, j); } template<bool useCache> friend const typename T::scalar abstractWaveletSpecialization<Derived, T, useCache>::moment(const Derived*

Passing static method as argument, no address-of operator required?

 ̄綄美尐妖づ 提交于 2019-12-10 19:07:13
问题 class ThreadWorker { public: ThreadWorker(void); virtual ~ThreadWorker(void); static void DoSomething(); }; int main() { boost::thread thread1(ThreadWorker::DoSomething); boost::thread thread2(ThreadWorker::DoSomething); boost::thread thread3(&ThreadWorker::DoSomething); } I'm playing around with Boost.Thread and I notice it doesn't seem to matter whether I use the address of operator (&) or not when passing a static member function as an argument. Does it not matter? And if not, why? Is one

Is there anyway to use a member function as a default parameter?

空扰寡人 提交于 2019-12-10 15:35:17
问题 It tried something like this, which doesn't work. Is there a way to get a similar effect? class A { public: int foo(); void bar(int b = foo()); }; 回答1: Yes. Overload the function and call the member-function in it. void bar() { bar(foo()); } 来源: https://stackoverflow.com/questions/4901233/is-there-anyway-to-use-a-member-function-as-a-default-parameter

How to call a template member function in a template base class?

别说谁变了你拦得住时间么 提交于 2019-12-10 14:23:44
问题 When calling a non-templated member function in a base class one can import its name with using into the derived class and then use it. Is this also possible for template member functions in a base class? Just with using it does not work (with g++-snapshot-20110219 -std=c++0x): template <typename T> struct A { template <typename T2> void f() { } }; template <typename T> struct B : A<T> { using A<T>::f; template <typename T2> void g() { // g++ throws an error for the following line: expected

Pointer to member function syntax

北慕城南 提交于 2019-12-10 09:46:09
问题 I'm trying to wrap my head around pointers to member functions and am stuck with this example: #include <iostream> class TestClass { public: void TestMethod(int, int) const; }; void TestClass::TestMethod(int x, int y) const { std::cout << x << " + " << y << " = " << x + y << std::endl; } int main() { void (TestClass::*func)(int, int) const; func = TestClass::TestMethod; TestClass tc; tc.func(10, 20); return 0; } What I think the code should do: In the first line of main I declare a pointer to

static member function with C language binding?

对着背影说爱祢 提交于 2019-12-09 16:09:40
问题 The following C++ code compiles with Visual C++ and g++: struct S { static void foo(); }; extern "C" void S::foo() {} struct T { static void foo(); }; extern "C" void T::foo() {} auto main() -> int { S().foo(); T().foo(); } Is it valid? If it's valid, since the implementation may be in a separate translation unit, does that imply that a static member function always has the same calling convention as a C function (and if not, how does it not imply that)? 回答1: C++11 7.5/4 "Linkage

What's the best way to sum the result of a member function for all elements in a container?

寵の児 提交于 2019-12-09 02:37:04
问题 Let's say I have the following object: struct Foo { int size() { return 2; } }; What's the best way (most maintainable, readable, etc.) to get the total size of all objects in a vector<Foo> ? I'll post my solution but I'm interested in better ideas. Update: So far we have: std::accumulate and a functor std::accumulate and a lambda expression plain ol' for-loop Are there any other workable solutions? Can you make something maintainable using boost::bind or std::bind1st/2nd ? 回答1: In addition

struct with member function as parameter

拟墨画扇 提交于 2019-12-09 02:09:14
问题 I am a beginner in C++ and stack exchange. I am working on an Interface class that gets keyboard input and checks to see whether it is correct through looping through an array of structs which contains strings to compare to and strings to output depending if it is equal to the compare string or not. If the input is correct, it will print the string within the struct and a function within the structure is called and does some action. interface.hpp #include <string> class Input_Interface {