Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
The1ManMoshPit
Apr 17, 2005

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

Adbot
ADBOT LOVES YOU

That Turkey Story
Mar 30, 2003

Optimus Prime Ribs posted:

So the way that I decided to do it is like this:

code:
#ifndef _COOLENGINE_H_
#define _COOLENGINE_H_

class CoolEngine
{
private:
	//bunch of variables for the engine
public:
	class Window
	{
	public:
		static void create();
		static void destroy();
	};

	class Input
	{
	public:
		static void setup();
		static void checkKeysDown();
	};
};

#endif

//
//
//

int main()
{
	CoolEngine::Window::create();

	//do whatever here

	CoolEngine::Window::destroy();

	return 0;
}

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:
CoolEngine::Window some_window;
It is automatically destroyed when it goes out of scope and is exception safe to that point. Similarly, functions of the window should either be non-static member functions or functions taking a window as a parameter. This makes it not possible to use the functions without the window being initialized.

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.

Optimus Prime Ribs
Jul 25, 2007

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.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

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?

That Turkey Story
Mar 30, 2003

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.
That and I didn't intend on making an instance of it.

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.

Optimus Prime Ribs
Jul 25, 2007

Avenging Dentist posted:

Then why are you making a class for it?

That was my original mistake and I'm not using classes anymore.

That Turkey Story
Mar 30, 2003

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.

Optimus Prime Ribs
Jul 25, 2007

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?

Crazy RRRussian
Mar 5, 2010

by Fistgrrl

Optimus Prime Ribs posted:

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?

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.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

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.

Josh Lyman
May 24, 2009


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?

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

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:
The C Programming Language by Kernighan and Ritchie
The C++ Programming Language by Bjarne Stroustrup
Accelerated C++ by Andrew Koenig and Barbara E. Moo
Other recommended C++ 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? :psyduck:

Dijkstracula fucked around with this message at 02:51 on Mar 18, 2010

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

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++.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Avenging Dentist posted:

A good 80% of C++0x features aren't really meant for a person who needs Accelerated C++.
Of course. But, if you're buying the language reference at this stage, by the time you get to actually needing it there will be a new edition out and you'll have paid full price for an out of date book.

Josh Lyman
May 24, 2009


Dijkstracula posted:

DONNA! Get me the first post on line two!


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? :psyduck:
I didn't know if any of the books in the OP would work on a short timetable, and I haven't used any of the foo in 21 days books so I don't know if they're good or bad.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

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.
Stroustrup + the ISO standard is the ideal language reference. Although the world is far from ideal and it's important to keep bookmarks to the implementation's documentation, so that one can be constantly reminded of how bad the implementation is.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

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.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

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)

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

This is one of the all-time best programming articles and makes me want to post a thread for the classics of the field.

An Outland Dish
Jul 26, 2009

by Tiny Fistpump

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.

floWenoL
Oct 23, 2002

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.

An Outland Dish
Jul 26, 2009

by Tiny Fistpump

floWenoL posted:

...and wrong.


...but accurate.

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? :ohdear:

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?

floWenoL
Oct 23, 2002

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.

An Outland Dish
Jul 26, 2009

by Tiny Fistpump

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.

Crazy RRRussian
Mar 5, 2010

by Fistgrrl

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?

pseudorandom name
May 6, 2007

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.

floWenoL
Oct 23, 2002

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.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

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).

Wit_sponge
Dec 29, 2008

FUUUCK
i need a sweet compiler for windows, i normally use linux but i need to make some exe's

plaese halp

Crazy RRRussian
Mar 5, 2010

by Fistgrrl
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?

yippee cahier
Mar 28, 2005

Wit_Sponge posted:

i need a sweet compiler for windows, i normally use linux but i need to make some exe's

plaese halp
mingw probably be best

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.

Genpei Turtle
Jul 20, 2007

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.

Standish
May 21, 2001

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?
I suppose you could mmap() or shmat() the same file/shared memory segment at two different addresses? If you were doing this you'd know it, so you probably have some other scope/memory corruption problem going on, impossible to tell without code...

OddObserver
Apr 3, 2009

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?
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.

Sounds like it's two different objects.

Genpei Turtle
Jul 20, 2007

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.

raminasi
Jan 25, 2005

a last drink with no ice
Can I address-of into a struct? e.g.

code:
struct MyStruct {
    int a;
    int b;
    int c;
};

void whatever() {
    struct MyStruct var;
    int * d;
    d = &var.b;
}

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Yes

Wit_sponge
Dec 29, 2008

FUUUCK

sund posted:

mingw probably be best

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.

thanks mate, i ended up using i ended up using devC++

which uses mingw anyway i think

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

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.

Adbot
ADBOT LOVES YOU

Crazy RRRussian
Mar 5, 2010

by Fistgrrl

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.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply