Questions tagged [member-pointers]
71 questions
52
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…
![](../../users/profiles/5470596.webp)
YSC
- 34,418
- 7
- 80
- 129
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…
![](../../users/profiles/2729109.webp)
ikh
- 9,139
- 1
- 28
- 64
19
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…
![](../../users/profiles/420892.webp)
Cedric H.
- 7,040
- 10
- 45
- 76
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…
![](../../users/profiles/34509.webp)
Johannes Schaub - litb
- 466,055
- 116
- 851
- 1,175
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 =…
![](../../users/profiles/755694.webp)
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…
![](../../users/profiles/1356926.webp)
Svalorzen
- 4,961
- 3
- 25
- 50
11
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:…
![](../../users/profiles/414934.webp)
Ole Dittmann
- 1,654
- 1
- 12
- 20
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?
![](../../users/profiles/382375.webp)
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…
![](../../users/profiles/109525.webp)
0x26res
- 7,536
- 9
- 48
- 90
6
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…
![](../../users/profiles/580590.webp)
Jaime
- 1,052
- 2
- 12
- 26
5
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…
![](../../users/profiles/639734.webp)
SerbanLupu
- 417
- 2
- 5
- 8
5
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
![](../../users/profiles/547231.webp)
0xbadf00d
- 14,584
- 15
- 60
- 93
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";
…
![](../../users/profiles/6149078.webp)
DarthRubik
- 3,657
- 14
- 49
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…
![](../../users/profiles/1850574.webp)
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…
![](../../users/profiles/1212596.webp)
Paul Draper
- 64,883
- 37
- 172
- 246