|
What you have posted is no different from having globally accessible functions in a namespace and global variable declarations in a 'private' header that is only included by your game logic, except it is more confusing and ugly. There's no reason to structure your code that way at all. Why does there need to be a Window class if I am never going to instantiate it? Edit: beaten I guess
|
# ? Mar 17, 2010 16:02 |
|
|
# ? Jun 9, 2024 00:35 |
|
Optimus Prime Ribs posted:So the way that I decided to do it is like this: As was pointed out, you could just use a namespace instead of a nested class, but there's something I consider a little worse here -- a good thing to consider when working in C++ is to avoid having named "create" and "destroy" functions for just about anything and instead prefer constructors and destructors. Take your window example for instance, if instead of calling window::create and window::destroy you just put that code in a constructor and destructor for window, your function body simplifies to: code:
The alternative, which was also suggested by some, would be to just change everything to not use classes at all, but I argue that you're not gaining anything by that other than grouping functions together. Even if you only ever plan on making one instance, the abstraction and additional functionality of making it a class with proper semantics generally wins out and it doesn't really take much if any more effort. Just make it non-copyable until you have a use-case.
|
# ? Mar 17, 2010 16:56 |
|
Well I guess I'll definitely have to switch over my classes to namespaces then. I'll never be making instances of the class.The Turkey Story posted:but there's something I consider a little worse here -- a good thing to consider when working in C++ is to avoid having named "create" and "destroy" functions for just about anything and instead prefer constructors and destructors. I get what your saying, but the code in that function is what actually makes the window appear onscreen. It just made more sense to me for that to me called by a method, rather than the constructor. That and I didn't intend on making an instance of it. Thanks for the help, guys.
|
# ? Mar 18, 2010 00:55 |
|
Optimus Prime Ribs posted:I get what your saying, but the code in that function is what actually makes the window appear onscreen. It just made more sense to me for that to me called by a method, rather than the constructor. A constructor is a method. You're really just trading safety (i.e. automatic cleanup of your window) for syntax that doesn't even look much different. Optimus Prime Ribs posted:That and I didn't intend on making an instance of it. Then why are you making a class for it?
|
# ? Mar 18, 2010 01:02 |
|
Optimus Prime Ribs posted:I get what your saying, but the code in that function is what actually makes the window appear onscreen. It just made more sense to me for that to me called by a method, rather than the constructor. You're making a window and destroying it -- that's instantiating the notion of a window. Putting the functionality into a class as a constructor, destructor, and member functions makes perfect sense, is simpler and safer.
|
# ? Mar 18, 2010 01:06 |
|
Avenging Dentist posted:Then why are you making a class for it? That was my original mistake and I'm not using classes anymore.
|
# ? Mar 18, 2010 01:14 |
|
You are calling create and destroy, how is this not making an instance? You may not see right now why this is equivalent, but it is. You're only avoiding the right tool for the job by not using constructors and destructors.
|
# ? Mar 18, 2010 01:19 |
|
That Turkey Story posted:You are calling create and destroy, how is this not making an instance? You may not see right now why this is equivalent, but it is. You're only avoiding the right tool for the job by not using constructors and destructors. Because the create() and destroy() functions inside the ::Window namespace don't contain the actual window code. I want my engine to be cross-platform. So depending on which OS it's on, Window::create() will make a Windows/Linux/Mac/Whatever window. Or is that a stupid way to go about it?
|
# ? Mar 18, 2010 01:28 |
|
Optimus Prime Ribs posted:Because the create() and destroy() functions inside the ::Window namespace don't contain the actual window code. I don't see why any of this matters. The Window's functions you expose to the rest of your program can be anything but their implementation can differ depending on the platform used. I don't really understand why having a function in namespace or class or as constructor or destructor here would make any difference as far as cross-platform goes because inside your exposed functions you could have checks for specific platform using macros or whatever is a good way of doing this, and specific platform Dependant code could be just private function somewhere. However the way your library functions are exposed to the user shouldn't really matter as far as making things cross-platform.
|
# ? Mar 18, 2010 01:33 |
|
Personally I think you should focus on learning how to write well-designed code intended for a single platform before you delve into the #ifdef hell and otherwise coding nightmare that is cross-platform development.
|
# ? Mar 18, 2010 02:44 |
|
I have a friend who works for CNN and has a background using FORTRAN for weather modeling. She's looking to take on the responsibiliy of developing the "magic walls" they use on CNN, which is written in C++. She'd like to familiarize herself with the language in short order, but the only book I can think of is C++ in 21 days. Any other suggestions?
|
# ? Mar 18, 2010 02:45 |
|
Josh Lyman posted:I have a friend who works for CNN and has a background using FORTRAN for weather modeling. She's looking to take on the responsibiliy of developing the "magic walls" they use on CNN, which is written in C++. She'd like to familiarize herself with the language in short order, but the only book I can think of is C++ in 21 days. Any other suggestions? DONNA! Get me the first post on line two! The first post in this thread posted:Good Books: Accelerated C++ is the ideal book to learn from; the Stroustrup is the ideal language reference, though with C++0x about ready for prime time I don't know how good of an investment it'll be in the long term. edit: also, why would you recommend any of the foo in 21 days books? Dijkstracula fucked around with this message at 02:51 on Mar 18, 2010 |
# ? Mar 18, 2010 02:47 |
|
Dijkstracula posted:Accelerated C++ is the ideal book to learn from; the Stroustrup is the ideal language reference, though with C++0x about ready for prime time I don't know how good of an investment it'll be in the long term. A good 80% of C++0x features aren't really meant for a person who needs Accelerated C++.
|
# ? Mar 18, 2010 02:53 |
|
Avenging Dentist posted:A good 80% of C++0x features aren't really meant for a person who needs Accelerated C++.
|
# ? Mar 18, 2010 03:01 |
|
Dijkstracula posted:DONNA! Get me the first post on line two!
|
# ? Mar 18, 2010 03:20 |
|
Dijkstracula posted:Accelerated C++ is the ideal book to learn from; the Stroustrup is the ideal language reference, though with C++0x about ready for prime time I don't know how good of an investment it'll be in the long term.
|
# ? Mar 18, 2010 03:30 |
|
Josh Lyman posted:I didn't know if any of the books in the OP would work on a short timetable They won't because nothing will. You could learn enough to muddle through things in a few weeks, but learning to program well takes years, and Fortran isn't exactly the best place to start (especially not Fortran 77 or something). Josh Lyman posted:and I haven't used any of the foo in 21 days books so I don't know if they're good or bad. They're among the worst books in the world.
|
# ? Mar 18, 2010 03:37 |
|
Yeah, C++ is likely the worst of all possible languages to try to "pick up the jist of quickly" just because it's so rich. I'll let Mister Norvig explain why even the general case is bad. And, as AD said, there is no good substitute for learning the material properly, even if you're on a supposedly tight schedule (for examples of what happens when this is ignored, I direct you to the Coding Horrors thread)
|
# ? Mar 18, 2010 04:05 |
|
Dijkstracula posted:I'll let Mister Norvig explain why even the general case is bad. This is one of the all-time best programming articles and makes me want to post a thread for the classics of the field.
|
# ? Mar 18, 2010 04:12 |
|
Avenging Dentist posted:They're among the worst books in the world. Wait, why are these books so bad? I thought that as a beginner to C++ it was a good way to keep me focused and not completely lost since their explanations are simple and mundane unlike the C++ book by the dude who wrote C++ which is super word heavy.
|
# ? Mar 18, 2010 07:50 |
|
An Outland Dish posted:...their explanations are simple and mundane... ...and wrong. quote:...unlike the C++ book by the dude who wrote C++ which is super word heavy... ...but accurate.
|
# ? Mar 18, 2010 08:04 |
|
floWenoL posted:...and wrong. Point taken. I'm kind of scared now because I learned basic stuff using the 21 day C++ book thing. I wonder how corrupted I am? Also, I have a question about using structures in C++. Well, that is the question. I know they are not really supported and class is a more updated version, but I always felt that when I needed a public only class, I might as well use a struct since it seems to work the same way. Or am I missing some kind of overhead?
|
# ? Mar 18, 2010 08:10 |
|
An Outland Dish posted:Also, I have a question about using structures in C++. Well, that is the question. I know they are not really supported and class is a more updated version, but I always felt that when I needed a public only class, I might as well use a struct since it seems to work the same way. Or am I missing some kind of overhead? In C++, structs are exactly like classes except the default visibility is 'public' instead of 'private'. That's it.
|
# ? Mar 18, 2010 08:11 |
|
floWenoL posted:In C++, structs are exactly like classes except the default visibility is 'public' instead of 'private'. That's it. That's what I thought. I had a teacher tell me to use a class where I declare everything in public because "class is a C++ thing and struct is a C thing" once before.
|
# ? Mar 18, 2010 08:15 |
|
floWenoL posted:In C++, structs are exactly like classes except the default visibility is 'public' instead of 'private'. That's it. Is the layout in memory same as a struct for a class with both public and private members? That is, if I am reading a class from a file or char buffer or something, can I cast that char buffer to be pointer to class and then have a working instance of that class through that pointer?
|
# ? Mar 18, 2010 08:40 |
|
LockeNess Monster posted:Is the layout in memory same as a struct for a class with both public and private members? That is, if I am reading a class from a file or char buffer or something, can I cast that char buffer to be pointer to class and then have a working instance of that class through that pointer? Yes, but you don't want to do that for other reasons.
|
# ? Mar 18, 2010 08:48 |
|
LockeNess Monster posted:Is the layout in memory same as a struct for a class with both public and private members? That is, if I am reading a class from a file or char buffer or something, can I cast that char buffer to be pointer to class and then have a working instance of that class through that pointer? Yes. No, but the best chance of it working would be when you have POD class.
|
# ? Mar 18, 2010 09:05 |
|
LockeNess Monster posted:Is the layout in memory same as a struct for a class with both public and private members? That is, if I am reading a class from a file or char buffer or something, can I cast that char buffer to be pointer to class and then have a working instance of that class through that pointer? You really should be using a serialization library for something like that, unless you're a fan of endianness issues, security vulnerabilities, and weirdness with inheritance (esp. with polymorphic types).
|
# ? Mar 18, 2010 09:21 |
|
i need a sweet compiler for windows, i normally use linux but i need to make some exe's plaese halp
|
# ? Mar 18, 2010 11:44 |
|
The reason I was asking is because I like to cast char buffers to some struct in C for doing networking or reading binary files in, when I know exactly the layout of the incoming data and dealing with endianess is not too bad. I can just keep doing same thing in C++ program, just try not to use classes for it just structs that do not use any inheritance or templates? Or what is the "orthodox" way of reading in a packet or binary file with a fixed layout?
|
# ? Mar 18, 2010 16:14 |
|
Wit_Sponge posted:i need a sweet compiler for windows, i normally use linux but i need to make some exe's or this gets you full MSVC9: http://www.microsoft.com/downloads/details.aspx?FamilyID=c17ba869-9671-4330-a63e-1fd44e0e2505&displaylang=en works pretty nice for me because I have to share code with Visual Studio folks.
|
# ? Mar 18, 2010 17:53 |
|
Is it possible for the same variable to occupy two different locations in memory and if so how would that sort of situation come about? I ask because I've been trying to troubleshoot a program that's been displaying some bizarre behavior by outputting a variable and its address every iteration of a loop it's supposed to be changing. While reading the output I noticed the address seemed to be in two places--once at address 0x3D27C4 and then again at 0x3D2584. Which instance got changed during the loop appeared to be more or less a crapshoot. If it makes any difference this particular variable is a data member of a derived class. (which is also present in the parent class) It's just a plain old int, nothing fancy like a pointer or an object or anything else.
|
# ? Mar 18, 2010 18:00 |
|
Genpei Turtle posted:Is it possible for the same variable to occupy two different locations in memory and if so how would that sort of situation come about?
|
# ? Mar 18, 2010 18:13 |
|
Genpei Turtle posted:Is it possible for the same variable to occupy two different locations in memory and if so how would that sort of situation come about? Sounds like it's two different objects.
|
# ? Mar 18, 2010 18:28 |
|
OddObserver posted:Sounds like it's two different objects. ..and indeed it is. I commented out a lot of the initialization code for the second object to troubleshoot this, but not the instantiation of the object itself. Well, now I feel dumb. At least I know the problem's with something stupid I'm doing with my code instead of some sort of arcane memory issue.
|
# ? Mar 18, 2010 19:57 |
|
Can I address-of into a struct? e.g.code:
|
# ? Mar 19, 2010 00:37 |
|
Yes
|
# ? Mar 19, 2010 00:42 |
|
sund posted:mingw probably be best thanks mate, i ended up using i ended up using devC++ which uses mingw anyway i think
|
# ? Mar 19, 2010 02:04 |
|
Wit_Sponge posted:thanks mate, i ended up using i ended up using devC++ DevC++ is just about the worst IDE in the world. There's really almost no reason to use anything but Visual Studio on Windows.
|
# ? Mar 19, 2010 02:18 |
|
|
# ? Jun 9, 2024 00:35 |
|
Avenging Dentist posted:DevC++ is just about the worst IDE in the world. There's really almost no reason to use anything but Visual Studio on Windows. I don't really understand all the hate for DevC++. Sure it freezes occasionally, but it is a lot simpler and more straightforward to use and learn how to use than Visual Studio. It is pretty easy to set up C++ projects in it and have them compile and run fine. The only thing missing is good code formatter and not freezing.
|
# ? Mar 19, 2010 02:33 |