[C++] 如何使用父类调用子类的函数实现

slowfastflow slowfastflow 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

class A
{
public:
A();
~A();
virtual void fun(string str1, string str2) = 0;
}

class B: public A
{
public:
B();
~B();
virtual void fun(string str1, string str2)
{
cout << str1.c_str() << str2.c_str() << endl;
}
}

class C
{
public:
C();
~C();
……
void fun2(A* a);
……
protect:
typedef std::list<A*> A_LIST;
A_LIST m_alist;
typedef std::list<string> S_LIST;
S_LIST m_slist;

string m_str;
pthread_t m_metux;
……
}

C::fun2(A*a)
{
if(a == NULL)
return;

pthread_mutux_lock(m_metux);
m_alist.push_back(a);
m_alist.unique();

S_LIST:const_iterator iter = m_slist.begin();
for(; iter != m_slist.end(); ++iter) {
string idetity = (*iter);
a->fun(m_str, idetity);
}
}


如何使用函数 fun2?


int main(int argc, char ** argv)
{
A* p = new A;

// C::fun2(p);

return 0;
}
...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
slowfastflow
C++ 语言 65,214 成员 250,514 内容