Questions tagged [member-pointers]

82 questions
2
votes
1 answer

Pointer to class member as template parameter (with type of the following class)

I'm trying to define an internal list as template class that has a type safe container_of member function. For that the template must include the type of the container and the offset where in the container the list can be found (a member pointer).…
Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
2
votes
1 answer

Why is it not allowed to cast Derived T::* to Base T::*?

Background: Many functional languages support algebraic data types, which can to a degree be emulated with virtual functions and inheritance. The most obvious solution involves a heap allocation, since the derived types come in different sizes.…
2
votes
2 answers

c++ template and inheritance with method pointer

if I declare: class Avoidance : public Schema{ and I try to void*(Schema::*pt)(); pt=&Avoidance::frontBusy; compiler report me error: cannot convert ‘void* (Avoidance::*)()’…
volperossa
  • 1,339
  • 20
  • 33
2
votes
1 answer

Check for equality of derived template classes in the base class

This is my first question on this platform. I am sorry if something is unclear or if I failed to ask in an appropriate way. The code below should compile with any C++11 Compiler. I tried to reduce it to the minimum. The sense of this code might got…
1
vote
2 answers

is a pointer to data member its offset?

Am I safe to assume that the offset of a data member (offsetof(mystruct, myfield)) is numerically equal to the raw value of a member pointer retrieved with &mystruct::myfield, or is it implementation dependent?
Lorenzo Pistone
  • 5,028
  • 3
  • 34
  • 65
1
vote
1 answer

Type of member pointer not recognised as

The following code example tries to extract the value types of member pointer of a struct foo previously fed into a function serialize(). This maybe needs further explanation: I want to eventually iterate through each member of the struct foo and…
glades
  • 3,778
  • 1
  • 12
  • 34
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
0 answers

Assign an object's pointer-to-function property in generic function in Delphi - why does this code work?

I was not programming in Delphi Pascal for over 20 years. The current challenge is to wrap a C-shared library API in Pascal in more or less OOP way. The pattern I used is lazy binding at runtime, i.e. loading a library and linking to the functions…
1
vote
0 answers

Saving & loading paired objects to/from data files

I have two classes, A and B, whose member object data will be saved in two text files, A_file and B_file. Class A includes class B. (Specifically, class A contains a pointer to a class B object.) I am wondering how to save & load data to & from the…
LouisInLA
  • 31
  • 4
1
vote
1 answer

Calling derived class's methods from pointer to base class via reinterpret_casting the method pointer. Is this UB?

With a pointer to an object of a derived type assigned to a pointer of its base class, I've found that you can reinterpet_cast a method from the derived class to a pointer of the base class, even if the base class doesn't have any such function…
Brandon
  • 483
  • 3
  • 12
1
vote
2 answers

What is the meaning of template arguments of this type - `ClassA T::*ELEM`?

I came across this code recently, in the context of intrusive lists: template struct Node{ T *next; T *prev; Node(): next(nullptr), prev(nullptr){} }; /* * Intrusive doubly-linked-list * * */ template
aspen100
  • 965
  • 11
  • 22
1
vote
1 answer

Benefit of copying pointer class arguments to class members?

I have a player class that uses my DirectX Graphics class to create surfaces and draw sprites. I am passing the graphics class as a pointer to the player class methodlike this: **game.cpp** m_pPlayer->Init(Graphics* graphics); Inside the player…
ball
  • 39
  • 7
1
vote
2 answers

Member Variable Pointer

For a given struct: struct foo { void fooFunc(){} int fooVar = 0; }; I can create a call wapper to the function: std::mem_fn( &foo::fooFunc ) such that I can pass it into another method and call it on an object. I want to know if there is a…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
1
vote
2 answers

Joining a pointer and member pointer into a function pointer

Please consider such code: class MyClass{ public: void MyFunc(int x){ std::cout << x << std::endl; } }; int main(){ MyClass* class_ptr = new MyClass(); void (Myclass::*member_func_ptr)(int) = &MyClass::MyFunc; // This…
rafalcieslak
  • 915
  • 1
  • 12
  • 25
1
vote
2 answers

Passing a pointer to member function

There are a number of examples out there but yet I can't seem to figure out the solution to my problem. I have class FooSource{ ... void StartGetFoos(void (*callback)(vector*, IAsyncResult)); ... } When StartGetFoos() is called,…
Professor Chaos
  • 8,850
  • 8
  • 38
  • 54