Questions tagged [member-pointers]
82 questions
3
votes
0 answers
How to allow use of private member pointer as template parameter?
I have a class (User in the example below) that derives from a base class (Base), passing a third class type (Used) and a member pointer within that class as template parameters to the base class.
What do I need to do within Used (i.e., what do I…

zennehoy
- 6,405
- 28
- 55
3
votes
1 answer
Call function through pointer to class member
In following code:
class foo
{
public:
void foo_function() {};
};
class bar
{
public:
foo foo_member;
void bar_function(foo bar::*p_foo)
{
// what is the corrct sintax for following:
this->*p_foo->foo_function(); //…

codekiddy
- 5,897
- 9
- 50
- 80
3
votes
1 answer
Is it standard C++ to assign a member pointer to the address of another member in the constructor initializer?
Does this conform to the standard?
class Foo {
Bar m_bar;
Bar * m_woo;
public:
Foo() : m_bar(42, 123), m_woo(&m_bar) { }
};

Nick Strupat
- 4,928
- 4
- 44
- 56
3
votes
1 answer
C++ conversion: have pointer to object member, calculate pointer to object
C++ has static_cast to convert base_class_pointer to derived_class_pointer.
It is very similar operation to convert object_data_member_pointer to object_pointer.
I wrote the function ConvertDataMemberPtrToObjectPtr using unsafe C type conversion.…

stan5
- 81
- 1
- 4
3
votes
0 answers
C++ constexpr member pointers in classes
I'm trying to use a class to group all the parameters of a template data structure, in particular an intrusive AVL tree. The user would do something like this:
struct MyEntry {
MyEntry *parent;
MyEntry *child[2];
int balance;
int…

Ambroz Bizjak
- 7,809
- 1
- 38
- 49
2
votes
2 answers
Odd syntax: asterisk after scope operator (::)?
Help me understand the following code snippet:
(foo.h)
class Foo
{
public:
typedef void (MyType::*Handler)(SomeOtherType* t);
Foo(Handler handler) : handler_(handler) { }
private:
Handler…

Daniel
- 23,365
- 10
- 36
- 34
2
votes
0 answers
Is it legal to cast between member pointers of unrelated base classes?
Given a struct Derived: Base, Other, is it legal to cast T Other::* -> T Derived::* -> T Base::* and dereference it on a Base* that points to a Derived?
I know that the two casts required to do this are valid individually: I can go from a T Other::*…

Jonathan S.
- 1,796
- 5
- 14
2
votes
2 answers
Create std::function which returns variant with a value of a function member. Segmentation fault
I am writing a wrapper class to wrap custom struct member variables into variants.
Given a reference to a struct object, std::function must return a variant, that contains a value of a specific member - with which std::function was initially…

Sergey Kolesnik
- 3,009
- 1
- 8
- 28
2
votes
2 answers
How to get object type of pointer to non-static data member at compile time?
Suppose we have a simple data class like this:
struct DataObj
{
char member[32];
}
And the type of pointer to the member in the data object:
typedef decltype(&DataObj::member) memberObjPtr;
How can I deduce the type of the member variable the…

robo
- 93
- 7
2
votes
2 answers
Comparing memberpointers
I read about the builtin comparison operators. I wondered why there are no ordering operators(<, <=, >, >=) for member pointers. It is valid to compare the adresses of two members of an instantiation of a…

cmdLP
- 1,658
- 9
- 19
2
votes
0 answers
downcasted pointer to member as template argument
I'm working in some code using the Curiously Recurring Template Pattern, where the derived class is passed up to specialize its own base classes at compile time.
With this, I run into an annoying problem which I have made a toy example for. I have…

Brandon
- 43
- 4
2
votes
4 answers
How to cast member variable pointer to generic type in C++
I have code similar to this in my application:
class A
{
public: int b;
}
class C
{
public: int d;
}
void DoThings (void *arg1, MYSTERYTYPE arg2);
A obj_a;
C obj_c;
DoThings(&obj_a, &A::b);
DoThings(&obj_c, &C::d);
The question is - What…

zaratustra
- 8,148
- 8
- 36
- 42
2
votes
1 answer
Why can't one use scope resolution with member pointer dereference?
Consider a simple example:
struct FooParent {
virtual void bar() { }
};
struct Foo: FooParent {
void bar() { }
};
int main() {
Foo foo;
void (Foo::*foo_member)() = &FooParent::bar;
//(foo.*FooParent::foo_member)();
…

W.F.
- 13,888
- 2
- 34
- 81
2
votes
0 answers
syntax regarding static const-pointer to data member
Suppose there is a class with a few data members, such as this:
struct s {
char c;
int i;
};
If I need a const-pointer to a member, it's simple enough:
auto s::* const ptr = &s::c;
If I try to create a static member of the same type, some…

Xeren Narcy
- 875
- 5
- 15
2
votes
3 answers
Get pointer to enclosing instance from member pointer
I have a custom class with some data members. I've got a pointer to one of the class' data member, and I would like to have a pointer to its enclosing instance. For example:
class MyClass{
public:
int a;
int b;
virtual…

Sipka
- 2,291
- 2
- 27
- 30