【问题标题】:Main class can't inherit method from my second class主类不能从我的第二类继承方法
【发布时间】:2013-01-16 16:33:38
【问题描述】:

我的主课如下所示:

#include "stdafx.h"
using namespace std;

class MemoryAddressing : Memory {
    int _tmain(int argc, _TCHAR* argv[])
    {
        Memory mem;
        int hp = Memory.ReadOffset(0x000000);
    }
}

然后我有第二节课:

#include <windows.h>
#include <iostream>

using namespace std;

static class Memory {
public : static int ReadOffset(DWORD offset) {
    DWORD address = 0x000000;
    DWORD pid;
    HWND hwnd;
    int value = 0;

    hwnd = FindWindow(NULL, L"");

    if(!hwnd) {
        cout << "error 01: Client not found, exiting...\n";
        Sleep(2000);
    }else {
        GetWindowThreadProcessId(hwnd, &pid);
        HANDLE handle = OpenProcess(PROCESS_VM_READ, 0, pid);
        if(!handle) {
            cout << "error 02: no permissions to read process";
        }
        else {
            ReadProcessMemory(handle, (void*) offset, &value,     sizeof(value), 0);
        }
    }
}
};

很明显,我试图在我的MemoryAddressing 类中从我的Memory 类继承ReadOffset 方法。我不知道该怎么做,似乎班级无法交流。

我已经了解 Java 和 C#,但我认为 C++ 非常不同。

【问题讨论】:

  • 使用公共继承。即:class MemoryAddressing : public Memory
  • 为什么要创建一个mem局部变量?
  • 这段代码真的是 C++ 吗?
  • 你期待什么输出?你得到什么输出? (请复制并粘贴)。
  • 我听说有人有Memory 课程,但我从没想过这是真的……

标签: c++ class memory inheritance methods


【解决方案1】:

没有static class这样的概念

并且类的默认继承是私有的。私有继承意味着关系对类的用户是隐藏的。很少使用它,但有点像聚合,意思是“在面向对象级别上实现”,而不是“是一种类型”。

并且不建议调用方法 _tmain,但是您要做什么?覆盖主要? (或者您正在尝试复制 Java,其中类具有静态主要方法以使其可作为入口点运行)。

【讨论】:

    【解决方案2】:

    在 C++ 中 static class 不起作用。您的继承语法也已关闭:

    class MemoryAddressing : Memory {
    

    应该是

    class MemoryAddressing : public Memory {
    

    在 C++ 中,类在继承中需要在 : 之后的访问修饰符。如果未提供,则默认为 private。这是the C++ tutorial's chapter on inheritance:的摘录

    In order to derive a class from another, we use a colon (:) in the declaration of the    
    derived class using the following format:
    
    class derived_class_name: public base_class_name
    { /*...*/ };
    
    Where derived_class_name is the name of the derived class and base_class_name is the   
    name of the class on which it is based. The public access specifier may be replaced by  
    any one of the other access specifiers protected and private. This access specifier   
    describes the minimum access level for the members that are inherited from the base   
    class.
    

    因此,如果没有修饰符,则只会继承私有成员。但是,由于无法从派生类访问私有成员,所以本质上class A : private B 基本上不会导致继承发生。

    【讨论】:

    • 它说错误:使用类 MemoryAddressing 继承时不是结构或类:公共内存
    • @user1924410 可能是因为static class 对编译器没有意义,而在 C++ 中,如果某些东西的声明被搞砸了,它甚至不会识别它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 2012-09-03
    • 2014-05-10
    • 2010-12-21
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    相关资源
    最近更新 更多