1,037 questions
1
vote
1
answer
44
views
How to pass a pointer to member function to a template function
Here is my simplified code: S.h
class SBase {
...
public:
virtual std::vector<int32_t> getNumbersSet1();
virtual std::vector<int32_t> getNumbersSet2();
}
class SDerived : public ...
14
votes
2
answers
805
views
MSVC calls wrong virtual method when storing a pointer to virtual method in a static inline variable
I'm encountering unexpected behavior on MSVC when storing a pointer to a virtual method in a static inline variable. The issue does not occur on GCC or Clang.
Specifically, when I store a pointer to a ...
3
votes
1
answer
78
views
Can't call stored member function on stored object
I'm trying to create sort of an event system, I don't care if it is suboptimal, I'm doing it for fun. I do not want alternate solutions, I want to understand why this isn't working and how could I fix ...
-1
votes
2
answers
220
views
Why ISO C++ forbid taking the address of a bound member function to form a pointer to member function?
Here is the error in GCC (which i have already fixed):
ISO C++ forbid taking the address of a bound member function to form a pointer to member function
This is the error line of my code:
threads[i] ...
8
votes
2
answers
196
views
Get pointer to derived class member made public with using declaration
Consider a base class with a couple of public fields and a derived class which inherits the base privately and makes one of the inherited fields public via the using declaration. I need to access this ...
-1
votes
1
answer
116
views
C++ Reference is not updating when referenced-variable is [duplicate]
I recreated small example of a problem I'm seeing.
I have two classes. Class Owner has a member variable, a. Class Client has a member variable which references the member variable a from the Owner ...
0
votes
0
answers
48
views
deduct holder's class of c++ 'pointer to member' of derived class
I think this question is related to C++ pointer to member of derived class, but slightly different.
I have a class accepts a pointer-to-member as template parameter, and will deduct the holder's type ...
0
votes
2
answers
101
views
How to use a pointer to a member function as a field of a struct data member to call a member function? [duplicate]
I am trying to port C code to a C++ program and thus encapsulate the logic from the C program inside a C++ class. The step I am stuck at is using a function pointer, which is a field inside a struct ...
0
votes
1
answer
88
views
Can a pointer to member function be used directly with reinterpret_cast?
A pointer to member function isn't a regular pointer.
but a pointer to a pointer to member function does.
struct X {
void func() {}
};
int main() {
auto p1 = &X::func;
auto p2 = &...
1
vote
1
answer
58
views
How to send a pointer of a method of an object into a function?
I'm trying to create a notebook program
cpp file:
#include <string>
#include "Menu.h"
class Notebook
{
std::vector<std::string> m_notes;
public:
void addNote()
{
...
1
vote
1
answer
127
views
Why do we need to refer to the object to access a pointer to a member function?
class Harl {
private:
void debug( void );
void info( void );
void warning( void );
void error( void );
public:
void complain( std::string level );
};
...
1
vote
0
answers
55
views
Assign member function to base class function pointer [duplicate]
I have to implement a low-level interface for the library which exposes structure with a function pointers like this:
struct Interface {
int (*IterfaceFn) (int value);
};
Library then expects a ...
0
votes
1
answer
75
views
How to choose const method overload when casting of method pointer is made [duplicate]
I have a class with overloaded method:
#include <iostream>
#include <utility>
struct Overloads {
void foo() {
std::cout << "foo" << std::endl;
}
...
2
votes
1
answer
84
views
Member pointer template parameter pointing into the object being templated
I would like to have a base class that I derive from that offers serialization mechanism:
template <typename Type, typename Class, Type Class::* Member>
class Serializable
{
public:
void ...
0
votes
3
answers
173
views
Mutual conversion of pointers to a structure and its first member, and strict aliasing in C
I have structure
typedef struct {
int i;
float f;
} S;
The rules for mutual conversions between pointers to a structure and its first member say that I can write like this
S s;
int *pInt = (int ...
0
votes
0
answers
34
views
Calling methods from array of method pointers not executing [duplicate]
This doesn't execute any suggestions:
void (CClient::*pMethod[CNetwork::ClientEvent::E_CE_MAX_EVENTS])();
pMethod[CNetwork::ClientEvent::E_CE_ACCEPTED] = &CClient::Accepted;
pMethod[CNetwork::...
1
vote
1
answer
129
views
Template function accepting a pointer-to-member refuses to compile [duplicate]
Consider this class:
template <typename T>
struct Buffer {
template <typename Field>
int add_member_attribute(
std::string&& name,
Field T::* ...
0
votes
0
answers
61
views
using `std::views:transform` with a pointer to data member [duplicate]
In this so question about sorting only some fields in a std::vector of struct, this interesting answer was provided:
std::vector<SimpleStruct> vs = {{1, 10, 11}, {5, 100, 111}, {3, 1000, 1111}};
...
0
votes
1
answer
100
views
Strange inheritance behavior with base class
I hope someone can help me since can't quite understand how it is possible that the following code can work.
I have a base class with some classes that derive from it.
Each derived class has its own ...
0
votes
1
answer
114
views
Are there hidden dangers when using callbacks with member pointers?
In Arduino projects, I often find that I need to have some class call a callback function when something happens, for instance when a pin reaches a certain value, or when a certain amount of time has ...
1
vote
1
answer
87
views
pointer to member function as template parameter issue
I want to be able to defer calls to member functions of some object. Therefore, I need to be able to store member function pointers plus parameters as data.
My approach uses C++ templates that treat ...
4
votes
3
answers
188
views
Can you have a pointer to a data member of a subobject's class?
I've got interested in one thing - in c++ we have pointers to data member, for example:
struct A
{
int a_1 = 1;
};
A a;
int A::* p_a = &A::a_1;
a.*p_a = 10;
But if I have something like ...
0
votes
1
answer
626
views
C++ Callback to member function pointers on ESP32 / Arduino
It seems a difficult thing to do in C++. A callback to a member function in a class in C style, thus making this a void(*)() to be able to use this in a C-style callback function.
In this case I want ...
1
vote
1
answer
149
views
C-style wrapper to C++ member function, ordinary function pointer to member function
So I sort of tried to accomplish “the impossible” at least in C++20 and with the constraint that the class for which I am extracting a member function pointer can be evaluated as a constexpr.
#include ...
0
votes
1
answer
177
views
C++ pointer-to-member if the member is an array
I have a structure with several members of unsigned short type. The values stored in these types should be in a certain range, so I use a setter method for them that checks the range of the value ...
-1
votes
1
answer
93
views
How to call member function pointer where it is not in class itself? [duplicate]
struct CALLBACK_DATA
{
Object *pointer;
void (Object::*callback)();
};
int main()
{
CALLBACK_DATA data = getData();
//This isn't working.
(data.pointer)->(data.*callback)();
}
...
1
vote
1
answer
66
views
Embedded: How replace the interrupt vector call with a lambda (poiner to member) in C++17
At the moment I have a problem. I want to replace the interrupt vector target call for a peripheral driver. What works for now is this:
namespace test
{
class PortDriver{
public:
using ...
-1
votes
1
answer
99
views
Why value of member variable is changing? [duplicate]
Here is a linked list:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct Arrival
{
string arrAddress;
double distance;
string ...
-1
votes
1
answer
105
views
How can I fix the argument type mismatch between my key-callback function and glfwSetKeyCallback?
I have created a Camera class containing a keymap of std::map<int, void(Camera::*)() to facilitate an in-game option for key bindings and higher performance key-action lookup than what could be ...
0
votes
1
answer
137
views
Member function that takes member function as parameter
I have one class Hunter that has a member function random_hunt which returns a pair<int, int> const that's supposed to be equivalent to some point (x, y). I'm trying to implement a second member ...
2
votes
2
answers
111
views
passing member function const and non-const overload to std::function
I was trying to get "more fluent" with function pointer manipulation and I've got an issue with passing a member function to std::function when there are two overload: const and non-const. ...
3
votes
1
answer
169
views
Can I deduce the argument types of a member function pointer template parameter?
I often work with C-style libraries, where the callback-and-opaque-pointer pattern is common:
void callback(void *user_data, ...) { ... }
register_callback(callback, ptr_to_something_useful);
When ...
0
votes
1
answer
52
views
Append, C-strings and struct member functions
How I can add what characters I want to the end of what the user would type in. Also a pointer is being passed into the wordArr[] if that helps at all
#include <iostream>
#include <string>
...
2
votes
2
answers
113
views
How to pass a pointer to member function to a parent class?
I have a class which derives from another class who implements a map of callback functions.
I need to pass pointers to the callback from the child to the parent but I get compilation conversion errors....
0
votes
2
answers
52
views
pass non-static member function with neither virtual inheritance nor templates [duplicate]
How do I pass the non-static member function eval of object Problem1 obj to the object Solver solver ?
#include<iostream>
#include<functional>
// non-templated class
struct Solver{
...
0
votes
2
answers
113
views
pointer to member function in C++ [duplicate]
I have problem in this line std::cout << &X::a << std::endl; this line print 1 supposed to printed address of &X::a
this my code
#include <iostream>
#include <string>...
0
votes
2
answers
550
views
Getting an error of "Reference to non-static member function must be called"
I'm not able to understand what this error means, and what is causing it.
Here is my code, please help me to point out the mistake:
class Solution {
public:
bool sortbysec(const pair<int,int>...
1
vote
2
answers
88
views
Passing a set of pointers to member functions of a template class
I have here a minimized version of code below, that won't compile.
#include<iostream>
#include<vector>
#include<set>
template<int D>
class Points{
public:
std::vector<...
2
votes
2
answers
215
views
Is it possible to return a member variable of a class specified by a template function?
I am trying to generalize a function for a game engine I am writing to simplify the shader loading process.
Anyway, the difficulty arises in my attempts to templating the function. I attempted to ...
1
vote
1
answer
119
views
How to return a member function pointer from a class method?
I am trying to use something like a strategy pattern, where there are two different Parsers Type1Parser and Type2Parser using an interface IParser.
There is another class ParsingText where we have 2 ...
0
votes
1
answer
488
views
How to get the address/offset of the struct member for serialization purposes
I am trying to implement a function that sets a struct member and also serializes the data to an external output. For serialization purposes, I need to know the size and offset of the struct member, ...
0
votes
0
answers
20
views
Map of method pointers refactored from map of function pointers - C++ [duplicate]
I was utilizing a map of function pointers with different option values as follows in my main.cpp file for different menu items:
int main()
{
while(true)
{
printMenu();
int ...
2
votes
1
answer
161
views
Higher order functions in C++: Creating an integrator that accepts member functions
I would like to create a function-object class that numerically integrates some functions on-demand. Its public interface would consist of a method void T::step(double dt) that computes an integration ...
2
votes
3
answers
517
views
How do I pass a member function template as a template parameter?
I have a member funtion like this:
class Foo
{
public:
template<typename... Args>
static void bar(const Args&... args)
{
/* code */
}
};
And I want to pass it as a template ...
0
votes
2
answers
159
views
Call function implementing type on instance by a pointer
I'm trying to implement subscriber-publisher pattern. My base class Subscriber doesn't have a listener method, it declares type Handler instead. The idea behind this is that a derived class will be ...
2
votes
0
answers
79
views
Getting the default argument of function (C++) [duplicate]
I have a function that takes a pointer to a member function (along with the necessary arguments) and calls the function on a class instance. Is there a way to not have to pass the default arguments or ...
2
votes
0
answers
64
views
C++ Use pointer-to-member and absolute address of member to get address of object [duplicate]
With a class definition, an object pointer and a pointer-to-member, it is possible to obtain an absolute pointer to that member:
class X {
public:
int a { 0 };
int b { 1 };
int c { 2 };
};
...
1
vote
3
answers
623
views
Pointer to class member as template argument
How to implement a template that gets a class and a pointer to a member of this class?
I want to template a sort of Accessor of the form
template<??? T, ??? P>
class Accessor
{
// ...
T&...
0
votes
0
answers
31
views
Change with taking pointers-to-members as template arguments in C++17 [duplicate]
I know that C++17 introduced template< auto >, but the following example doesn't use it, yet still only compiles in C++17:
struct Foo
{
int bar;
};
template< int Foo::* >
void ...
0
votes
1
answer
458
views
Create object of class B inside class A and pass a method of Class A in constructor of object of Class B [duplicate]
I am trying to create object of another class in one class (I am able to do this) and I want to pass a method of the first class to the second class as constructor argument (I am not able to do this. ...