I apologize for not having the patience of going through the entirety of
your code, but I will comment on your constructor/destructor design.
krastavica...@abv.bg wrote:
> .cpp
> [code]#include "HANGMAN.h"
>
> //Ctor and Dtor
> HANGMAN::HANGMAN(){
Since your question is about code design (not just correctness), I would
point out that, while it's ultimately a question of personal preferences
and coding conventions, it's generally customary to reserve all-caps names
for precompiler macros, and class names are usually in CamelCase (or, at
the very least, not in all-caps).
Of course the compiler itself doesn't care either way (unless there's a
name clash), but it can help other people reading your code.
Also, with regards to code correctness, you are not initializing all of
the member variables in your constructor. This is inadvisable because the
compiler is not forced to implicitly initiaize basic types (such as pointers)
and in fact most compilers won't. (This means that pointers will generally
*not* be initialized to null if you don't do it explicitly, which means they
will *not* be safe to delete, if so left uninitialized.)
If you have an instance of a class (such as std::string) as a member variable,
that doesn't need to be explicitly initialized in your constructor because
it has a constructor of its own, and that is guaranteed to be called
implicitly. (The only thing that's not guaranteed to be implicitly
initialized is basic types such as ints and pointers.)
> SetConsoleTitle ("Hangman v1.0");
> hStdout = GetStdHandle (STD_OUTPUT_HANDLE);
This is not standard C++ (unless those functions really are part of your
code, but it looks more like Windows-specific stuff) so it's really out of
the scope of this newsgroup. However, calling that code in the constructor
of a class is dubious design. That kind of code should be called at the
start of your program execution, and only once. The most usual place to
put that code in is in the main() function of your program.
> exitGame = false;
> inGame = false;
While initializing such member functions in the body of your constructor
is not wrong per se, it's usually recommended to do it with an initializer
list instead. (This makes sure that they are initialized before they are
used, and it gives better opportunities for the compiler to optimize your
code.)
> lenofFile = CountLinesOfFile ("words.txt"); //Count lines
> CreateWordsBuf (lenofFile, "words.txt"); //Allocate and store
> them
You are allocating dynamic memory at the end of a pointer in your class,
but you have neither disabled the copy constructor and copy assignment
operators, nor implemented them. This is prone to errors. If you
explicitly allocate dynamic memory like this, you should definitely do
either one of those things (ie. either disable the copy constructor and
copy assignment operators, or implement them appropriately). Leaving them
to compiler-defaults breaks your class (except in some extremely specific
situations, but this is not such a situation).
The most advisable solution to this is that if you don't really need to
explicitly allocate dynamic memory, you use one of the standard data
containers instead, usually std::string or std::vector for sequential
data. Since those classes have all of their constructors and assignment
operators well defined, your code automatically becomes correct without
you having to implement those for your class.
However, if you really must allocate the memory explicitly yourself, then
at the very least disable the copy constructor and copy assignment operator
of your class. This makes sure that it cannot be accidentally broken (because
instances of your class cannot then be copied nor assigned).
If your class must be copyable, and you absolutely must allocate the memory
explicitly yourself, then it becomes a bit complicated because then you must
implement the copy constructor and copy assignment operators for your class
to work properly, and this is not a trivial thing to do, especially for a
total beginner. If you can, avoid this for now and learn it later. (Your
code leads me to believe that this class doesn't actually need to be copied,
so the easiest way is to simply disable them.)
(Yes, this can be a bit of a drag with C++ sometimes because it's not a
garbage-collected language like most other OO languages, but it's not that
big of a deal when you get used to it.)
> srand (time(NULL)); //seed for picking element index from
> _wordsBuf
>
>
> CreditsIntro();
Again something that is dubious to do in a constructor, and looks more like
code that should be in main().