簡體   English   中英

將對象傳遞給函數並訪問其屬性 [C++ & OpenGL)

[英]Passing object to a function and accessing its properties [C++ & OpenGL)

我使用 typedef struct 方法來聲明父級及其屬性。 但是,當我將對象的實例傳遞給另一個函數並訪問它的屬性時,盡管在最后一個函數中為它們分配了值,但我的變量(從 DrawDisc 打印出來)得到了“0”。

但是,當我在為 pos[0] 和 pos[1] 分配值后打印出 MyMouse 函數中的 pos 屬性時,我得到了正確的值。

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/freeglut.h>
#include <iostream>

using namespace std;

typedef struct discType
{
    double pos[2];          // The X and Y coordinates of the center of the disc.
    double speed[2];        // The velocity of the disc in X and Y directions. Can be negative.
    double radius;          // Radius of the disc.
    unsigned char color[3]; // R, G, B colors of the disc.
} discType;


int numDiscs = 0;                   // Number of discs that have been added.

discType disc[ MAX_NUM_OF_DISCS ];  // Array for storing discs.

bool drawWireframe = false;         // Draw polygons in wireframe if true, otherwise
                                    // otherwise polygons are filled.

int winWidth = 800;                 // Window width in pixels.
int winHeight = 600;                // Window height in pixels.

void DrawDisc(const discType *d)
{
    static bool firstTime = true;
    static double unitDiscVertex[ NUM_OF_SIDES + 1 ][2];

    if ( firstTime )
    {
        firstTime = false;
    }
   
    cout << d->pos[0] << endl;   //prints '0'
    cout << d->pos[1] << endl;   //prints '0'

}

void MyMouse(int btn, int state, int x, int y)
{
    if ( btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
    {
        if ( numDiscs >= MAX_NUM_OF_DISCS )
            printf( "Already reached maximum number of discs.\n" );
        else
        {
            discType d;

            d.pos[0] = (double)x;
            d.pos[1] = (double)y;
            
            // For testing
            cout<<d.pos[0]<<endl;
            cout<<d.pos[1]<<endl;

            DrawDisc(&d);

            numDiscs++;
            glutPostRedisplay();
        }
    }
}

這里我們有一組磁盤:

discType disc[ MAX_NUM_OF_DISCS ]; 

這里我們有一個方法可以初始化一個完全不相關的磁盤:

void MyMouse(int btn, int state, int x, int y)
{
    if ( btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
    {
        if ( numDiscs >= MAX_NUM_OF_DISCS )
            printf( "Already reached maximum number of discs.\n" );
        else
        {
            discType d; //< I am unrelated!

            /* snip */
    
            DrawDisc(&d);

            numDiscs++; //< I somehow relate to the global array?
        }
    }
}

您是否打算這樣做:

void MyMouse(int btn, int state, int x, int y)
{
    if ( btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
    {
        if ( numDiscs >= MAX_NUM_OF_DISCS )
            printf( "Already reached maximum number of discs.\n" );
        else
        {
            disc[numDiscs].pos[0] = (double)x;
            disc[numDiscs].pos[1] = (double)y;
            
            // For testing
            cout<<disc[numDiscs].pos[0]<<endl;
            cout<<disc[numDiscs].pos[1]<<endl;

            DrawDisc(&disc[numDiscs]);

            numDiscs++;
            glutPostRedisplay();
        }
    }
}

暫無
暫無

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

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