在派生类的方法中使用基类的方法

Using a base class's methods inside a derived class's methods

本文关键字:方法 基类 派生      更新时间:2023-10-16

我对继承的理解非常糟糕,我似乎找不到正在寻找的答案。对于这篇文章的时间,我深表歉意,但我觉得我需要非常具体。

我有一个基类" progctl",可以向进程发送信号。我有一个名为"蜂鸣器"的简单程序,该程序刚刚永远运行,给出"蜂鸣!"每10秒发出消息,直到中断。我负责制作" beepctrl"的派生类旨在专门发送信号并更改蜂鸣器程序。

我的问题是:我无法弄清楚如何从beepctrl中的方法访问progctl的方法和变量。主要在beepctrl.cpp中,我必须使用progctl的活着并发送方法来检查该过程是否存在并结束。

我的蜂示构建器的progctl扩展是什么?我的笔记只说这使我能够设置属性值。我真的很抱歉。我知道尝试在这里找到多个问题(和广泛的问题)是不好的礼节,但我确实迷路了。

progctl.hpp

#ifndef  PROGCTL_HPP
#define PROGCTL_HPP
#include <stdlib.h>
#include <unistd.h>
#include <string>
class ProgCtl {
    private:
      std::string   m_program;
      pid_t         m_pid;
      void          find_pid();
    public:
      // Constructors
      ProgCtl():m_program(""),m_pid(-1){}
      ProgCtl(std::string prog):m_program(prog),m_pid(-1){}
      ProgCtl(char *prog):m_pid(-1) {
          m_program = std::string(prog);
      }
      // Setters
      void       setName(std::string prog);
      void       setName(char *prog);
      void       setPid(pid_t pid){m_pid = pid;}
      // Other
      bool       alive();
      int        send(int sig);
      int        start();
};
#endif

beepctrl.hpp;由于这个基本问题

,远非完整
#ifndef BeepCtrl_HPP
#define BeepCtrl_HPP
#include "ProgCtl.hpp"
#include <string>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <iostream>
class BeepCtrl: public ProgCtl{
public:
    BeepCtrl(std::string name):ProgCtl(name){};
    BeepCtrl():ProgCtl(){};
    BeepCtrl(char *prog):ProgCtl(prog){};
    void stop(); //if alive, send kill
    void beep_faster(); //test if beeper is alive then decrement inveral
    void beep_slower(); // increment interval
};
#endif

beepctrl.cpp

#include "BeepCtrl.hpp"
void stop() //if alive, send kill
{
    //std::set_terminate(std::cerr << "terminate handler calledn");
    if(alive()){//---alive and send not declared in this scope---
        send(9);
    }
    else{
    throw 0;
    }
}
void beep_faster() //test if beeper is alive then decrement inveral
{
//throw if an invalid value
}
void beep_slower() // increment interval
{
//throw if an invalid value
}

您应该在CPP文件中为您的方法指定类:

void BeepCtrl::stop() //if alive, send kill
{
    //std::set_terminate(std::cerr << "terminate handler calledn");
    if(alive()){//---alive and send not declared in this scope---
        send(9);
    }
    else{
    throw 0;
    }
}

edit 这个答案我无法弄清楚如何从beepctrl 中的方法访问progctl的方法和变量。@sergi0解决了实际的汇编问题。


应该通过派生类访问的基类的成员应声明protected而不是private

class ProgCtl {
    protected:
      std::string   m_program;
      pid_t         m_pid;
      void          find_pid();
    public:
      // Constructors
      ...
};

仍然无法从外部访问受保护的名称,但是子类可以访问它们。

当您继承一类时,也会发生类似的事情:

class BeepCtrl: public ProgCtl { ... }

在这里,通过类型BeepCtrl对象对ProgCtl的公共名称的访问将保持不变。受保护的名称仍将受到保护。

class BeepCtrl: protected ProgCtl { ... }

在这里,仅在 BeepCtrl中的函数中,可以通过类型BeepCtrl对象访问ProgCtl的公共名称。

class BeepCtrl: private ProgCtl { ... }

在这里,ProgCtl的任何名称都隐藏在BeepCtrl和外部的位置。