动态函数调用C ++

时间:2015-08-26 10:43:02

标签: c++ c++11

我想做这样的事情:

public class SampleFragmentPageAdapter extends FragmentStatePagerAdapter {

final int PAGE_COUNT = 3;
private String tabTitles[] = new String[]{"HOME", "BEACON", "NEARBY"};
private int[] imageResId = {
        R.drawable.home,
        R.drawable.beacon,
        R.drawable.nearby
};
private Context context;

public SampleFragmentPageAdapter(FragmentManager fm, Context context) {
    super(fm);
    this.context = context;
}

@Override
public Fragment getItem(int position) {
    Fragment fragment = null;
    if (position == 0)
        fragment = new FragmentTab1();
    if (position == 1)
        fragment = new FragmentTab2();
    if (position == 2)
        fragment = new FragmentTab3();
    return fragment;
}

@Override
public int getCount() {
    return PAGE_COUNT;
}

@Override
public CharSequence getPageTitle(int position) {
    // Generate title based on item position
    Drawable image = context.getResources().getDrawable(imageResId[position]);
    image.setBounds(0, 0, 32, 32);
    // Replace blank spaces with image icon
    SpannableString sb = new SpannableString("   " + tabTitles[position]);
    ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
    sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return sb;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    return super.instantiateItem(container, position);
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    super.destroyItem(container, position, object);
}
}

电话会是:

class Base{};
class Specialized1 : public Base
{
public:
    int GetCount(){ return 1; }
};
class Specialized2 : public Base
{
public:
    bool IsCorrect() { return true; }
};

class Example
{
public:
    template< class ATTR_CLASS, class RETURNED_PARAMETER_CLASS >
    int GetPerfectAttributeIndex( const RETURNED_PARAMETER_CLASS & perfect_parameter, ***RETURNED_PARAMETER_CLASS (*function_to_call)()*** )
    {
        for ( int i = 0; i < AttributeCount; ++i )
        {
            if ( perfect_parameter ==
                static_cast< ATTR_CLASS >( MyAttributeTable[ i ] )->function_to_call() )
            {
                return i;
            }
        }
        return -1;
    }


    Base** MyAttributeTable;
    int AttributeCount;
};

所以我知道这段代码因为***之间的部分而无法正常工作 但是我怎样才能改变它以使其工作?使用一些C ++ 11魔术?

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

问题是function_to_call不是指向成员函数的指针。您应该使用Base*dynamic_cast更加安全,然后再检查nullptr

class Base
{
public:
    virtual ~Base() = default;
};

class Specialized1 : public Base
{
public:
    int GetCount() { return 1; }
};

class Specialized2 : public Base
{
public:
    bool IsCorrect() { return true; }
};

class Example
{
public:
    template <class ATTR_CLASS, class RETURNED_PARAMETER_CLASS>
    int GetPerfectAttributeIndex(
        RETURNED_PARAMETER_CLASS const& perfect_parameter,
        RETURNED_PARAMETER_CLASS(ATTR_CLASS::*function_to_call)()) // added ATTR_CLASS::
    {
        for(int i = 0; i < AttributeCount; ++i)
        {
            auto ptr = dynamic_cast<ATTR_CLASS*>(MyAttributeTable[i]);

            if(!ptr)
            {
                // handle the case of an invalid cast
            }

            if(perfect_parameter == (ptr->*function_to_call)()) // extra parentheses added and ->* operator used
                return i;
        }

        return -1;
    }

    Base** MyAttributeTable;
    int AttributeCount;
};