Classes and using an array with no predefined value?

Started by
4 comments, last by MARS_999 21 years, 11 months ago
Ok I am new to using classes for the most part. With code like this what can I do to let the compiler count the chars in my string?
  
#ifndef Sprite
#define Sprite

class Sprite
{
public:
	int armor;
	int speed;
	char weapon1[];
	char weapon2[];
	char weapon3[];
	char name[];

	Sprite();
	~Sprite();
	bool LoadTexture(void);
	void Draw(void);
	void Update(void);
};

#endif
  
I hope I don''t have to use a enum(MAX_STRING = 100) to define my array what a waste of memory if the string is only 10 chars or 50 chars. Thanks for the help. Bill Gates is my Pool Boy!! Nothing is to good for me!!
Advertisement
// declarationchar * weapon1;char * weapon2;char * weapon3;char * name; 

// allocationweapon1 = new char[ required_length ];// ditto others... 

// lengthint len = strlen( weapon1 );// ditto others... 

If you''re using C++, though, look into std::string, which is dynamically sized and has a much more natural syntax.

Now, go learn the language!

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
quote:Original post by Oluseyi
// declarationchar * weapon1;char * weapon2;char * weapon3;char * name;  

// allocationweapon1 = new char[ required_length ];// ditto others...  

// lengthint len = strlen( weapon1 );// ditto others...  

If you''re using C++, though, look into std::string, which is dynamically sized and has a much more natural syntax.

Now, go learn the language!

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!



Thats great but I don''t know the length of the string. Also you can''t allocate a block of memory for a string until you know how long its going to be. I am planning on loading from a file the strings information. So don''t I have to have the array already declared with a known array size? I know with C or C++ not using classes something like this should work.


  char weapon1[] = "Machine Gun";  


But in the class I posted you are declaring an array and it needs to know how much memory to set aside? or am I confused? Thanks for the help so far.


Bill Gates is my Pool Boy!!
Nothing is to good for me!!
If you can''t use C++''s std::string (for whatever reason), use a fixed size buffer. Decide on a string length that accomodates most of your strings (common choices are 64, 256 and 1024):
char weapon1[64]; 


[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
If you''re bound and determined not to use std::string, you could do something like this:
(put a number which is the size in front of each string in the file)
(sample strings in the file)
10 mystringlf
25 rocket launchers are cool

then your code could be something like this:
class myclass{private:char *mystring;public:void readmystring(istream& in){  int n;  in>>n;  mystring=new char[n];  in.read(mystring,n);}}; 

That should be about right(not completely sure about the in.read call parameter order but you get the idea)
quote:Original post by MARS_999
I hope I don''t have to use a enum(MAX_STRING = 100) to define my array what a waste of memory if the string is only 10 chars or 50 chars.

No you don''t. C programmer''s disease is for C programmers to suffer from.

[C++ FAQ Lite | ACCU | Boost | Python]

This topic is closed to new replies.

Advertisement