const_cast member function pointer

N

nick.keighley.zczc

Yes I read the FAQ! I'm not using a typedef as they don't seem to work withtemplates in C++98.


template<typename WB>
void EditProgramWizard::populateButton (MParameterButton* paramButton, WB* band,
int (WB::*read)(),
void (WB::*write)(int))
{
int (WB::*const_read)() = const_cast<int (WB::*)() const> (read);
PopulateButton (paramButton, band, const_read, write);
}

the cast fails with:

myprog.cpp: In member function ‘void EditProgramWizard::populateButton(MParameterButton*, WB*, int (WB::*)(), void (WB::*)(int)) [with WB = WeightBand]’:
myprog.cpp:464:125: instantiated from here
myprog.cpp:1521:68: error: invalid use of const_cast with type ‘int (WeightBand::*)()const’, which is not a pointer, reference, nor a pointer-to-data-member type

I was under the impression ‘int (WeightBand::*)()const' *was* a pointer.

Oh. Are pointers to member functions not const castable?
 
A

Alf P. Steinbach

template<typename WB>
void EditProgramWizard::populateButton (MParameterButton* paramButton, WB* band,
int (WB::*read)(),
void (WB::*write)(int))
{
int (WB::*const_read)() = const_cast<int (WB::*)() const> (read);
PopulateButton (paramButton, band, const_read, write);
}

the cast fails with:

myprog.cpp: In member function ‘void EditProgramWizard::populateButton(MParameterButton*, WB*, int (WB::*)(), void (WB::*)(int)) [with WB = WeightBand]’:
myprog.cpp:464:125: instantiated from here
myprog.cpp:1521:68: error: invalid use of const_cast with type ‘int (WeightBand::*)()const’, which is not a pointer, reference, nor a pointer-to-data-member type

I was under the impression ‘int (WeightBand::*)()const' *was* a pointer.

You're adding 'const' to the function signature.

And no, a member pointer is not a pointer in the ordinary sense.

It's more like an offset, which needs to be combined with an object
pointer or reference (using ugly syntax) in order to be usable.

Oh. Are pointers to member functions not const castable?

Pointers yes, functions no.


Cheers & hth.,

- Alf
 
N

nick.keighley.zczc

template<typename WB>
void EditProgramWizard::populateButton (MParameterButton* paramButton, WB* band,
int (WB::*read)(),
void (WB::*write)(int))
{
int (WB::*const_read)() = const_cast<int (WB::*)() const> (read);
PopulateButton (paramButton, band, const_read, write);
}
the cast fails with:
myprog.cpp: In member function ‘void EditProgramWizard::populateButton(MParameterButton*, WB*, int (WB::*)(), void (WB::*)(int)) [with WB = WeightBand]’:
myprog.cpp:464:125: instantiated from here
myprog.cpp:1521:68: error: invalid use of const_cast with type ‘int (WeightBand::*)()const’, which is not a pointer, reference, nor a pointer-to-data-member type

as usual gcc is doing its best to tell me what I've done wrong.

so not a pointer...
You're adding 'const' to the function signature.

yes! I wanted to turn a 'int WB::read ()' into a 'int WB::read() const' as there's some const inconsistency in the bowels of the program
And no, a member pointer is not a pointer in the ordinary sense.
It's more like an offset, which needs to be combined with an object
pointer or reference (using ugly syntax) in order to be usable.

ah yes
Pointers yes, functions no.

Cheers & hth.,

yes.

My colleague suggested a reinterpret_cast. I guess this is digging the holeeven deeper?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top