繁体   English   中英

在基类函数中使用此函数来处理派生类成员函数,其中函数在派生类中被覆盖

[英]Acessing derived class member function using this in base class funciton, where function in overriden in derived class

-------------common/cthread.h----------------
#include<iostream>
using namespace std ;
#include<string.h>
#include<pthread.h>
#include<stdio.h>
#include<errno.h>

namespace Framework
{
class CThread
{
 public:
 CThread(void)
 {
 }
 virtual ~CThread(void)
 {
 }
 void Execute() ;
 virtual unsigned int Run(void ) = 0 ;
 static unsigned int ThreadRun(void *obj) ;

 protected:
 pthread_t thread_obj ;

 private:
 int thread_ret_value ;
};
}
-----------common/cthread.cc----------------
#include "cthread.h"

namespace Framework
{

void *(*threadRun) (void*) = reinterpret_cast <void *(*) (void *)> (CThread::ThreadRun) ;
//int CThread::value_part = 20 ;

void CThread::Execute()
{
    cout<<endl<<"["<<__FILE__<<":"<<__LINE__<<"] "<<"Initializing CThread...";
    try
    {
        thread_ret_value = pthread_create(&thread_obj, 0, threadRun, this) ;
        if(thread_ret_value)
        {
            cout<<"["<<__FILE__<<":"<<__LINE__<<"] "<<"Thread could not be created : "<<strerror(thread_ret_value);
            fprintf(stderr,"Error - pthread_create() return code: %d\n",thread_ret_value);
        }
        else
            pthread_join(thread_obj, NULL) ;
    }
    catch(exception& e)
    {
        cout<<endl<<"["<<__FILE__<<":"<<__LINE__<<"] "<<"Exception Occured in ["<<__FILE__<<":"<<__LINE__<<"] "<<e.what() ;
    }
    cout<<endl<<"["<<__FILE__<<":"<<__LINE__<<"] "<<"Exiting CThread...";
}

unsigned int CThread::ThreadRun(void *obj)
{
    int ret_status = 0 ;
    cout<<endl<<"["<<__FILE__<<":"<<__LINE__<<"] "<<"Started CThread...";
    CThread *pthread = (CThread*) obj ;
    cout<<"\nRunning CThread..." ;
    pthread->Run() ;
    cout<<"\nStopped CThread..." ;
}

------------------dispatcherthread.h------------------------
#include<iostream>
using namespace std ;

#include "common/cthread.h"
using namespace Framework ;

class Dispatcherthread: public CThread
{

    public:
    Dispatcherthread()
    {
        cout<<"\nConstructor Dispatcherthread Called." ;
    }
    //static int nuThread ;
    void Initialize() ;
    virtual unsigned int Run() ;
    void aman() ;
    int *dispatcherImp();
    void stop() ;
};
-------------------dispatcherthread.cc-------------------
#include"dispatcherthread.h"

void Dispatcherthread::Initialize()
{
    cout<<endl<<"["<<__FILE__<<":"<<__LINE__<<"] "<<"Initializing Dispatcher Thread...";
}

unsigned int Dispatcherthread::Run()
{
    cout<<"\nThread started.";
/// for(int i=0; i<=10; i++)
///     cout<<"\nThread is running : "<<i ;
    cout<<"\nThread stopped.";
}
---------------------------------------------------------------

我在以下位置遇到了段错误:common / thread.cc:36(“ this”无法访问派生类中重写的函数)。 请帮我。 我知道>基本错误。 如果我将两个类都写在同一个文件或名称空间中,它将起作用。

这条线是个问题。

void *(*threadRun) (void*) = reinterpret_cast <void *(*) (void *)> (CThread::ThreadRun) ;

您正在使用应该返回一个int的函数,并将其强制转换为一个返回void的函数。 这些功能类型的堆栈结构很可能会有所不同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM