Various Questions

Hey there!

And so begins my trek into the world of C++! I have become quite fluent in Java and have begun to pick up C++, it's moving along quickly but certain things, mostly syntactical, are holding me up.

Here are a few things that are on my mind that perhaps someone could assist me in answering:

First:
Coming from the world of Java, the idea of 'null' is fresh within my mind... However, in C++'s world of pointers I'm finding that the concept of null is not so outright. So, my question is this, how does one check for nulls and how might they occur in C++? My understanding is that a null in C++ is a pointer that holds a value of zero. Is this correct?

Secondly:
Again, coming from Java, the term new is used to instantiate any object. Whereas, in C++ it is used to declare allocations for dynamic memory. I understand this much, however, I'd like to know of a good example in which one would use new effectively as opposed to making constant variables (if that's the right name for them, i.e. string myString not using the 'new' operator).

Thirdly:
I understand the concept of 'Friends' and how to declare them. But is this a common practice? Declaring objects to be friends outside of the same object type seems like a terrible excuse to couple classes.

Lastly:
Having just read the exceptions lesson on this site one key thing has come to my attention, that is, declaring the type of exception a function may throw. For example:

1
2
3
void toThrow() throw(int)	{
	throw 'a';
}


As far as I could gather from the guide the keyword throw() after the function identifier, in this case throw(int), it is supposed to limit the type of exception that can be thrown? But in this case, when I run the program it allows me to catch the char exception that is thrown. Obviously I'm misunderstanding the concept of the throw() operator and if someone could better explain it to me I would be very grateful!

Thanks a ton for any and all responses, and my apologies if something I wrote here is incorrect (haven't gotten my C++ legs yet.)
1. Yes. Note that Java lets you have null objects (which are really just null pointers, but the language hides that fact from you). That's not the case here, unless otherwise allowed by a particular class.
2. "Constant variable" is an oxymoron, obviously. What you're calling "constant variable" is in fact an automatic object. Automatic objects exist on the stack; they're called automatic because, unlike dynamic objects, they're automatically destructed when they go out of scope. Dynamic objects never go out of scope and you have to manage them yourself. One allocates objects on the heap when they're very large (e.g. an array), when it can't be known at compile time how many of them will be needed (e.g. nodes in a linked list), when it's desirable to return an object from a function while skipping expensive copy procedures, or -- more generally -- when the object should outlive the function that created it, when you need to have "null" instances of the object but it's not possible to modify the class (e.g. an array of pointers to dynamic objects), etc.
3. For the past four years, I've only needed to use the friend keyword once. I've used goto more often.
4. 'a' can be implicitly converted to an int, which can be implicitly converted back to char (I think that works with exceptions, too).
Just so you know, the exceptions part of C++ is kind of a mess; exception specifications are particularly useless. A function that throws something that doesn't appear in it will compile all the same, but will call std::unexpected() when that exception is thrown at run time (std::unexpected() never returns). It's completely useless if you're the one writing the function, and it's almost as useless if you're the one using it. Exception specifications will be deprecated in C++0x. Good riddance.
Wow, excellent response Helios! Just a few things I'd like to make sure I have straight.

To check for a null pointer does one do:
1
2
3
if(pointer == 0){
//blahfooblah
}


Also, when you say exception specifications are useless are you saying that I'd be best of not even using the throws() operator?

And lastly, when you said that you have only used the goto operator more often, what do you mean? Is it frowned upon to used goto or did you use goto to avoid the use of friend? Haha, sorry a little confused on that one.

Thanks again for the help!
To check for a null pointer does one do: [example]
Yes. Some, myself included, prefer to obviate comparisons to zero:
1
2
if (!var) //same as var==0
if (var)  //same as var!=0 
It's just a matter of style, though. Use whichever you prefer. Note, however, that some compilers will give harmless warnings when var is a pointer on the second example.

Also, when you say exception specifications are useless are you saying that I'd be best of not even using the throws() operator?
This is what I mean:
T function() throw(/*something*/) //<- Completely useless. You may as well not bother.

And lastly, when you said that you have only used the goto operator more often, what do you mean? Is it frowned upon to used goto or did you use goto to avoid the use of friend? Haha, sorry a little confused on that one.
goto is generally (sometimes unfairly) reviled in structured languages such as C++, but there are a very few instances where using it may be acceptable. What I was saying was that I've encountered those instances more often than I've thought "I could really use the friend keyword in this class structure". I'm not saying it's useless, I'm just saying it's perfectly possible to do without it.
Incidentally, the same goes for exceptions.
Hi, pardon me barging in. Pointers/references still confuse me. Is null really a pointer or is it a value? Is it the same null as in a null-terminated string or are there different null?
NULL in C++ is a value. It's the value 0. A null terminated string is a string terminated by a the char '\0' which has a value of 0, which is the same as NULL- hence null-terminated string
Last edited on
Ok, just as a question of good practice...

I've been tinkering with making a LinkedList implementation just to make sure I have my syntax down and that I understand pointers. However, as I worked along, I had a question pop into my head... Is it better practice to return variables by reference or simply return a pointer to them? My gut said pointer as it tended to be much easier.

So, in short:
myType& myFunct(int BlahFoo) {}

Or,

myType* myFunct(int BlahFoo) {}

Thanks for any help on this! Also, if you wouldn't mind, whichever one you suggest could you explain why?
Depends on whether you want to return dynamic objects that the caller will take ownership of, and whether you want to return a null pointer. If either or both of those are true, return a pointer. Otherwise, it's probably better to return a reference.
Topic archived. No new replies allowed.