member

What is the simplest way to “cast” a member function pointer to a function pointer in C++?

匆匆过客 提交于 2019-12-10 16:13:30
问题 I want to provide a member function for the "comp" parameter of an STL algorithm like lower_bound( ..., Compare comp ). The comp() function accesses a non-static member field so it must itself be a non-static member but the type of a non-static member function pointer is different from that of an ordinary function pointer. What is the best way around this problem? 回答1: This is the most common use of std::mem_fun and std::mem_fun_ref . They're templates that create functors that invoke the

pthread in a class

谁说我不能喝 提交于 2019-12-10 14:47:54
问题 Hey everyone, considering the following code (compiled with g++ -lpthread thread_test.cpp ) how can I know what number thread I am in from inside "thread_function"? And let me know if you have any other suggestions. Thanks! thread_test.cpp: #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> class A { public: A(); void run(); private: static void* thread_function( void *ptr ); pthread_t m_thread1, m_thread2; static int m_global; }; int A::m_global = 0; A::A() { int

Does struct with a single member have the same performance as a member type?

拥有回忆 提交于 2019-12-10 14:46:02
问题 Does struct with a single member have the same performance as a member type (memory usage and speed)? Example : This code is a struct with a single member: struct my_int { int value; }; is the performance of my_int same as int ? 回答1: Agree with @harper overall, but watch out for the following: A classic difference is seen with a "unstructured" array and an array in a structure. char s1[1000]; // vs typedef struct { char s2[1000]; } s_T; s_T s3; When calling functions ... void f1(char s[1000])

Copy constructor for object with vector fo object as member variable

删除回忆录丶 提交于 2019-12-10 11:49:10
问题 I have a class A with a vector<Object> object_list_ as a member variable. I know that if I had a vector of pointers I would have needed to write a specific copy constructor to achieve a deep copy. However, in this case, as I don't have pointers, when copying an object of type A , the default copy constructor will automatically also call the copy constructors of Object or each element in object_list_ or do I have to write my own copy constructor to do that? 回答1: The default copy constructor

How can references to private class members be dangerouse

邮差的信 提交于 2019-12-10 11:19:54
问题 I am currently reading the CERT Secure Coding Standard for Java. I am struggling with this rule. The severity of this rule is high, which means a violation of this rule can lead to a privilege escalation or execution of code. I don't really understand how a violation of this rule can lead to such fatal things. Can somebody make an example of an attack on a code which violates this rule? 回答1: You might have an invariant which you establish in the constructor of the class, e.g. that date

container_of的函数用法总结

老子叫甜甜 提交于 2019-12-10 04:53:47
首先看看函数的原型: #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) 其原理是利用,结构体中的一个成员变量的地址获得该结构体的首地址。 其中: #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 注: typeof 关键字是用来定义变量数据类型的。 如: 把y定义成x指向的数据类型: typeof(*x) y; 解析如下: const typeof ( ( ( type * ) 0 ) -> member) * __mptr =(ptr) ; //定义 一个(type *)0->member 类型的指针变量 __mptr 并且将 ptr 赋给 __mptr ;也就是临时存放 ptr 的地址(这里的还有一个作用,可以检查ptr的类型是否与member相同)。 (type *)( ( char * ) __mptr - offsetof ( type, member ) ) ;//该结构体成员变量的地址减去该结构体成员变量地址的偏移量

std::all_of not accepting class member function as function with 1 argument

…衆ロ難τιáo~ 提交于 2019-12-09 23:55:32
问题 I can't figure what I am doing wrong with this std::all_of call. I have a class Statistics: class Statistics { public: bool isDataSet() const { return m_data.size() > 0; } private: std::vector<double> m_data; }; Each instance of Statistics class corresponds to a certain object. In another function in a different file I want to display statistics only if data has been initialized in all Statistics instances. I want to use std::all_of function in the following manner: if( std::all_of(m_stats

Accessors vs. public members

天大地大妈咪最大 提交于 2019-12-09 18:20:50
问题 I have a class with a lot of built-in type members with read/write access. Should I make them public members and provide get/set methods for each one? How about structures? 回答1: If there are invariants you need to preserve, then yes. Otherwise, don't bother. 回答2: The whole reason to have accessors (getters) and modifiers (setters) is to provide yourself with an extra level of indirection. This extra level of indirection allows you to provide a read only view of your variable to a public

New pc causing “namespace of type specified in the imports doesn't contain any public member” in VB.NET

删除回忆录丶 提交于 2019-12-08 19:58:57
问题 I just got a new PC (Win 7) with VS 2010 (same version as my old PC). I got a VB.NET solution from source control that contains two projects. One of the projects builds fine. The other project flags every non-MS Imports statement with: Namespace or type specified in the Imports &1 doesn't contain any public members or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases. The ironic

C++ template static member instantiation

佐手、 提交于 2019-12-08 14:47:19
问题 #include <map> #include <iostream> template <typename T> class A { static std::map<int, int> data; public: A() { std::cout << data.size() << std::endl; data[3] = 4; } }; template <typename T> std::map<int, int> A<T>::data; //std::map<int, int> A<char>::data; A<char> a; int main() { return 0; } What is wrong with this? Without explicit instantiation it breaks at data[3] = 4; Explicit instantiation solves the problem but the program breaks after std::cout << data.size() << std::endl; what means