C++ 在main()之前的单独文件中初始化/准备类对象
简单地说,我正在尝试在一个.h或.cpp文件中准备纹理和其他半常量对象,以便它们可以在其他文件中使用。我希望能够做到以下几点:C++ 在main()之前的单独文件中初始化/准备类对象,c++,static,extern,C++,Static,Extern,简单地说,我正在尝试在一个.h或.cpp文件中准备纹理和其他半常量对象,以便它们可以在其他文件中使用。我希望能够做到以下几点: class Position { private: int x; int y; public: /* constructor, destructor, and other code */ void setPosition(int x, int y) { this->x = x;
class Position
{
private:
int x;
int y;
public:
/* constructor, destructor, and other code */
void setPosition(int x, int y) { this->x = x;
this->y = y; }
};
//otherfile.extension
// I want to be able to declare these objects here...
Position specialPosition1;
Position specialPosition2;
// ...and then be able to do this somewhere where it will keep
// this information for any other file that includes this.
specialPosition1.setPosition(25, 25);
specialPosition2.setPosition(-10, -10);
然后,在另一个文件中,有如下内容:
class Position
{
private:
int x;
int y;
public:
/* constructor, destructor, and other code */
void setPosition(int x, int y) { this->x = x;
this->y = y; }
};
//otherfile.extension
// I want to be able to declare these objects here...
Position specialPosition1;
Position specialPosition2;
// ...and then be able to do this somewhere where it will keep
// this information for any other file that includes this.
specialPosition1.setPosition(25, 25);
specialPosition2.setPosition(-10, -10);
我希望能够调用它们的setPosition()
方法,并在同一个文件中准备它们,以便在其他文件中使用(如果可能)。或者至少能够设置它,使信息在其他地方使用之前存在
如果我记得的话,让它们保持静态并不能解决这个问题;另外,我仍然没有(已知)地方调用setPosition
来准备对象。我也读了一些关于外部的东西,虽然我对它的了解还很模糊
如何才能在
main()
之前准备这些对象,或者更准确地说,在其他文件中使用这些对象之前,准备这些对象的“最佳”方法是什么?我认为您根本不想调用设置位置。我认为最好在构造函数中初始化它们。我假设这些特殊位置是恒定的
我想您应该在.h文件中将它们声明为extern
,然后在.cpp中定义它们:
位置。h:
struct Position {
int x;
int y;
Position(int _x, int _y) : x(_x), y(_y) {}
//...
};
#include "Position.h"
extern const Position specialPosition1;
extern const Position specialPosition2;
#include "SpecialPositions.h"
const Position specialPosition1{25, 25};
const Position specialPosition2{-10, -10};
#include "SpecialPositions.h"
#include <iostream>
int main() {
std::cout << specialPosition1.x << "\n";
}
特殊位置。h:
struct Position {
int x;
int y;
Position(int _x, int _y) : x(_x), y(_y) {}
//...
};
#include "Position.h"
extern const Position specialPosition1;
extern const Position specialPosition2;
#include "SpecialPositions.h"
const Position specialPosition1{25, 25};
const Position specialPosition2{-10, -10};
#include "SpecialPositions.h"
#include <iostream>
int main() {
std::cout << specialPosition1.x << "\n";
}
SpecialPositions.cpp:
struct Position {
int x;
int y;
Position(int _x, int _y) : x(_x), y(_y) {}
//...
};
#include "Position.h"
extern const Position specialPosition1;
extern const Position specialPosition2;
#include "SpecialPositions.h"
const Position specialPosition1{25, 25};
const Position specialPosition2{-10, -10};
#include "SpecialPositions.h"
#include <iostream>
int main() {
std::cout << specialPosition1.x << "\n";
}
Main.cpp:
struct Position {
int x;
int y;
Position(int _x, int _y) : x(_x), y(_y) {}
//...
};
#include "Position.h"
extern const Position specialPosition1;
extern const Position specialPosition2;
#include "SpecialPositions.h"
const Position specialPosition1{25, 25};
const Position specialPosition2{-10, -10};
#include "SpecialPositions.h"
#include <iostream>
int main() {
std::cout << specialPosition1.x << "\n";
}
然后在SpecialPositions.h文件中定义它们(不需要SpecialPositions.cpp):
这正是我需要知道的。我对extern之类的东西略知一二,但我真的不知道如果一个对象是const,你可以调用其他构造函数,尽管这感觉应该是常识。但是,是否有理由使用大括号而不是圆括号?查找时,我看到的大多数示例都使用括号。例如,@Uulamock在C++11中是新出现的,constexpr
构造函数并不意味着对象必须是const,只要您愿意,它可以在编译时构造。我没有特别好的理由使用大括号而不是圆括号。它在C++11中被称为,也是新的,在某些上下文中有一些小的优势。