Questions tagged [data-member-pointers]
9 questions
6
votes
1 answer
Data member pointers as associative container keys
I am trying to create an std::set of pointers to data members. However, I can't find a method to sort or hash such pointers.
They can't be compared with operator<, they don't seem to be supported by std::less and there is no standard integer type…

François Andrieux
- 28,148
- 6
- 56
- 87
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
3
votes
5 answers
Use struct member pointer to fill-in a struct in C++
So I have the following available:
struct data_t {
char field1[10];
char field2[20];
char field3[30];
};
const char *getData(const char *key);
const char *field_keys[] = { "key1", "key2", "key3" };
This code is given to my and I cannot…

José D.
- 4,175
- 7
- 28
- 47
1
vote
1 answer
Is there a way to get pointer-to-member without mentioning the class name if it is used within the context of the class
The code below gives the following compilation error:
main.cpp:5:48: error: invalid use of non-static data member 'id'
static constexpr int Type::* mem_ptr_id = &id;
^~
main.cpp:5:34: error: default…

Alex Tsvetanov
- 11
- 4
1
vote
1 answer
How to get a member pointer from a class pointer and plain pointer to member in C++?
I can get plain pointer to a member of an object instance like in the example. How can I do it in the reverse direction?
struct S {
int i;
};
// We have a pointer to an instance of S and an int member pointer in S.
// This function returns a…

Gábor
- 71
- 2
1
vote
2 answers
auto template parameter, data member and constness
Suppose I have a pointer to data member and I want to know if it's const or not. In other words:
struct S {
const int i; // this is const
int j;
};
In C++ I used to do something like this:
template

skypjack
- 49,335
- 19
- 95
- 187
1
vote
0 answers
Should member pointers always use new in their initialization?
In this question, I get weird segmentation fault in a method the constructor invokes, but apparently nowhere else (not even in a CodeBreaker::testVectorOfElements called from main() whose definition looks like:
void…

Mike Warren
- 3,796
- 5
- 47
- 99
1
vote
1 answer
Variadic list of pointers to data members
Consider this working code. The function searchByDataMember uses a pointer to data member as argument to search a value among a container.
#include
#include
-
#include
- 4,817
- 3
- 20
- 43

prestokeys
0
votes
0 answers
C++ Best way to have class member which is a pointer (or reference) to another class and can handle both const and non-const situations
Consider the following sample code. It compiles and works as expected. However, if I add "const" to the beginning of the first line of main function, it will not compile, because B class takes a pointer to A, and with const being added, we will have…

KMot
- 467
- 5
- 13