スーパークラスを継承したメンバ関数を、同じスーパークラスを継承したメンバ関数ポインタにいれたいのですがうまくいきません。
このような形で実装してみたいのですが、どうすればいいのでしょうか?
そもそも無理なのでしょうか?
Source.cpp
#include<stdio.h>
#include<vector>
using namespace std;
#include"Header.h"
/////////////////////////////////////
// Cardの基礎部分
void cCard::AddToAbility(CardGrant tes)
{
this->Grant.push_back(tes);
}
void cCard::Preparition()
{
for (auto v : Grant)
{
v;
}
}
void cCard::Test()
{
printf("%d", this->hp);
}
/////////////////////////////////////
// メインループ!
int main()
{
cMonster1 mons1;
cMonster2 mons2;
// モンスター1号の能力を発動!ここでerror C2276
mons2.AddToAbility(&mons1.Ability);
// モンスター2号の処理
mons2.Preparition();
// モンスター2号のhp表示
mons2.Test();
while (1);
}
Header.h
#pragma once
class cCard;
typedef void (cCard::*CardGrant)(); // たぶんこれが問題だと思ってます。
class cCard
{
public:
cCard():hp(1) {}; // カードの初期化
virtual ~cCard() {};
virtual void Ability() {}; // このカードの能力
void AddToAbility(CardGrant); // 能力を受けた
void Preparition(); // このカードに受けた効果を処理する
void Test(); // hpを表示する
protected:
struct
{// カードの中身
int hp;
};
vector<CardGrant> Grant; // このカードにかかってる付与効果
};
////////////////////////////////////////////
// ここからカードの中身について書いてくよ!
class cMonster1 : public cCard
{// モンスター1号!
void Ability() { this->hp -= 1; }; // hpを0にしたい!
};
class cMonster2 : public cCard
{// モンスター2号!
};