简体   繁体   中英

C++/Qt how to declare and initialize a global array of objects

How can I make it a global arrays of objects, like QPoint point[5][6];

global.h

#include <QPoint>
extern QPoint point[5][6];//Am I correct?

global.cpp

#include <global.h> // How to initialize???

main.cpp

#include <global.h>
use them;

Globals are evil. Alas, the idiomatic way to do it is:

  1. Declare the global variable extern in a header file.
  2. Define the variable in one source file (and no more). Include the declaration from the header to ensure the definition is correct.
  3. Use the variable in any number of source files; include the declaration from the header.

global.h

#ifndef BENHUAN_GLOBAL_H_INCLUDED
#define BENHUAN_GLOBAL_H_INCLUDED
#include <QPoint>
extern QPoint point[5][6]; // declaration
#endif

global.cpp

#include "global.h'
QPoint point[5][6]; // definition

main.cpp

#include "global.h"

...
   point[1][2] = QPoint(5,6);

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