Jump to content
  • Create New...
  • 0

initialization of classes/structs



Whats the correct way to initialize structs/classes?

I dont want to use memset, so I did it that way:

class ClientHandleInfo
		DWORD	dwHandle = 0;
		DWORD	account_id = 0;
		DWORD	player_id = 0;
		BYTE	account_index = 0;
		char	login[LOGIN_MAX_LEN + 1] = "";
		char	safebox_password[SAFEBOX_PASSWORD_MAX_LEN + 1] = "";
		char	ip[MAX_HOST_LENGTH + 1] = "";


class ClientHandleInfo
		DWORD	dwHandle;
		DWORD	account_id;
		DWORD	player_id;
		BYTE	account_index;
		char	login[LOGIN_MAX_LEN + 1];
		char	safebox_password[SAFEBOX_PASSWORD_MAX_LEN + 1];
		char	ip[MAX_HOST_LENGTH + 1];

I'm just using "" instead of "/0" because the +1 should do it already, correct me if i'm wrong, I'm learning by doing I never learned C++


Whats the correct initialize for a struct like that? Can someone show me a example?

typedef struct SShopItemTable
	DWORD		vnum = 0;
	WORD		count = 0;
    TItemPos	pos;
	long long	price = 0;
	BYTE		display_pos = 0;
	long		alSockets[ITEM_SOCKET_MAX_NUM];
	TPlayerItemAttribute	aAttr[ITEM_ATTRIBUTE_MAX_NUM];
	DWORD 		price_type = 1;
	DWORD		price_vnum = 0;
	SShopItemTable() {
		memset(&alSockets, 0, sizeof(alSockets));
		memset(&aAttr, 0, sizeof(aAttr));
} TShopItemTable;



Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

If you wanna initialize some class members with default values I think the best way is to use constructors.


So for the SShopItemTable you can do something like this:


        vnum = 0;
        count = 0;
        pos = TItemPos();
        price = 0;


What this will do is when a new SShopItemTable object is created it will initialize it with these values.



  • Love 1
Link to comment
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

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.