Skip to main content
gamedev.net
🔒 Locked

Questions about C++ syntax

Started by CloudNine Dec 17, 2002 at 10:32 AM 26 replies 450+ views
Original Post
CloudNine
CloudNine
Hi, I''ve got a few questions about C++ in general 1) What''s the difference between a struct, and say a namespace? 2) What''s with the ... as a function argument? 3) What''s the point in pointers (*audience groans*)like this int a*=b ? Thanks in advance CloudNine
civguy
civguy
quote:
Original post by Anonymous Poster
int a*=b
Is this a pointer or are you doing a times equals?
That''s illegal syntax. But I think OP meant int *a=b; since he was talking about pointers. And int *a=b; means that ''a'' is a pointer that will point at the same integer that ''b'' points at. Just like in natural language you''d say "Person *me = you" and then I''d be pointing with my finger at the same person as you do. But let''s just cross our fingers that we''re now really pointing at a person and not, say, a goat. Because goats don''t like to confused as people. Or were we pointing at a deleted person, that''d be nasty too don''t you think? Yes, this story had a reasoning behind it - that dumb pointers should be used with caution. I think it''s time to get some sleep...
Xai
Xai
A namespace provides the compiler a way to distinguish between multiple code elements (types, function, or variables) which would otherwise have the same signature. They are usually used to wrap libraries, or sections of related code, so that other sections of code will not interfere with the expected behavior, and the library will play nice with other code ...

Example ... I want to write an OpenAL sound wrapper library with classes and functions such as:

void Init(void);
void Shutdown(void);

class Buffer;
class Source;
class Listener;
class Device;
class Context;

you see how generic these names are ... so if I didn''t have namespaces ... then what would I do when I also wanted to make a OpenGL class library with:

void Init(void);
void Shutdown(void);
class Buffer;
class Context;
class Object;

You see the problem, there are 2 function name conflicts, and 2 class name conflicts ... if I was smart, I would wrap ALL of the OpenAL code in a namespace - perhaps "al" or "OpenAL" depending on your personal preference, and naming conventions, then wrap the gl stuff in a namespace "gl" or "OpenGL", then ... in my main program I could call the correct functions like this:


  
int main(void)
{
OpenAL::Init();
OpenGL::Init();

OpenAL::Device primarySoundDevice;
OpenGL::Device primaryVideoDevice;

// etc


OpenGL::Shutdown();
OpenAL::Shutdown();
}
CloudNine
CloudNine
Regarding pointers, does that mean that a will point at whatever b is pointing at, instead of pointing directly to b, which could cause memory errors if deleted. (That was a bad sentence)

Is that it?
GoofProg
GoofProg
int *a=b;
What this does is sets the value of whats in ''b'' and places it into *a or the address of a
The nightmare travels across the cosmos with his burning mane.  The trail of ash that is produced. ?Have a nice day!?
civguy
civguy
quote:
Original post by CloudNine
does that mean that a will point at whatever b is pointing at, instead of pointing directly to b

quote:
Original post by civguy
int *a=b; means that ''a'' is a pointer that will point at the same integer that ''b'' points at

Analysis: it''s the former of your choices.
Dean Harding
Dean Harding
What you''ve actually given is "a times equals b", it''s short-hand for this:

a = a * b;

Is that really what you were asking?

If I had my way, I''d have all of you shot!


codeka.com - Just click it.

Sneftel
Sneftel
quote:
Original post by CloudNine
Regarding pointers, does that mean that a will point at whatever b is pointing at, instead of pointing directly to b, which could cause memory errors if deleted. (That was a bad sentence)

Is that it?


No, a points at the memory used by b. If a was set to 2, b now points to a place in memory that has a "2" in it.

quote:
]Original post by GoofProg
What this does is sets the value of whats in 'b' and places it into *a or the address of a


Er. You either have no idea what you're talking about, or you're making more typos than an epileptic stenographer.


Don't listen to me. I've had too much coffee.

[edited by - sneftel on December 17, 2002 4:08:51 PM]
Sneftel
Sneftel
quote:
Original post by Sneftel
[quote]Original post by CloudNine
Regarding pointers, does that mean that a will point at whatever b is pointing at, instead of pointing directly to b, which could cause memory errors if deleted. (That was a bad sentence)

Is that it?


No, a points at the memory used by b. If a was set to 2, b now points to a place in memory that has a "2" in it. Deleting this pointer would be an error. We would say that the pointer does not "own" the memory it points to.

quote:
]Original post by GoofProg
What this does is sets the value of whats in ''b'' and places it into *a or the address of a


Er. You either have no idea what you''re talking about, or you''re making more typos than an epileptic stenographer.


Don''t listen to me. I''ve had too much coffee.

[edited by - sneftel on December 17, 2002 4:08:51 PM]

SabreMan
SabreMan
quote:
Original post by Dean Harding
What you''ve actually given is "a times equals b"

