在派生类的方法中使用基类的方法
Using a base class's methods inside a derived class's methods
我对继承的理解非常糟糕,我似乎找不到正在寻找的答案。对于这篇文章的时间,我深表歉意,但我觉得我需要非常具体。
我有一个基类" 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
和外部的位置。
- 在模板基类中为继承类中的可选重写生成虚拟方法
- 为什么此派生对象无法访问基类的后递减方法?
- 使用基类中的派生方法运行线程,而无需使用模板
- 将子类方法声明为基类的友元
- 是否可以使用基类非虚拟方法中的派生类虚拟方法?
- 在派生类中使用基类的私有成员变量的最佳方法
- 如何在成为指向基类的指针后保留对子类方法的使用?
- 从基类实例调用派生类方法而不进行强制转换
- 继承:调用基类的成员和方法
- 通过基类接受方法转发派生 UniquePtr 的右值会移动引用而不是复制
- 引用基类模板的成员变量的简单方法
- C++ 使用派生类方法更改基类数据成员
- C++初始化之前派生类调用基类的方法
- 将派生类方法与基类unique_ptr一起使用
- 为什么派生类的实例从基类调用方法?
- 基类可以声明虚拟方法但不定义它吗?仍然在派生类中定义
- googletest:测试基类具有纯虚拟方法的派生类时的核心转储
- 从基类调用重写的方法
- 为什么允许将派生类的方法static_cast为基类的方法?
- C++-从方法基类调用派生类中的重写方法