Hey all.
I know I could probably find out all of this if I sat down and read a big book of learning c++, but I've always found that I barely learn from it. This is partly because I know enough from other languages that I tend to skip 90% of it, partly because it completely zaps my motivation to program in the first place, and finally because I usually do a good job of picking things up myself. These days I find I only really learn by going in head-first and doing something, rather then going through tutorials/books, etc. So - I HIGHLY appreciate everyone's time with this.
I've got some experience over a wide range of languages but for whatever-reason I've never been 100% sold on what I was using, mostly due to the lack of flexibility in said language. So for the up-teenth time I've tried c++ and finally after a round of c# and its namespaces I'm getting a semi-decent grasp of it. There's a few things about c++ that I've never dealt with before that I'm having difficult to understand (mostly conceptually). As such I'm making a small simple game kinda like missile command but a bit more..... modern - pretty much a 3D-rendered, 2D plane version of it, defending a planet, with enemies coming from all directions.
Well, to start, I know how to use a pointer, and what they do, but coming from a C#/AS background, I'm finding it difficult to know when to actually use them. I never seem to use them at all, really - all I seem to know is new classes should be pointed to? and so should constant strings? What's the effect of using a pointer over not? Faster? Less memory? When do I use them? Also, I know what a pointer is ( * ) (points to a memory location) and I know what ( & ) does (translates a memory location back to a value) but what is it called? (the &).
And what the hell is the deal with (const <stringtype> *) being shown everywhere for strings? I mean, how can you have a constant variable that you can remove? I assume that I'm reading it wrong, but rather it's saying that the variable's value is a constant............. which it isn't always is? What's the deal with const?
To start with I've made everything that should only appear/happen once as static as I'm used to writing this way, but I want to learn singletons and do it properly (also so I can use constructors/destructors properly instead of Init()/Shutdown()). I've looked a bit at how to do them them but I'm not really picking up on 'how to' do them.
What are some peoples favourite places on where to find winapi functions? The MSDN or is there some other perhaps easier/better places out there?
A question for ORGE users -
And finally (for now heh ) ascii/unicode. I haven't delved into unicode much, though I want to, it seems like orge is almost pure-ascii based? In my experiences it hasn't dealt well with unicode. I'd assume by now it'd fully support unicode where it is needed. I don't understand why we can't just work in a fully unicode orge/codeing enviroment. In my mind it'd make things so much simpler.
Finally, I've always had troubles trying to work out dynamic controls, the concept, pseudo-code, if you will. But that can wait for another topic when I've done a bit more work on this fantastical terrible engine of fail I'm working on. XD
Big thanks for everyone who takes the time to read and comment.
Clarifying a few things about c++
Moderator: Coders of Rage
-
- Chaos Rift Junior
- Posts: 345
- Joined: Tue Jan 12, 2010 7:23 pm
- Favorite Gaming Platforms: PC - Windows 7
- Programming Language of Choice: c++;haxe
- Contact:
Re: Clarifying a few things about c++
I'll just be answering the pointer part.
okay so first thing thats wrong. & does not give you the value of the pointer, & is a reference of a var.
exp.
int a = 10;
int *pInt = &a;
&a returns the mem location where var "a" starts.
if you did printf("%d\n", *pInt) you would get 10. *pInt is called DEreferencing the var. it give you the value where pointer "p" is pointing to.
now for the use.
this is kinda a broad subject and can only be learnt by doing. pointers can be faster, but you need to know how to use them. they can also save memory if you use them properly, they dont make memory smaller. memory has to be defined somewhere, but you can have multiple pointers to the same memory instead of having multiple objects in memory.
here is just a small example. this is not really functional, but just show some simple pointer inc.
okay so first thing thats wrong. & does not give you the value of the pointer, & is a reference of a var.
exp.
int a = 10;
int *pInt = &a;
&a returns the mem location where var "a" starts.
if you did printf("%d\n", *pInt) you would get 10. *pInt is called DEreferencing the var. it give you the value where pointer "p" is pointing to.
now for the use.
this is kinda a broad subject and can only be learnt by doing. pointers can be faster, but you need to know how to use them. they can also save memory if you use them properly, they dont make memory smaller. memory has to be defined somewhere, but you can have multiple pointers to the same memory instead of having multiple objects in memory.
here is just a small example. this is not really functional, but just show some simple pointer inc.
Code: Select all
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * const argv[])
{
char *name = "avansc";
do{
printf("%c", *name);
} while(*name++);
return 0;
}
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
-
- Respected Programmer
- Posts: 387
- Joined: Fri Dec 19, 2008 3:33 pm
- Location: Dallas
- Contact:
Re: Clarifying a few things about c++
This is one of the primary gripes from your systems programmer types about managed code like C# or Java which is that memory localization and allocation is almost completely abstracted from you. Of course in C# there is the option to go unsafe and write out regular ole' C++ style heap allocation routines, but that is not recommended. With C++ there are numerous ways to "get memory".
So to start, "new" operates in much the same way as C#, however in C# you can use new as a modifier to hide inherited members from other classes, in C++ you can not do this. In terms of memory its just the same, its a heap allocation. Heap allocation is fairly fast and the memory persists until you explicitly delete/delete[] it (no garbage collectors). So if you fail to delete it, it's a leak. Now why heap allocation? Really, in C++ there's only two good reasons: 1) persistence/scope 2) size. First, if you need a block of memory to persist BEYOND the point in which it was created, new it. Secondly, if it's a massive chunk of memory that you need, new it. The reason "new" is so important in C# where C++ doesnt particularly care is because in C# it's illegal to use uninitialized values. C++ won't complain, you just won't get desired results. Thusly, you can have a class called "crap" and say crap p; If you try to use "p" in C# it will complain that it hasn't been initialized. C++ won't complain, but you'll have a bunch of garbage in "p". The new operator, like C# also invokes the constructor, if one was not supplied, a default will be given (don't count on this, however....write constructors). Similarly delete invokes destructors.
That said, there is an alternate method of grabbing memory which is called stack allocation. This is your typical int stuff[50]; notation. This memory exists in a different location and uses the stack set aside for that function space. It's limitations are: 1) persistence/scope 2) size. First, it goes bye-bye when it goes out of scope. Secondly, you're limited to the amount of memory you can address there. Stack allocation tends to be faster because they're all near references to memory. Eg: it's just right there, instead of WAY over there. So in general, if you only need a little bit "for right now", prefer stack allocation.
So the pointer notation (*) is just that. It's JUST a notation. The underlying CONCEPT...is what I just said above. The pointer just says "memory is here". Thats it.....that's all, there's not a damn thing else that it does, its just a placeholder for memory. No mystique, no behind-the-scenes, it's not a "dont bother the man behind the curtain". So if you are RECEIVING a memory block....the receiver should be.......a pointer. "new" returns a memory address. Thusly, the type on the left hand side is ....a pointer. If you want to pass memory around from one place to another...you pass....pointers. Its not a speed versus X kindof thing either. This is just what you must do to handle memory. Where ppl tend to get confused is in the & (address of) operator. You can pass a THING to something else "by reference", which is essentially an implicit pointer to the caller. Or you can take a THING's address by saying int* p = &thing; So, since & is the address of, then the left hand side should expect to RECEIVE a memory address, which means.......pointer. To dereference a pointer you use (*). This means, to take the value of what the pointer points to.
I'm not sure what you mean by (const <stringtype> *). I'm not exactly sure what "stringtype" is. You'll have to clarify that one.
OGRE is agnostic to unicode/ASCII. It doesn't care. It uses what you feed it, so in that regard its a user interface ONLY issue. So if you setup your project build options to do UNICODE only....well, there you go....thats it. I'm not sure what kind of GUI subsystem they're using these days, if its still CEGUI, then yes, CEGUI can display UNICODE glyphs.
Dynamic controls? As opposed to controls that don't move? ;]
So to start, "new" operates in much the same way as C#, however in C# you can use new as a modifier to hide inherited members from other classes, in C++ you can not do this. In terms of memory its just the same, its a heap allocation. Heap allocation is fairly fast and the memory persists until you explicitly delete/delete[] it (no garbage collectors). So if you fail to delete it, it's a leak. Now why heap allocation? Really, in C++ there's only two good reasons: 1) persistence/scope 2) size. First, if you need a block of memory to persist BEYOND the point in which it was created, new it. Secondly, if it's a massive chunk of memory that you need, new it. The reason "new" is so important in C# where C++ doesnt particularly care is because in C# it's illegal to use uninitialized values. C++ won't complain, you just won't get desired results. Thusly, you can have a class called "crap" and say crap p; If you try to use "p" in C# it will complain that it hasn't been initialized. C++ won't complain, but you'll have a bunch of garbage in "p". The new operator, like C# also invokes the constructor, if one was not supplied, a default will be given (don't count on this, however....write constructors). Similarly delete invokes destructors.
That said, there is an alternate method of grabbing memory which is called stack allocation. This is your typical int stuff[50]; notation. This memory exists in a different location and uses the stack set aside for that function space. It's limitations are: 1) persistence/scope 2) size. First, it goes bye-bye when it goes out of scope. Secondly, you're limited to the amount of memory you can address there. Stack allocation tends to be faster because they're all near references to memory. Eg: it's just right there, instead of WAY over there. So in general, if you only need a little bit "for right now", prefer stack allocation.
So the pointer notation (*) is just that. It's JUST a notation. The underlying CONCEPT...is what I just said above. The pointer just says "memory is here". Thats it.....that's all, there's not a damn thing else that it does, its just a placeholder for memory. No mystique, no behind-the-scenes, it's not a "dont bother the man behind the curtain". So if you are RECEIVING a memory block....the receiver should be.......a pointer. "new" returns a memory address. Thusly, the type on the left hand side is ....a pointer. If you want to pass memory around from one place to another...you pass....pointers. Its not a speed versus X kindof thing either. This is just what you must do to handle memory. Where ppl tend to get confused is in the & (address of) operator. You can pass a THING to something else "by reference", which is essentially an implicit pointer to the caller. Or you can take a THING's address by saying int* p = &thing; So, since & is the address of, then the left hand side should expect to RECEIVE a memory address, which means.......pointer. To dereference a pointer you use (*). This means, to take the value of what the pointer points to.
Code: Select all
int p = 5; // P holds the value of 5
int* ptr = &p; // ptr holds 0x488853BA (some memory address) p is AT 0x488853BA
int stuff = *ptr; // stuff holds 5, because what ptr points to holds the value of 5
OGRE is agnostic to unicode/ASCII. It doesn't care. It uses what you feed it, so in that regard its a user interface ONLY issue. So if you setup your project build options to do UNICODE only....well, there you go....thats it. I'm not sure what kind of GUI subsystem they're using these days, if its still CEGUI, then yes, CEGUI can display UNICODE glyphs.
Dynamic controls? As opposed to controls that don't move? ;]
-
- Chaos Rift Junior
- Posts: 345
- Joined: Tue Jan 12, 2010 7:23 pm
- Favorite Gaming Platforms: PC - Windows 7
- Programming Language of Choice: c++;haxe
- Contact:
Re: Clarifying a few things about c++
I was pretty much saying & did the same as a pointer! Opps!avansc wrote:I'll just be answering the pointer part.
okay so first thing thats wrong. & does not give you the value of the pointer, & is a reference of a var.
That's pretty much what I figured, I just wanted to make sure. And I meant make memory usage smaller for the program as a whole...... It was 2am when I writ this!avansc wrote:this is kinda a broad subject and can only be learnt by doing. pointers can be faster, but you need to know how to use them. they can also save memory if you use them properly, they dont make memory smaller. memory has to be defined somewhere, but you can have multiple pointers to the same memory instead of having multiple objects in memory.
I think I understand everything else about pointers though. Thanks!
Do I only need to delete on each "new" statement, or will there be others times too?qpHalcy0n wrote:Heap allocation is fairly fast and the memory persists until you explicitly delete/delete[]
edit:qpHalcy0n wrote:I'm not sure what you mean by (const <stringtype> *). I'm not exactly sure what "stringtype" is. You'll have to clarify that one.
Consider this which I found somewhere out there. What's the deal with const in these situations?
1) Strictly ANSI (Cannot use with UNICODE)
1. char - ANSI char.
2. LPSTR - Long pointer to ANSI string.
3. LPCSTR - Long pointer to const ANSI string.
4. std::string - Ansi string.
5. "hello" - An ansi string.
2) Strictly UNICODE (Cannot use with ANSI)
1. wchar_t - UNICODE char.
2. LPWSTR - Long pointer to UNICODE string.
3. LPCWSTR - Long pointer to const UNICODE string.
4. std::wstring - UNICODE string.
5. L"hello" - An Unicode string
IIRC, the project is set to default ASCII, and orge is probably #defineing to use ascii because of that. I'll change the project to unicode and see what happens.qpHalcy0n wrote:OGRE is agnostic to unicode/ASCII. It doesn't care. It uses what you feed it, so in that regard its a user interface ONLY issue. So if you setup your project build options to do UNICODE only....well, there you go....thats it. I'm not sure what kind of GUI subsystem they're using these days, if its still CEGUI, then yes, CEGUI can display UNICODE glyphs.
By dynamic controls I mean user-changeable controls, haha :P IE, "Up instead of W" for someone like my mum o.O
edit: Decided to just sit on my ass, stop my whining and bitching and go get a book and at least try to get through it. c++ is just far more complicated then anything I've tried before, even though I already understand a good deal of it, but the online tuts are just not cutting it.
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: Clarifying a few things about c++
Good choice, but you'll also want to do a lot of learning by doing; it allows you to develop your own coding style and determine what parts of a language you like/dislike. For example, with C I like that it's:Live-Dimension wrote:edit: Decided to just sit on my ass, stop my whining and bitching and go get a book and at least try to get through it. c++ is just far more complicated then anything I've tried before, even though I already understand a good deal of it, but the online tuts are just not cutting it.
- concise
does no more than you tell it to do
does no less than you tell it to do (these 2 are the case with MOST languages)
it provides a pretty deep understanding of how the computer actually does things
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
-
- Chaos Rift Junior
- Posts: 345
- Joined: Tue Jan 12, 2010 7:23 pm
- Favorite Gaming Platforms: PC - Windows 7
- Programming Language of Choice: c++;haxe
- Contact:
Re: Clarifying a few things about c++
I know all sorts of languages and am pretty decent with PHP (Procedural) and C# (OOP). I understand how to program and quite well in a few languages. I've just never done anything at such a low level like c++ - it's taking time for me to get wrapped around specific concepts. I just don't learn well with books =\
Re: Clarifying a few things about c++
Good talk, I've learned things :D