| | |
Number of Variables in Constructor
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
I have a question that pertains to any programming language but since C/C++ is my primary language I'll ask it here.
I'm designing a test program that will simulate the outcome of a 12 round fight between 2 boxers. Now my question is, is there a limit or number I should shoot for in regards to the number of member variables a class should have?
Since my objects are representing people, very specific types of people, I seem to have a large number of member variables. These represent everything from first and last name to strength, stamina, speed, agility etc.
The reason I ask this, is because my constructor is very large and I have like 40 variables in the constructor initialization list. I'm just wondered if this is a common occurrence in games or in other large applications? Or is my class too bloated?
I'm doing everything all my books have told me too do as far as breaking down the parts of a person into smaller logical parts, but none of the books I have read so far delve into what to do with classes that have this many members. I just wanted to know if I'm missing some fundamental concept I should be using to address the growing number of members my classes have.
Any help or opinions would be great!
Thx
I'm designing a test program that will simulate the outcome of a 12 round fight between 2 boxers. Now my question is, is there a limit or number I should shoot for in regards to the number of member variables a class should have?
Since my objects are representing people, very specific types of people, I seem to have a large number of member variables. These represent everything from first and last name to strength, stamina, speed, agility etc.
The reason I ask this, is because my constructor is very large and I have like 40 variables in the constructor initialization list. I'm just wondered if this is a common occurrence in games or in other large applications? Or is my class too bloated?
I'm doing everything all my books have told me too do as far as breaking down the parts of a person into smaller logical parts, but none of the books I have read so far delve into what to do with classes that have this many members. I just wanted to know if I'm missing some fundamental concept I should be using to address the growing number of members my classes have.
Any help or opinions would be great!
Thx
•
•
•
•
The reason I ask this, is because my constructor is very large and I have like 40 variables in the constructor initialization list. I'm just wondered if this is a common occurrence in games or in other large applications? Or is my class too bloated?
C++ Syntax (Toggle Plain Text)
class Human { public: Human::Human(std::string name, std::string last_name, int age); private: std::string name; std::string last_name; int age; //etc }; class Skills { public: Skills::Skills(int att, int defense) ; private: int attack; int defense; //etc }; class Boxer { public: Boxer(Human* hum, Skills* sk); private: Human* human; Skills* skills };
niek_e
Thank You for your response.
yes, I am currently inheriting my specialized persons classes from a more general human class, but my skills class and my attributes class are still really large. And your response brings about another question for me, should my skills class and my attributes class be nested within the more specialized boxer class or should I just use pointers in the constructor to point to them as you did?
Thx again
Thank You for your response.
yes, I am currently inheriting my specialized persons classes from a more general human class, but my skills class and my attributes class are still really large. And your response brings about another question for me, should my skills class and my attributes class be nested within the more specialized boxer class or should I just use pointers in the constructor to point to them as you did?
Thx again
Ahh I see why I shouldn't nest them inside the CBoxer class, because if I ever wanted to add another specialized class like CKarateMaster, I would have to nest the attributes class and the stats class within each specialized class instead of just pointing to it in the constructor. Your way future proofs it from needless over-typing.
![]() |
Similar Threads
- Too Many Variables? (C)
- Input from a default constructor (C++)
- Complex Number Class (C++)
- class pointing to a string (C++)
- Using mysqli_bind_param with an unknown number of variables (PHP)
- Function for assigning variables (Python)
Other Threads in the C++ Forum
- Previous Thread: passing values between dialog boxes
- Next Thread: C++ random between two choices ?
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






