pointer-to-member

How to declare a member function const pointer while point to const address

廉价感情. 提交于 2019-12-11 11:28:45
问题 What I mean is something like const char* const p; or char const * const p; . The p here stand for a pointer points to a const char while the pointer itself is also const. So *p = 'a'; or char c = 'c'; p = &c; won't be complied. Please someone tell me how to declare a pointer points to a member function, both what it points to and itself is const, with and without typedef . Not use in practice just curious about. This is not what I'm asking about. 回答1: Member function pointers can't be

Printing a pointer-to-member-field

折月煮酒 提交于 2019-12-11 10:53:22
问题 I was debugging some code involving pointers to member fields, and i decided to print them out to see their values. I had a function returning a pointer to member: #include <stdio.h> struct test {int x, y, z;}; typedef int test::*ptr_to_member; ptr_to_member select(int what) { switch (what) { case 0: return &test::x; case 1: return &test::y; case 2: return &test::z; default: return NULL; } } I tried using cout : #include <iostream> int main() { std::cout << select(0) << " and " << select(3) <

Is there pointer to member traits or something like this?

六月ゝ 毕业季﹏ 提交于 2019-12-09 13:17:41
问题 Based on other my question. Consider the following code template<typename T, int N> struct A { typedef T value_type; // save T to value_type static const int size = N; // save N to size }; Look, I can use value_type and size as template parameter. typedef A<int, 2> A1; typedef A<A1::value_type, A1::size + 3> A2; // OK, A2 is A<int,5> Now I want to do the same with pointer to member: struct Foo { int m; int r; }; template<int Foo::*Mem> struct B { static int Foo::* const mp; }; template<int

Passing pointer to 2D array c++

狂风中的少年 提交于 2019-12-09 11:56:46
问题 I'm having this problem for quite a long time - I have fixed sized 2D array as a class member. class myClass { public: void getpointeM(...??????...); double * retpointM(); private: double M[3][3]; }; int main() { myClass moo; double *A[3][3]; moo.getpointM( A ); ??? A = moo.retpointM(); ??? } I'd like to pass pointer to M matrix outside. It's probably very simple, but I just can't find the proper combination of & and * etc. Thanks for help. 回答1: double *A[3][3]; is a 2-dimensional array of

C++ Member Function Pointers

孤人 提交于 2019-12-07 17:18:41
问题 I'm doing a little game in C++ and I'm discovering the class members function pointers. I don't have any idea to make them work in the right way, but here is my attempt. // A struct where the function pointer will be stored for the call // By the way, is there a way to do the same thing with classes ? // or are structs still fine in C++ ? (Feels like using char instead of string) typedef struct s_dEntitySpawn { std::string name; void (dEntity::*ptr)(); } t_dEntitySpawn; // Filling the struct,

pointer to member function of incomplete type

人盡茶涼 提交于 2019-12-06 20:13:00
问题 I don't understand why adding a forward declaration for a class changes a size of its pointer to member type #include <iostream> using namespace std; int main() { //struct CL; //cout<<sizeof(int (CL::*)())<<endl; struct CL{}; cout<<sizeof(int (CL::*)())<<endl; } output VS2013: 4 But if I uncomment the first two lines in main(), then the output is different: 16 16 So, only a simple adding a forward declaration before a definition of struct CL increases a size of a pointer to member of CL. Why?

Mapping from pointers-to-member

*爱你&永不变心* 提交于 2019-12-06 08:29:55
问题 (Note: in case this feels like an X-Y problem, scroll below the separator for how I arrived at this question) I am looking for a way to store pointers-to-member-functions (of different types) and compare them for equality. I need to store a mapping from pointer-to-member-function to an arbitrary object, and then search this mapping. It doesn't have to be an associative container, a linear search is fine. Also note that the pointers serve as mapping keys only, they are never dereferenced. My

C++ Member Function Pointers

折月煮酒 提交于 2019-12-06 04:23:14
I'm doing a little game in C++ and I'm discovering the class members function pointers. I don't have any idea to make them work in the right way, but here is my attempt. // A struct where the function pointer will be stored for the call // By the way, is there a way to do the same thing with classes ? // or are structs still fine in C++ ? (Feels like using char instead of string) typedef struct s_dEntitySpawn { std::string name; void (dEntity::*ptr)(); } t_dEntitySpawn; // Filling the struct, if the entity's classname is "actor_basicnpc", // then I would like to do a call like ent-

How Does Getting the Address of a Class Member Through a Scope Resolution Operator Work When Using a Pointer-to-Member?

岁酱吖の 提交于 2019-12-06 00:30:21
When using a pointer-to-member (AKA dot-star or arrow-star) to access a class member, we can use the following syntax: A * pa; int A::*ptm2 = &A::n; std::cout << "pa->*ptm: " << pa->*ptm << '\n'; My question is how does the &A::n statement work? In the above example n is a variable. If instead of a member variable, n was a function (and we defined a pointer-to-a-member-function instead of a pointer-to-a-member), I might reason that since a class's functions can be effectively static (see Nemo's comment), we could find a class's function's address via &A::some_function . But how can we get the

Accessing function pointer inside class

你。 提交于 2019-12-05 18:14:07
I am defining function pointer inside a class and trying to access it through an instance of the class but it shows an error. Here is the code: 1 #include<stdio.h> 2 3 class pointer { 4 public: 5 int (pointer::*funcPtr)(int); 6 pointer() { 7 funcPtr = &pointer::check; 8 } 9 10 11 int check(int a) 12 { 13 return 0; 14 } 15 16 }; 17 18 int main() 19 { 20 pointer *pt=new pointer; 21 return (pt->*funcPtr)(3); 22 } It shows a compile time error: checkPointer.cpp:21:15: error: ‘funcPtr’ was not declared in this scope please help me. Thank You in advance. The issue here is that funcPtr is declared