2013-01-16 47 views
0

繼承方法我有我的主類,看起來像這樣:主類不能從我的第二類

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

很明顯,我試圖從我的類繼承ReadOffset方法在我的MemoryAddressing類中。我不知道如何去做,似乎這些班級無法溝通。

我已經知道Java和C#,但我認爲C++是非常不同的。

+0

使用公共繼承。即:'class MemoryAddressing:public Memory' –

+0

爲什麼要創建一個mem本地變量? – Csq

+2

這段代碼真的是C++嗎? – undu

回答

1

有沒有這樣的概念static class

和一類的默認的傳承是私有的。私有繼承意味着該關係對類的用戶是隱藏的。使用它很少見,但有點像聚合,意味着「在」面向對象的層面上實現「,而不是」是一種類型「。

而且不建議調用方法_tmain,但是你想要做什麼?覆蓋主? (或者你試圖複製Java,其中類有靜態主要方法,使他們作爲入口點可運行)。

0

在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本質上不會導致繼承發生。

+0

它說錯誤:當繼承類MemoryAddressing時不是結構體或類:public Memory – Paze

+0

@ user1924410可能是因爲'static class' doesn'對編譯器是有意義的,而在C++中,如果某些東西的聲明被搞砸了,那麼它就不會識別它。 – ApproachingDarknessFish

+0

我刪除了靜態:P – Paze