简体   繁体   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.";
}
---------------------------------------------------------------

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.

相关问题 在基类构造函数中对派生类使用重写函数 - Using overriden function in base class constructor for derived class 使用派生 class 成员 function 访问基 class 的私有成员 - Accessing private member of base class using derived class member function 基类函数访问派生类成员 - Base class function accessing derived class member 派生类不调用基类的成员函数 - Derived class not calling member function of base class 如何使用基础 class ZC1C425268E68385D1AB5074C17A9 访问派生的 class 成员 function? - How to access derived class member function using Base class function? 使用基类函数指针访问派生类成员函数 - Using base class function pointer to access derived class member function 在派生类的对象上使用指向基类的成员函数的指针 - Using pointer to member function of a base class on object of derived class C ++多态性:派生类调用基类虚拟函数,而不是重写的派生类 - C++ Polymorphism: Derived class calls Base class virtual funciton instead of the overriden derived one 将基类函数与派生类值一起使用 - Using base class function with derived class values 使用基成员 function 的重载解决方案引入派生 class - the overload resolution of using base member function introduced into derived class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM