member-functions

Passing a C++ Member Function Pointer to an STL Algorithm

别等时光非礼了梦想. 提交于 2019-12-01 18:36:23
问题 I have a member function as follows: class XYZ{ public: float function(float x); private: float m_DensityMin; float m_DensityMax; }; Now, I'm trying to transform a std::vector<float> foo using the std::transform STL algorithm by passing the member function function , and storing the resulting values in a vector bar . If I use the function as a global function, with random data that should denote the member variables of the class, it works fine. However, as the function requires the use of

Reasons for defining non-const 'get' member functions?

房东的猫 提交于 2019-12-01 14:57:52
I'm working on learning C++ with Stroustrup's (Programming Principles & Practice Using C++) book. In an exercise we define a simple struct: template<typename T> struct S { explicit S(T v):val{v} { }; T& get(); const T& get() const; void set(T v); void read_val(T& v); T& operator=(const T& t); // deep copy assignment private: T val; }; We're then asked to define a const and a non-const member function to get val . I was wondering: Is there any case where it makes sense to have non-const get function that returns val ? It seems much cleaner to me that we can't change the value in such situations

Reasons for defining non-const 'get' member functions?

孤人 提交于 2019-12-01 13:44:36
问题 I'm working on learning C++ with Stroustrup's (Programming Principles & Practice Using C++) book. In an exercise we define a simple struct: template<typename T> struct S { explicit S(T v):val{v} { }; T& get(); const T& get() const; void set(T v); void read_val(T& v); T& operator=(const T& t); // deep copy assignment private: T val; }; We're then asked to define a const and a non-const member function to get val . I was wondering: Is there any case where it makes sense to have non-const get

Why member functions can't be used as template arguments?

你离开我真会死。 提交于 2019-12-01 04:02:07
Why member functions cannot be used as template arguments? For example, I want to do like: struct Foo { void Bar() { // do something } }; template <typename TOwner, void(&func)()> void Call(TOwner *p) { p->func(); } int main() { Foo a; Call<Foo, Foo::Bar>(&a); return 0; } I know that a similar thing can be done using pointers-to-member; well, it's cool enough most of the time, but I'm just curious about why pointers "should" be used. I see no ambiguity of interpreting "p->func()" above. Why the standard prohibits us to use member functions as template arguments? Even static member functions

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

爱⌒轻易说出口 提交于 2019-12-01 03:27:22
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 ? In addition to your own suggestion, if your compiler supports C++0x lambda expressions, you can use this shorter version:

struct with member function as parameter

馋奶兔 提交于 2019-12-01 01:31:02
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 { struct command_output { std::string command; std::string success_string; std::string failure_string; void

How to tell if class contains a certain member function in compile time [duplicate]

▼魔方 西西 提交于 2019-11-30 22:50:44
Possible Duplicate: Is it possible to write a C++ template to check for a function's existence? say there are 2 classes: struct A{ int GetInt(){ return 10; } }; struct B{ int m; }; I want to use object of type A or B in following function tempate< typename T > int GetInt( const T & t ) { //if it's A, I'll call: return t.GetInt(); //if its' B, I'll call: return t.m; } Now, because there are whole bunch of classes, some contain GetInt(), some don't, I don't want to write specialization for each type, I only want to distinguish them by ' containing GetInt() or not in compile time ', how should I

How to use variadic templates to make a generic Lua function wrapper?

我怕爱的太早我们不能终老 提交于 2019-11-30 07:35:32
For my current project, I've been writing a lot of C/C++ to Lua wrappers. A large number of these are simple setters and getters, so I managed to write some templates that make it easy to generate these, like so: // Class Return Field template <typename T, typename U, U T::*Member> int luaU_get(lua_State* L) { T* obj = luaW_check<T>(L, 1); luaU_push<U>(L, obj->*Member); return 1; } static luaL_reg Foo_Table[] = { ... // Now I can just use this generic template to avoid // writing simple getter functions { "getbar", luaU_get<Foo, Bar, &Foo::bar> }, ... }; I would like to do something similar

C++ pointer-to-method template deduction doesn't compile when targeting x86, but works with x64

眉间皱痕 提交于 2019-11-30 04:44:14
问题 I've got this sample code: struct A { int foo() { return 27; } }; template<typename T> struct Gobstopper { }; template<> struct Gobstopper<int(void)> { Gobstopper(int, int) { } // To differentiate from general Gobstopper template }; template<typename ClassType, typename Signature> void DeduceMethodSignature(Signature ClassType::* method, ClassType& instance) { // If Signature is int(), Gobstopper<> should resolve to the specialized one. // But it only does on x64! Gobstopper<Signature>(1, 2);

Why is a public const method not called when the non-const one is private?

泪湿孤枕 提交于 2019-11-30 02:32:35
Consider this code: struct A { void foo() const { std::cout << "const" << std::endl; } private: void foo() { std::cout << "non - const" << std::endl; } }; int main() { A a; a.foo(); } The compiler error is: error: 'void A::foo()' is private`. But when I delete the private one it just works. Why is the public const method not called when the non-const one is private? In other words, why does overload resolution come before access control? This is strange. Do you think it is consistent? My code works and then I add a method, and my working code does not compile at all. NathanOliver When you call