1) Why is the right selector always an empty string? What's the point of passing a parameters to set it in your constructor/Selector functions if the parameter goes unused? |
Because I was going to skip it until I was finished. Has been changed since I posted the code though.
2) Your logic in the Options function is backwards: |
Lol I noticed last night when I started working on it again. Fixed as well.
Also, continuously push_back'ing a value is a bad way to resize a vector. It would make much more sense to simply call resize(). This function can be simplified greatly: |
I knew about resize(), but I was really tired and making programming hard. Code fixed.
3) You can't put function bodies in header files like that unless they are inlined or templated. The functions inside the class are okay (if you give the body inside the class, it is implicitly inlined), but that last Options function will cause problems if this header file is #included in multiple cpp files. Move that function body to a .cpp file -- or make it inline. |
I never knew this, can you explain into more detail as to why functions can't be in the header? I never had compile errors or warnings that I was aware of and I just made a header file that has functions and still no warnings. or is it just bad practice?
4) vector is in the std namespace. I'm not sure how this code is compiling for you, but you should be getting an error saying that vector is undefined. You should have
std::vector<const char*> vMenuOptions; |
I'm not sure why either. But fixed regardless.
5) Consider using strings instead of char pointers. This way you avoid memory ownership issues. |
I like the idea of the char pointers. I find char* is easier to work with in this situation, but I will get back to changing them, gives me something else to learn about.
1) avoid using #define to define constants. Really, avoid #define as much as possible. The only thing you'll really need it for (usually) is the header guard. #defines ignore language scope rules and pollute the namespace, making them error prone. Constants should be declared with the const keyword... or possibly with an enum: |
I haven't changed it yet, but that is definitely something I will do. I didn't use enums much in school bc I saw no point, but I will definitely get that changed.
2) I'm not a fan of hungarian notation. There's no need to prefix the type of the variable in the name. It doesn't really do anything to benefit you and just makes maintanance harder (what if you decide to change the type later? Do you go back and rename the variable in all the spots you used it?) |
I like the idea of naming the variables after what it is, and I think it's easier for me to name them this way. I was taught it in high school, and I have always found it hard to break, especially seeing a lot of windows api books referring to it. And yes, I actually do search/replace when I change my variable types. I'm OCD about that kind of stuff. I also know it takes a few more keystrokes, but with autocomplete, it's not a big deal. I've also seen the arguements about it and nothing applies to me yet.
3) Also, "Option" could probably be changed to a more descriptive name. I would probably call them "AddOption" and "SetOption", as that is more descriptive of what they actually do. |
Again, I was tired and anxious to see feed back so I posted without thinking about my functions first. Fixed.
I appreciate the feedback, and you have given me a bunch of things to look into once I'm satisfied with how far I've gotten on this and some other files. I'm going to post my code again, and I know I'm going to hear about the system() functions, but they're purely designed for a windows environment at the moment. I've seen Duoas getting frustrated with others, and I will hopefully get around to learning about other OS's, in the meantime, the code works for me, and I believe a majority of other windows users.
Main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
#include "menu.h"
#include "header.h"
int main() {
VP_SetCursor(false);
Menu MainMenu("->","<-");
MainMenu.AddOption("Single Player");
MainMenu.AddOption("MultiPlayer");
MainMenu.AddOption("Exit");
while(MainMenu.Play() != 3) {};
return 0;
}
| |
Menu.cpp
http://codepad.org/iNCVzUZj
Header.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#ifndef _VP_HEADER_H_
#define _VP_HEADER_H_
#include <windows.h>
#include <conio.h>
void VP_SetCursor(bool bCursor) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO curCursorInfo;
curCursorInfo.bVisible = (bCursor ? TRUE : FALSE);
curCursorInfo.dwSize = 1;
SetConsoleCursorInfo(hConsole, &curCursorInfo);
}
void VP_GoToXY(int x, int y) {
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
CursorPosition.X = x;
CursorPosition.Y = y;
SetConsoleCursorPosition(console, CursorPosition);
}
void VP_ClearScreen() { system("cls"); }
#endif
| |
Any more opinions on either the new or old code? It works well, and I always try to make it dummy proof, but any improvements you can see or any other criticism would be greatly appreciated. Thanks again for all of the help and pointers.