Jump to content

initialization of classes/structs


Recommended Posts

Whats the correct way to initialize structs/classes?

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

class ClientHandleInfo
	{
	    public:
		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] = "";

original:

class ClientHandleInfo
	{
	    public:
		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

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:

 

    SShopItemTable()
    {
        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

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

Announcements



  • Similar Content

  • Activity

    1. 8

      ZycloniusMT2 Server from Australia still around

    2. 60

      Inbuild GR2 Animation

    3. 2

      wait() function bug

    4. 0

      Remove Party Role Bonuses

    5. 1

      Fix CBar3D

    6. 2

      set_quest_state not working

  • Recently Browsing

    • No registered users viewing this page.
×
×
  • Create New...

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.