主类无法从我的第二个类继承方法

Main class can't inherit method from my second class

本文关键字:第二个 继承 方法 我的      更新时间:2023-10-16

我的主类是这样的:

#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);
        }
    }
}
};

很明显,我试图从我的Memory类继承ReadOffset方法在我的MemoryAddressing类。我不知道如何,似乎类无法通信。

我已经知道Java和c#,但我认为c++是非常不同的。

没有static class这个概念

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

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

在c++中static class不起作用。你的继承语法也关闭了:

class MemoryAddressing : Memory {
应该

class MemoryAddressing : public Memory {
c++中的

类需要在继承中的:之后添加访问修饰符。如果没有提供,则默认为private。下面是c++教程中继承章节的节选:

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基本上不会导致继承发生。

相关文章: