[英]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.";
}
---------------------------------------------------------------
I am getting segmentation fault at : common/thread.cc:36 ( "this" is not able to >access function overridden in derived class). 我在以下位置遇到了段错误:common / thread.cc:36(“ this”无法访问派生类中重写的函数)。 Please help me on this. 请帮我。 I know something >very basic is wrong. 我知道>基本错误。 If i write both the classes in same file or namespace it works. 如果我将两个类都写在同一个文件或名称空间中,它将起作用。
This line is a problem. 这条线是个问题。
void *(*threadRun) (void*) = reinterpret_cast <void *(*) (void *)> (CThread::ThreadRun) ;
You are taking a function that is supposed to return an int
and casting it to a function that returns a void
. 您正在使用应该返回一个int
的函数,并将其强制转换为一个返回void
的函数。 The stack structure for those function types are most likely different. 这些功能类型的堆栈结构很可能会有所不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.