Questions tagged [member-pointers]

82 questions
5
votes
2 answers

Is printing of a member pointer to an int defined

Suppose I have this code: #include struct Mine { int a; int b; }; int main() { int Mine::* memberPointerA = &Mine::a; int Mine::* memberPointerB = &Mine::b; std::cout << memberPointerA; std::cout << "\n"; …
DarthRubik
  • 3,927
  • 1
  • 18
  • 54
5
votes
1 answer

C++ Pointer to member of member

//#define NOT_WORKS #define HOW(X) 0 struct A { }; struct B { A a; }; struct C { B b; }; int main(int argc, char **argv) { A B::*ba = &B::a; // ba is a pointer to B::a member B C::*cb = &C::b; // cb is a pointer to…
jdavidls
  • 348
  • 1
  • 5
5
votes
1 answer

Compile error: unresolved overloaded function type

I try to compile the following with g++ 4.7.2: template struct A { struct B { T t; template T get() { return this->*M; } }; B b; T get() { return…
Paul Draper
  • 78,542
  • 46
  • 206
  • 285
4
votes
2 answers

Does Haskell have pointers/references to record members?

I can create and reference relative pointers to struct members in C++ using the ::*, .*, and ->* syntax like : char* fstab_t::*field = &fstab_t::fs_vfstype; my_fstab.*field = ... In Haskell, I can easily create temporary labels for record getters…
Jeff Burdges
  • 4,204
  • 23
  • 46
4
votes
1 answer

Dynamically allocated member pointer is not zero initialized?

Consider the following code, where T is a member pointer: #include struct Foo { double v = 0; }; int main() { using T = double Foo::*; T p1 = nullptr; T p2 = T(); T p3{}; T * pp4 = new T(); T * pp5 = new T{}; assert(p1 ==…
Sedenion
  • 5,421
  • 2
  • 14
  • 42
4
votes
1 answer

Type of pointer to member from base class

I have a problem regarding member pointers. The following code fails to compile using both Oracle Solaris Studio 12.2's CC and cygwin GCC 4.3.4 but works with Microsoft Visual C++ 2010: struct A { int x; }; struct B : public A…
Mike
  • 68
  • 4
4
votes
1 answer

Reversing pointer to data member

Hi I am trying to figure out if it is legal (by the C++) standard to compute an offset of a member of a class (in order to reverse it). class A { public: int a, b, c, d; }; template ParentClass const *…
Sergey L.
  • 21,822
  • 5
  • 49
  • 75
4
votes
2 answers

Member function wrapper with a single argument template?

I made a template function which takes a member function as a parameter. However, since the class has to be declared before it can be used as part of the member function parameter, I have to make it a separate parameter: template
stands2reason
  • 672
  • 2
  • 7
  • 18
4
votes
0 answers

Comparison of Virtual Function Pointers in C++

Say I want to check to see whether a subclass has implemented one of it's parent's virtual functions (never mind whether this smells of bad architecture... it's an exercise). If I wanted to see if two regular functions were identical, I could just…
stett
  • 1,351
  • 1
  • 11
  • 24
4
votes
1 answer

Derived class cannot use member pointer to protected base class member

include class Base { protected: int foo; int get_foo() { return foo; } }; class Derived : public Base { public: void bar() { int Base::* i = &Base::foo; this->*i = 7; printf("foo is %d\n",…
Daniel Skarbek
  • 554
  • 4
  • 14
4
votes
1 answer

C++ strict aliasing rules and pointers to members

The following code yields warning in G++: #include #include template Q T::*pointer_to(P T::*p, Q P::*q) { typedef Q T::* output_ptr; // warning: dereferencing type-punned pointer will…
4
votes
2 answers

get type of member memberpointer points to

I would like to extract the type of the member a member pointer points to. template void demo(myClass& instance, void* ptr) { instance.*member = *reinterpret_cast(ptr); // can the someType in this line be…
ted
  • 4,791
  • 5
  • 38
  • 84
3
votes
4 answers

Can a single function pointer point to multiple classes member function

Here are the requirements posed by my application. I have a class A, that accepts a function pointer say cFunc, Basically in my implementation of A, I have it call cFunc multiple times. The cFunc pointer itself should point to different functions…
Nicomoto
  • 473
  • 2
  • 7
  • 15
3
votes
1 answer

Understanding pointer to member of class of type - no polymorphism

I thought it is straightforward that a 'pointer to member of class T of type DerivedT' can be used as a 'pointer to member of class T of type BaseT' if BaseT is base of DerivedT. The analogy seems to be obvious at least to me as DerivedT* can be…
Broothy
  • 659
  • 5
  • 20
3
votes
2 answers

is there a use for &func or class::func in c++?

This seems inconsistent. Why do we use &Example::func instead of Example::func? is there a use for Example::func or &exampleFunction? it doesnt seem like we can make a reference to a function so that rules out Example::func. and i cant think of a…
user34537