简体   繁体   中英

Main class can't inherit method from my second class

I have my main class which looks like this:

#include "stdafx.h"
using namespace std;

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

and then I have my second class:

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

It is obvious that I'm trying to inherit the ReadOffset method from my Memory class in my MemoryAddressing class. I have no idea how to, it seems like the classes are unable to communicate.

I already know Java and C# but I think C++ is very different.

There is no such concept as static class

And the default inheritence for a class is private. Private inheritence means that the relationship is hidden from users of the class. It is rare to use it but is a bit like aggregation and means "is implemented in terms of" on an Object Orientated level, rather than "is a type of".

And it's not advised to call a method _tmain, but what are you trying to do? override main? (Or you're trying to copy Java where classes have static main methods to make them runnable as entry points).

In C++ static class doesn't work. Also your inheritance syntax is off:

class MemoryAddressing : Memory {

should be

class MemoryAddressing : public Memory {

In C++ classes require an access modifier after the : in inheritance. If none is provided, it defaults to private . Here is an excerpt from 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.

So without a modifier only private members are inherited. However, since private members cannot be accessed from derived classes, so in essence class A : private B essentially causes no inheritance to happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM