Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to member template classes

Tags:

c++

AbstractFieldCollection is the base class of hardwareMissingAlarm, etc. hardwareMissingAlarm belongs to another class that is a template.

alarmFieldCollection.push_back((AbstractAlarmField Device::*)  &Device::hardwareMissingAlarm);
alarmFieldCollection.push_back((AbstractAlarmField Device::*)  &Device::hardwareErrorAlarm);
alarmFieldCollection.push_back((AbstractAlarmField Device::*)  &Device::badConfigAlarm);``

Then in another function I'm reading the vector like this:

for(int32_t i=0; i<alarmFieldCollection.size(); i++) 
{
    AbstractAlarmField Device::* pAF = alarmFieldCollection[i];
    std::cout << "isRaised: "<< pDev << std::endl;
    if ((pDev->*pAF).isRaised(pContext))
    {
           .....
    }
 }

and pDev is the Device object, however pDev->*pAF returns NULL. In fact when I'm printing &Device::hardwareErrorAlarm, &Device::hardwareMissingAlarm the result is 1. I don't know what I'm doing wrong.

isRaised is a method that belongs to the class AbstractAlarmField.

Thanks in advance.

like image 430
user1977398 Avatar asked Mar 17 '26 02:03

user1977398


1 Answers

You provided almost no code but it seems like you are storing an abstract object by value, not by reference or pointer. This may lead to object slicing and any kind of memory problem as a consequence. Try to use AbstractAlarmField& as the type of Device fields instead.

like image 150
Karel Petranek Avatar answered Mar 19 '26 16:03

Karel Petranek