Sign in to follow this  
jonathanplumb

Issues initializing static members of a class

Recommended Posts

I've tried googling the answer, but it seems like all the answers I find don't exactly either make sense or something, could somebody please explain this to me. I am trying to make a definitions class, so that I have static const data that I can use throughout my program. Here is my test code that I can't get to work:
#include <windows.h>
#include <string.h>

#define SYS_VERSION			"0.01a"
#define SYS_CLASS			"DAClass"
#define SYS_TITLE			"Test App"

#define DEFAULT_WINDOWW		1024
#define DEFAULT_WINDOWH		768
#define DEFAULT_WINDOWX		GetSystemMetrics(SM_CXSCREEN)/2-DEFAULT_WINDOWW/2
#define DEFAULT_WINDOWY		GetSystemMetrics(SM_CYSCREEN)/2-DEFAULT_WINDOWH/2
#define DEFAULT_FULLSCREEN	         false

class Definitions
{
public:
	static const char System_Version[strlen(SYS_VERSION)] = SYS_VERSION;
	static const char System_Class[strlen(SYS_CLASS)] = SYS_CLASS;
	static const char System_Title[strlen(SYS_TITLE)] = SYS_TITLE;
	
	static const int Default_WindowWidth = DEFAULT_WINDOWW;
	static const int Default_WindowHeight = DEFAULT_WINDOWH;
	static const int Default_WindowX = DEFAULT_WINDOWX;
	static const int Default_WindowY = DEFAULT_WINDOWY;

	static const bool Fullscreen = DEFAULT_FULLSCREEN;
};
This gives me the following error:
Error	1	error C2864: 'Definitions::System_Version' : only static const integral data members can be initialized within a class ...
I know I could just use the #defines throughout, but I'm trying to take advantage of Intellisense to make life a bit easier when coding. I don't quite understand what this error means and I have had no luck finding an answer online. I'm mostly a self-taught programmer, so there's a lot of "key terms" etc that go above my head still, like "integral" ... no clue. If anyone can explain how to fix this or an alternative to what I'm trying to do, please let me know. Thanks.

Share this post


Link to post
Share on other sites
(1) There is no reason in C++ to make this a class. The proper organizational tool here is a namespace.

(2) The error means exactly what it says: those members can't be initialized within the class. They can be declared there, and then initialized outside the class. Not that it matters, because we're going to use a namespace.

(3) There is no point in using #define constants that are only supposed to be used once in order to set up real constants. It gains nothing in terms of maintainability (you could just fix the real constants directly) and has definite costs (in terms of name collision).


namespace Definitions {
// By the way, you don't gain anything from going through hoops to declare
// constant string literals as char[] instead of char*.
const char* const System_Version = "0.01a";
const char* const System_Class = "DAClass";
const char* const System_Title = "Test App";

const int Default_WindowWidth = 1024;
const int Default_WindowHeight = 768;
const int Default_WindowX = GetSystemMetrics(SM_CXSCREEN) / 2 - DEFAULT_WINDOWW / 2;
const int Default_WindowY = GetSystemMetrics(SM_CYSCREEN) / 2 - DEFAULT_WINDOWH / 2;

const bool Fullscreen = false;
}


Boom. Done.

Quote:
Original post by jonathanplumb
After more experimenting, I solved my own problem. I just had to create a constructor that initialized all the variables there instead of directly in the class body.


This is a horrible abuse of classes, requiring you to instantiate a useless object.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this