Am I using a constructor correctly?

Started by
16 comments, last by SonicD007 17 years, 6 months ago
Quote:Original post by SonicD007
Ohhhh. Thats cool. I never even heard of that until now. Also, the get and set functions were for the program to use to give a character power levels and all that. If that's bad coding please point me in a direction on how to improve it because I don't want to get any bad habbits.


It is, generally speaking, bad coding, and I already told you that. More than once. A while ago. The lowercase c prefix on the class name is quite useless, too.

Please pay close attention:

class Enemy {  // I prefer to put the private stuff at the top, when possible.  // That's probably unusual, though.  std::string name;  int power_level;  int max_power_level;  int attack;  int defense;  int ki;  int max_ki;  int level;  int zenni;  public:  // A constructor is a function, so the usual rules for parameter passing  // apply: i.e., prefer to pass non-primitives by const reference.  // I would normally put a constructor this straightforward directly into  // the class body, but here I'll separate it out so you see how it's done.  Enemy(const std::string& name, int PL, int atk, int def, int ki, int lvl, int zenni);};Enemy::Enemy(const std::string& name, int PL, int atk, int def, int ki, int lvl, int zenni) :  // Initializer list goes between the colon and the open brace.  // Names outside the parens are the names of members as they were declared  // in your class. Stuff inside the parens are the expressions that will  // initialize those members. In your case, those will just be parameters.  name(name), power_level(PL), max_power_level(PL), attack(atk), defense(def),  ki(ki), max_ki(ki), level(lvl), zenni(zenni)// Constructor body can now be empty.{}



Advertisement
Quote:Original post by SimonForsman
class IntArray{	private:		int m_size;		int * m_data;		// disable copy-constructor and copy-assignment		// operator unless you're goint to implement		// them properly		IntArray(const IntArray &);		IntArray & operator=(const IntArray &);	public:		IntArray(int size)			:			m_size(size)		{			m_data = new int[m_size];		}		~IntArray()		{			delete[] m_data; //free up the used memory		}};

Which is a good example of why you should avoid manual memory management whenever possible (it's too easy to make mistakes) and use SC++L constructs instead.

Σnigma
I agree with above posts, but I use different code notation.
I would suggest that you dont initialize class members in construstor, but instead use it for inclass initialization, like allocation memory or some initiate some private math or something, and for setting class values use some Init func.
Example would be something like this
cLandLord::cLandLord(int type, int slave_count){ type_ = type; this->slaves= new cServant[slave_count]}cLandLord::InitUnit(int hp, int mana, ...){ hitPoints_ = hp; mana_ = mana;}


offtopic
whitch are code tags here?
Quote:Original post by Enigma
Quote:Original post by SimonForsman
class IntArray{	private:		int m_size;		int * m_data;		// disable copy-constructor and copy-assignment		// operator unless you're goint to implement		// them properly		IntArray(const IntArray &);		IntArray & operator=(const IntArray &);	public:		IntArray(int size)			:			m_size(size)		{			m_data = new int[m_size];		}		~IntArray()		{			delete[] m_data; //free up the used memory		}};

Which is a good example of why you should avoid manual memory management whenever possible (it's too easy to make mistakes) and use SC++L constructs instead.

Σnigma


you are 100% correct, i really shouldn't post sloppy code in the beginners forum. (im just too lazy sometimes). my only excuse is that it was ~4:30 am in the morning when i wrote it.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Quote:Original post by Odiee
I agree with above posts, but I use different code notation.
I would suggest that you dont initialize class members in construstor, but instead use it for inclass initialization, like allocation memory or some initiate some private math or something, and for setting class values use some Init func.
Example would be something like this
cLandLord::cLandLord(int type, int slave_count){ type_ = type; this->slaves= new cServant[slave_count]}cLandLord::InitUnit(int hp, int mana, ...){ hitPoints_ = hp; mana_ = mana;}


offtopic
whitch are code tags here?


IMHO, that violates the principle that the class should always be in a well-defined state. If you were to access the mana_ member (for example) after the object constructor but before you had called InitUnit, it would contain random data (assuming the object was on the stack or had been created with new).

I'm not saying a seperate Init function is bad per se, but you should at least be assigning default values to hitPoints_ and mana_ in the constructor in case they are accessed before InitUnit is called.

Even just calling InitUnit(0,0); at the end of the constructor would be an improvement.
Quote:Original post by EasilyConfused
IMHO, that violates the principle that the class should always be in a well-defined state. If you were to access the mana_ member (for example) after the object constructor but before you had called InitUnit, it would contain random data (assuming the object was on the stack or had been created with new).

I'm not saying a seperate Init function is bad per se, but you should at least be assigning default values to hitPoints_ and mana_ in the constructor in case they are accessed before InitUnit is called.

Even just calling InitUnit(0,0); at the end of the constructor would be an improvement.

I assumed that was obvious. But of course, In using this kind of notation, one should allways set default values of current class in it's constructor.
thanks for pointing this out.

offtopic:
still no word of code tags?
Quote:Original post by Odiee
I assumed that was obvious. But of course, In using this kind of notation, one should allways set default values of current class in it's constructor.
thanks for pointing this out.


Sorry. I'm a bit wary of "obvious" in the beginners forums [smile].

Quote:Original post by Odiee
offtopic:
still no word of code tags?


[_source_] [_/source_] but without the underscores for the scrollable box. It's all in the forum FAQs if you need more info on formatting posts.
@ Zalhman. Don't worry, I still remember what you said. This is still the same game that I was working on back then. I plan on rewriting my classes as I get better. I might fix the class prefix name now though just so if I have another problem that I need to show the class name, I won't make you think I'm not listening :)

Aside from that part, The reason I'm still working on the damn chicken text game is I spent a lot of time trying to get the saving and loading working but I haven't been able to do it so I decided to try and do something else on it for once.

@Adam Hamilton. Yea the P_* variables are in the Player class. They are protected. Enemy is derived from Player.

(in case your wondering, loading a file containing the names of other saved files is my issue)

This topic is closed to new replies.

Advertisement