What he''s actually given is invalid syntax.
SabreMan
SabreMan
quote:
Original post by Anonymous Poster
This is not illegal syntax

Neither is it the syntax that has been given.
Dean Harding
Dean Harding
quote:
Original post by SabreMan
What he''s actually given is invalid syntax.


Sorry, you''re right. I never noticed the "int" in front. Heh, I just had to add to all the other misinformation which was being passed around...

If I had my way, I''d have all of you shot!


codeka.com - Just click it.

weesi
weesi
quote:
Original post by civguy
[quote]Original post by Anonymous Poster
int a*=b
Is this a pointer or are you doing a times equals?
That''s illegal syntax. But I think OP meant int *a=b; since he was talking about pointers. And int *a=b; means that ''a'' is a pointer that will point at the same integer that ''b'' points at. Just like in natural language you''d say "Person *me = you" and then I''d be pointing with my finger at the same person as you do. But let''s just cross our fingers that we''re now really pointing at a person and not, say, a goat. Because goats don''t like to confused as people. Or were we pointing at a deleted person, that''d be nasty too don''t you think? Yes, this story had a reasoning behind it - that dumb pointers should be used with caution. I think it''s time to get some sleep...

a*=b is not illegal syntax. It means a = a*b
"Those who dont really have anything interesting to say should not be quoted"
davepermen
davepermen
assuming "int* a = b"

what ever value b contains, a gets set to this value

just like in
int a = b.

say, b contains 15, then a contains 15, too..

in
int* a = b,

say b contains the memory adress 0x2456123, then a contains the memory adress 0x2456123, too. thats all..


in fact, a pointer is just another integral type, containing numbers that should represent adresses.. (that "should" is the dangerous part of pointers.. they can contain _any_ number, => _any_ adress, that means, unimportant if you are allowed to have access to a certain piece of memory, you can have that adress and try to access it. that, of course, results in an error, namely the application crashes.. have fun:D)

"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
Paradigm Shifter
Paradigm Shifter
There''s shed loads of misinformation here. (I''d say about 2.3 sheds worth).
int* a = b;
- there''s an important piece of info missing here, namely the type of b. If it''s another int, then it''s just wrong, it should be
int* a = & b; // point a at memory address of b

if b is a pointer to an int, then it''s sensible. I''d recommend declaring all pointers with a "p" in front of them,

int* pInt = & anotherInt;
int* pInt = pAnotherInt;
etc.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
mds_79
mds_79

int* a;
int b = 10;
int c = 2;

a = &c //make 'a' point to the same address as 'c'

//changes the contents of 'a' (which in this case is points to 'c') to 10

*a = b;

std::cout << c; //outputs 10


Is this then not correct?

"Discipline is never an end itself, only a means to an end." - Robert Fripp



[edited by - mds_79 on December 18, 2002 11:08:29 AM]
"Discipline is never an end itself, only a means to an end." - Robert Fripp
Qw3r7yU10p!
Qw3r7yU10p!
Paradigm Shifter: First, I recommend not using pointers at all but rather references.
Second, if you do use pointers there''s no need to wart your variable names with a ''p''. If you can''t remember what type your variables are and you''ve lost sight of their declarations, then it could be a good idea to split your function to make it smaller. If you can see what it is, what''s the point of putting a ''p'' on it?
Anyway, using references make this a moot point.
CloudNine
CloudNine
What are references? Are they similar to pointers?

Also, why, when you declare a class instance as a function argument, do you have to make it a pointer?

Like this...

int rainbow(COLORS* red)

CloudNine
CloudNine
Heh, just found an article on pointers
Qw3r7yU10p!
Qw3r7yU10p!

int rainbowV(COLORS red); // pass by value
int rainbowP(COLORS* red); // pass by pointer
int rainbowR(COLORS& red); // pass by reference

you can call these as follows:

COLORS red(1,0,0);

rainbowV(red);
rainbowP(&red);
rainbowR(red);

pass by value will make a copy of the COLORS object, calling it''s copy constructor, if it has one, or doing memberwise assignment if it doesn''t have one.

pass by pointer will pass the address of the local red variable. Inside the rainbow function it will have access to that local variable.

pass by reference takes the local variable, red, and makes it available inside the rainbowR function. it doesn''t make a copy.

Pass by reference is very similar to pass by pointer. It effectively passes the address of the local variable. There is one difference though. Inside the function you can be pretty certain that the variable is okay to use, whereas with the pointer version of the function you''ll have to check to see if it''s null before using it.

Pass by reference gives you a stronger contract with the person using the function. If it happens that you need a null pointer to indicate some condition you can use a null object or keep the pointer. Or overload the function, or pass an enumeration to say what you mean, whatever. As long as the user knows what demands are placed on them.

I''ve just gone all through an engine for work replacing pointers with references and removing the checks for null pointers. It''s cleaned up the code a great deal and pushed all the pointer checking code that does exist into one place. Very nice! (we need a few pointers as we have a C interface).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.