Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > New to c++, need help with advanced topics

Reply
Thread Tools

New to c++, need help with advanced topics

 
 
Marcus Kwok
Guest
Posts: n/a
 
      12-05-2005
Ben Pope <(E-Mail Removed)> wrote:
> Old Wolf wrote:
>> Ben Pope wrote:
>>
>>> Incidentally, it's preferable in C++ to describe types,

>>
>> Says who?

>
> I was supposed to say there is a bias towards describing types, rather
> than variables.
>
> I'm not sure who said it, and it is of course personal preference. Both
> are equally valid.
>
> From what I've seen, Bjarne Stroustrup prefers the former (FWIW).


Agreed, see http://www.research.att.com/~bs/bs_faq2.html#whitespace

The choice between "int* p;" and "int *p;" is not about right and
wrong, but about style and emphasis. C emphasized expressions;
declarations were often considered little more than a necessary evil.
C++, on the other hand, has a heavy emphasis on types.

A "typical C programmer" writes "int *p;" and explains it "*p is what
is the int" emphasizing syntax, and may point to the C (and C++)
declaration grammar to argue for the correctness of the style.
Indeed, the * binds to the name p in the grammar.

A "typical C++ programmer" writes "int* p;" and explains it "p is a
pointer to an int" emphasizing type. Indeed the type of p is int*.
I clearly prefer that emphasis and see it as important for using the
more advanced parts of C++ well.

...

Whenever something can be done in two ways, someone will be confused.
Whenever something is a matter of taste, discussions can drag on
forever.

--
Marcus Kwok
 
Reply With Quote
 
 
 
 
Niklas Norrthon
Guest
Posts: n/a
 
      12-06-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> Niklas Norrthon wrote:
>
> > (E-Mail Removed) writes:
> >
> > > (E-Mail Removed) wrote:
> > >
> > > Variables should always be
> > > initialised at the point of declaration. If you don't know what to
> > > initialise it to yet, don't declare it yet.

> > Not always true, sometimes there are reasons not to initialize a
> > variable. Below are two cases where a delayed initalization is
> > necessary:

>
> I should have added
>
> If, because of language rules, you are forced to declare a variable
> before its first use, declare it as late as possible and initialise it
> to some defined value anyway.
>
> > int func1()
> > {
> > int i;

>
> int i = 0;
> >
> > for (i = 0; i < some_max_value; ++) {



Here we are in disagreement. I see no reason why one would init a
variable, if it assigned on the very next line of code.

If I really wanted to init the variable I'd rather do:

int = 0;

for (; i < some_max_val; ++i) { // and so on...

There is no harm in doing it your way, but I think my way is
perfectly acceptable too, and even prefered, since it makes
the code clearer. It's somewhat related assigning NULL to a
pointer after a call to free, which is totally unnecessary
if the variable goes out of scope on the next line of code...

/Niklas Norrthon
 
Reply With Quote
 
 
 
 
deane_gavin@hotmail.com
Guest
Posts: n/a
 
      12-06-2005

Niklas Norrthon wrote:
> (E-Mail Removed) writes:
>
> > Niklas Norrthon wrote:
> >
> > > (E-Mail Removed) writes:
> > >
> > > > (E-Mail Removed) wrote:
> > > >
> > > > Variables should always be
> > > > initialised at the point of declaration. If you don't know what to
> > > > initialise it to yet, don't declare it yet.
> > > Not always true, sometimes there are reasons not to initialize a
> > > variable. Below are two cases where a delayed initalization is
> > > necessary:

> >
> > I should have added
> >
> > If, because of language rules, you are forced to declare a variable
> > before its first use, declare it as late as possible and initialise it
> > to some defined value anyway.
> >
> > > int func1()
> > > {
> > > int i;

> >
> > int i = 0;
> > >
> > > for (i = 0; i < some_max_value; ++) {

>
>
> Here we are in disagreement. I see no reason why one would init a
> variable, if it assigned on the very next line of code.


The reason is to avoid ever having uninitialised variables around. That
way, when I restructure the code, I can guarantee that, no matter what
stupid mistake I make, the bug I introduce will not be a hard to track
down, appears to only break in release mode, undefined behaviour bug
caused by trying to use an uninitialised variable. Always initialising
the variable is a simple technique to avoid ever having that problem.

> If I really wanted to init the variable I'd rather do:
>
> int = 0;
>
> for (; i < some_max_val; ++i) { // and so on...
>
> There is no harm in doing it your way, but I think my way is
> perfectly acceptable too, and even prefered, since it makes
> the code clearer.


I certainly never suggested doing that!

Explicitly assigning the initial value to the loop variable is
certainly preferable to relying on previous code. But that point is
orthogonal to whether you should ever allow uninitialised variables in
your code.

> It's somewhat related assigning NULL to a
> pointer after a call to free, which is totally unnecessary
> if the variable goes out of scope on the next line of code...


Well, I suppose I wouldn't care too much about initialising i in code
like this

void f()
{
// ... do lots of stuff
int i; // immediately going out of scope
}

but I've never written code like that.

Using RAII principles, all resource release happens in destructors, so
I find that pointers do indeed go out of scope immediately after delete
is called on them. But if I was going to keep a pointer hanging around
for reuse, I would consider setting it to null for the same reason.

Gavin Deane

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Advanced Python Programming Oxford Lectures [was: Re: *Advanced*Python book?] Michele Simionato Python 1 03-27-2010 06:10 AM
is there any site which can provide any help on advanced C topics ashu C Programming 7 01-26-2006 01:19 PM
Advanced topics... optimising / C++ / OOP / RefCounting / type-safety st_ev_fe@hotmail.com C++ 4 11-15-2005 12:35 PM
Need help on topics to cover David Computer Information 0 07-31-2004 01:11 PM
Need Remote Button with sub-topics news.frontiernet.net Javascript 2 09-16-2003 03:42 AM



Advertisments