Questions tagged [member-pointers]
82 questions
55
votes
4 answers
Access to protected member through member-pointer: is it a hack?
We all know members specified protected from a base class can only be accessed from a derived class own instance. This is a feature from the Standard, and this has been discussed on Stack Overflow multiple times:
Cannot access protected member of…

YSC
- 38,212
- 9
- 96
- 149
23
votes
2 answers
Why is declaration order important for passing a member function pointer as a template argument?
Look at this code:
template struct Testee {};
class Tester
{
private:
void foo() {}
public:
using type_t = Testee;
};
It successfully compiles with g++ -std=c++14 -Wall…

ikh
- 10,119
- 1
- 31
- 70
20
votes
1 answer
Member function pointer
If the following from the C++ FAQ Lite is true: "a function name decays to a pointer to the function" (as an array name decays to a pointer to its first element); why do we have to include the ampersand?
typedef int (Fred::*FredMemFn)(char x, float…

Cedric H.
- 7,980
- 10
- 55
- 82
15
votes
2 answers
Why is it disallowed to convert from VirtualBase::* to Derived::*?
Yesterday, me and my colleague weren't sure why the language forbids this conversion
struct A { int x; };
struct B : virtual A { };
int A::*p = &A::x;
int B::*pb = p;
Not even a cast helps. Why does the Standard not support converting a base…

Johannes Schaub - litb
- 496,577
- 130
- 894
- 1,212
13
votes
9 answers
Can I assign a member data pointer to a derived type?
This is probably best shown with example code. The following fails to compile with g++:
struct Base {
};
struct Derived : public Base {
};
struct Container {
Derived data_;
};
int main(void) {
Base Container::*ptr =…

preynold
- 141
- 3
13
votes
4 answers
Match type of inherited member functions
I have the following snipped of code, which does not compile.
#include
struct A {
void foo() {}
};
struct B : public A {
using A::foo;
};
template struct helper{};
int main() {
helper…

Svalorzen
- 5,353
- 3
- 30
- 54
12
votes
8 answers
Nested data member pointer - not possible?
The following reduced code sample does not do anything useful but two subsequent assignments to a data member pointer. The first assignment works, the second one gives a compiler error. Presumably because its to a nested member.
Question would be:…

Ole Dittmann
- 1,764
- 1
- 14
- 22
10
votes
2 answers
Pointer-to-member-function performs virtual dispatch?
I executed the following code.
#include
class Base
{
public:
virtual void func()
{
std::cout<<"Base func called"<

Karen Baghdasaryan
- 2,407
- 6
- 24
9
votes
3 answers
What does template mean?
I was reading this prehistoric metaprogam example to detect whether a class supports member find. (or any other member).
template
class DetectFind
{
struct Fallback { int find; };
struct Derived : T, Fallback { };
…

Eduard Rostomyan
- 7,050
- 2
- 37
- 76
8
votes
2 answers
c++ pointer to member function, replacement for __closure
Some time ago, Borland have introduced in their BCB evironment an extension to C++ language. This extension is a __closure keyword. The question is, if it is possible to implement such functionality in plain C++ or C++11? If you are not familiar…
user3303127
7
votes
4 answers
Offset of pointer to member
template ptrdiff_t foo(T U::* m)
{
// return offset
}
How I can get the offset of the field 'm' in this context? I would prefer to use am compile-time expression.
Thanks in advance for any help. Best regards

0xbadf00d
- 17,405
- 15
- 67
- 107
7
votes
3 answers
member function pointer which returns same type of member function pointer
I'd like to declare a member function pointer in C++, that returns the same member function pointer type
This doesn't work:
class MyClass {
public:
typedef FunctionPtr (MyClass::*FunctionPtr)();
}
Does someone know a solution?

Frank28
- 71
- 2
7
votes
4 answers
Can I compose pointers to member
I'd like to compose member pointers. Basically I have a main class with different member. How do I create a member pointer for the main class that would point to a member of a member of that class. I hope the code below is explains what I'm trying…

0x26res
- 11,925
- 11
- 54
- 108
7
votes
4 answers
Is there anyway in C++11 to get member pointer type within a template?
I know this was not possible in C++03, but I'm hoping there is some new voodoo to allow me to do this. See below:
template
struct Binder
{
template
void AddMatch();
};
struct TestType
{
int…

Jaime
- 1,182
- 2
- 12
- 29
6
votes
3 answers
Function member pointer with private base
The following code yields a compile time error:
'base::print' : cannot access private member declared in class 'base_der'
However, I have made the member public in the derived class. Why doesn't this work?
#include
using namespace…

SerbanLupu
- 427
- 2
- 5
- 9