pointer to class member

this project is for an embedded micro controller.

in the project i wrote a class that generically services uarts. then i declare 6 objects of that class and hand them configurations for each specific uart.

internally all the objects have a send buffer of data that is still to be sent that gets populated by the object member function.

how can i make an array of function pointers that can point to the same member but of six different objects.

for example (not a working one)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class uart 
{
private:
struct myData
{
unsigned char data[20]
int head
int tail
int count;
}

public:
void data_put(unsigned char part) //simplified
{
data[head] = part;
head++;
count++;
}
}


uart uart_one;
uart uart_two;
uart uart_three;


void (*data_deposit[3])(unsigned char);

data_deposit[0] = &uart_one.data_put;
data_deposit[1] = &uart_two.data_put;
data_deposit[2] = &uart_three.data_put;
Pointers to member functions are associated with classes, not with objects. You can invoke them on any instance of the class they came from. You only need one pointer to member function in this case.

return_type (NameOfClass::*varname)(arg_types, ...) = &NameOfClass::member_function;

I recommend using a typedef.
1
2
3
typedef return_type (NameOfClass::*function_pointer_type)(arg_types, ...);
//or in C++11:
using function_pointer_type = return_type (NameOfClass::*)(arg_types, ...);
Last edited on
in your example

return_type (NameOfClass::*varname)(arg_types, ...) = &NameOfClass::member_function;

using my example would look like this?
void (uart::*point)(unsigned char) = &uart::data_put;

how would you then associate this pointer with a particular object?
if (from my example) i want to point to data_put ing the uart_two object.

further how could i make a look up table to store all the available objects that can be pointed to by the pointer?
viniisiggs wrote:
how would you then associate this pointer with a particular object?
1
2
(some_instance.*point)(some_unsigned_character);
(some_pointer_to_instance->*point)(some_unsigned_character);
viniisiggs wrote:
further how could i make a look up table to store all the available objects that can be pointed to by the pointer?
Why do you need this at all?
Last edited on
my project is that i'm making a simple uart router. the micro controller i'm using has 6 uarts. i want it to bring in a packet read it and verify that it's intact then send it to where it needs to go.

the way i designed my project is i have two generic classes, one to receive and one to send. i then declare 6 objects of each and hand them configuration setting to work with the chips hardware.

i'm trying to set up a system where if a message comes in on uart 1 that needs to go to uart 4 after the message is complete and verified the receive object for uart 1 can hand the message over to the send object for uart 4. (the packet length is variable to make things more complicated)

the thought i had was to use an array as a selector. if the address is 4 then

1
2
my_function_pointer = list_of_functions[4];
*myfunction(data_to_send);


if there is a better way to do this i'm all ears.
You only need one pointer to member function per class. In fact, &NameOfClass::some_member always gives the same address for the same class and member.
Last edited on
i'm sorry if i'm beating a dead horse but i'm at the limit of my understanding.

if i'm using one pointer how and where would i declare it and how would i make it point to different objects depending on what address the packet needs to go to?
Pointers-to-members are not ever associated with objects. Ever. No exceptions.

Given an object, you can access the member in the context of that object given the pointer-to-member.

I already posted this:
1
2
(some_instance.*point)(some_unsigned_character);
(some_pointer_to_instance->*point)(some_unsigned_character);
point is your pointer-to-member-function. The second set of parens calls the function.
Last edited on
> how can i make an array of function pointers that can point to the same member but of six different objects.

Bind and wrap.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <functional>

struct uart
{
    void do_put_data( unsigned char )
    { std::cout << "uart::do_put_data for uart '" << name << "' at " << this << '\n' ; }

    uart( std::string name ) : name(name) {}
    std::string name ;

    using data_put_t = std::function< void( unsigned char ) > ;
    const data_put_t data_put() { return std::bind( &uart::do_put_data, this, std::placeholders::_1 ) ; }
};

int main ()
{
    uart one("one"), two("two"), three("three"), four("four") ;

    uart::data_put_t data_put[] { one.data_put(), two.data_put(), three.data_put(), four.data_put() } ;

    for( const auto& put : data_put ) put(10) ;
}

http://coliru.stacked-crooked.com/a/183aa8402b65be9c
Bind and wrap (freestanding implementation):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct uart
{
    void do_put_data( unsigned char ) { /* ... */ }

    struct data_put_t
    {
        uart* const This ;
        data_put_t( uart* p ) : This(p) {}
        void operator() ( unsigned char c ) const { This->do_put_data(c) ; }
    };

    data_put_t data_put() { return data_put_t(this) ; }
};

int main ()
{
    const int NUARTS = 4 ;
    uart one, two, three, four ;

    uart::data_put_t data_put[NUARTS] { one.data_put(), two.data_put(), three.data_put(), four.data_put() } ;

    for( int i = 0 ; i < NUARTS ; ++i ) data_put[i](10) ;
}
Topic archived. No new replies allowed.