簡體   English   中英

C ++-為什么[pointer]和this-> [pointer]給出不同的值?

[英]C++ - Why do [pointer] and this->[pointer] give different values?

我有一個GameWorld類,其中有ALLEGRO_DISPLAY *display;

我試圖定義一個對象,該對象具有一個指向GameWorld對象的指針,然后將其用於訪問display 在努力使它正確引用的過程中,我注意到我的指針給我的結果是我所沒有的教程/解釋/先驗知識無法解釋。

我嘗試過的一些測試的屏幕截圖。

現在,在我看來,第1行和第3行應該具有相同的值。 據我了解,第1行輸出display的地址,第2行輸出GameWorld的地址。 第3行不應該輸出GameWorld指向的display的地址嗎?

簡而言之,如何通過引用GameWorld來訪問第1行輸出的相同值?

GameWorld.cpp:

#include "GameWorld.h"
#include "Assets.h"
#include "Tile.h"
#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <iostream>
#include <array>

GameWorld::GameWorld() {}

GameWorld::~GameWorld()
{
    if (display) { al_destroy_display(display); }
    if (timer) { al_destroy_timer(timer); }
    if (queue) { al_destroy_event_queue(queue); }
}

void GameWorld::Incept()
{
    ALLEGRO_DISPLAY *display = NULL;
    ALLEGRO_TIMER *timer = NULL;
    ALLEGRO_EVENT_QUEUE *queue = NULL;

    if (!al_init()) {
    fprintf(stderr, "failed to initialize Allegro!\n");
    return;
}

if (!al_init_image_addon()) {
    fprintf(stderr, "failed to initialize image addons!\n");
    return;
}

if (!al_install_keyboard()) {
    fprintf(stderr, "failed to initialize keyboard!\n");
    return;
}

if (!al_install_mouse()) {
    fprintf(stderr, "failed to initialize mouse!\n");
    return;
}

display = al_create_display(800, 640);
if (!display) {
    fprintf(stderr, "failed to create display!\n");
    return;
}
al_clear_to_color(al_map_rgb(0, 0, 0));
al_flip_display(); 

timer = al_create_timer(1.0 / 60);
if (!timer) {
    fprintf(stderr, "failed to create timer!\n");
    al_destroy_display(display);
    return;
}

queue = al_create_event_queue();
if (!queue) {
    fprintf(stderr, "failed to create event queue!\n");
    al_destroy_display(display);
    al_destroy_timer(timer);
    return;
}

al_register_event_source(queue, al_get_display_event_source(display));
al_register_event_source(queue, al_get_timer_event_source(timer));
al_register_event_source(queue, al_get_keyboard_event_source());
al_register_event_source(queue, al_get_mouse_event_source());

Tilesheet = al_load_bitmap("Bin/Tilesheet.bmp");

fprintf(stderr, "Running smooth!\n");

/*
std::array<std::array<int, 5>, 5> Bjergen = Assets::L2;

for (std::size_t i3 = 0; i3 < Bjergen.size(); i3++){
    for (std::size_t j3 = 0; j3 < Bjergen[i3].size(); j3++){
        std::cout << Bjergen[i3][j3] << " ";
    }
    std::cout << std::endl;
}
*/

Tile T1(*this, display, 0, 0, 0);
//Tile T2(this, display, 1, 16, 0);
//Tile T3(this, display, 32, 32, 0);
std::cout << display << " actual display" << std::endl;
std::cout << this << " gameworld" << std::endl;
std::cout << this->display << std::endl;
std::cout << &(this->display) << std::endl;
std::cout << (this->display) << std::endl;
std::cout << &this->display << std::endl;

al_start_timer(timer);
bool draw = true;
bool PlayingGame = true;

while (PlayingGame){
    ALLEGRO_EVENT ev;
    al_wait_for_event(queue, &ev);
    // START EVENT LISTENING
    if (ev.type == ALLEGRO_EVENT_TIMER){
        draw = true;
    }
    else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){
        PlayingGame = false;
    }
    else if (ev.type == ALLEGRO_EVENT_KEY_DOWN){
        if (ev.keyboard.keycode == ALLEGRO_KEY_DOWN){}
    }
    else if (ev.type == ALLEGRO_EVENT_KEY_UP){}
    else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN){
        std::cout << " (" << ev.mouse.x << "," << ev.mouse.y << ")\n";
    }
    // END EVENT LISTENING
    if (draw && al_is_event_queue_empty(queue)){
        al_clear_to_color(al_map_rgb(255, 255, 255));
        T1.Draw();
        //T2.Draw();
        //T3.Draw();
        al_flip_display();
    }
}
//GAME IS ENDING
al_destroy_display(display);
al_destroy_timer(timer);
al_destroy_event_queue(queue);
}

您在GameWorld::Incept()中有一個名為display的局部變量,該局部變量隱藏GameWorld類的display成員變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM