Connecting Tech Pros Worldwide Forums | Help | Site Map

A question of style?

Lilith
Guest
 
Posts: n/a
#1: Jul 23 '05
I've only recently started to learn Windows programming using VC++.
It's relatively easy to understand the basics fo attaching functions
to controls but I've been trying to learn Windows from the roots on
down. In looking at source code I'm finding a tendency for some
programmers to declare an object of their application type with a new
and subsequently use the pointer-member-of operator to access the
members of the class object. Considering it was obvious in these
instances that there was only going to be one application object, what
is the point of declaring a pointer and then go through the process of
allocating the object itself rather than simply declaring the object
and forgetting the allocation?

--
Lilith

Phlip
Guest
 
Posts: n/a
#2: Jul 23 '05

re: A question of style?


Lilith wrote:
[color=blue]
> I've only recently started to learn Windows programming using VC++.[/color]

Sample VC++ code is about the most wretched style you can find. The only
thing worse in our industry is Dr Dobb's Journal.
[color=blue]
> ...In looking at source code I'm finding a tendency for some
> programmers to declare an object of their application type with a new
> and subsequently use the pointer-member-of operator to access the
> members of the class object.[/color]

You just mean -> notation. Not pointer to member.

But, yes, these programmers are using 'new' because "all the other
programmers were doing it", not because they need an object to live longer
than the current scope.

Read books like /Design Patterns/ and /Effective C++/ to learn good style.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand


BigBrian
Guest
 
Posts: n/a
#3: Jul 23 '05

re: A question of style?


> I've only recently started to learn Windows programming using VC++.[color=blue]
> It's relatively easy to understand the basics fo attaching functions
> to controls but I've been trying to learn Windows from the roots on
> down.[/color]

This has nothing to do with the C++ language.
[color=blue]
> In looking at source code I'm finding a tendency for some
> programmers to declare an object of their application type with a new
> and subsequently use the pointer-member-of operator to access the
> members of the class object. Considering it was obvious in these
> instances that there was only going to be one application object, what
> is the point of declaring a pointer and then go through the process of
> allocating the object itself rather than simply declaring the object
> and forgetting the allocation?[/color]

I'm not exactly sure what your question is, but it sounds like you're
asking "what the difference between creating an object with new and
just declaring it as an automatic variable?" If that's what you're
asking, new creates memory on the heap, declaring an automatic variable
puts the object on the stack. Another difference is that with a
pointer to an object created with new, you can use polymorphism.




[color=blue]
> --
> Lilith[/color]

Victor Bazarov
Guest
 
Posts: n/a
#4: Jul 23 '05

re: A question of style?


Lilith wrote:[color=blue]
> I've only recently started to learn Windows programming using VC++.
> It's relatively easy to understand the basics fo attaching functions
> to controls but I've been trying to learn Windows from the roots on
> down. In looking at source code I'm finding a tendency for some
> programmers to declare an object of their application type with a new
> and subsequently use the pointer-member-of operator to access the
> members of the class object.[/color]

You mean -> operator, yes?
[color=blue]
> Considering it was obvious in these
> instances that there was only going to be one application object, what
> is the point of declaring a pointer and then go through the process of
> allocating the object itself rather than simply declaring the object
> and forgetting the allocation?[/color]

The difference is where the object is allocated and _when_ it is
initialised. Statically allocated objects (objects with static storage
duration) are allocated potentially in a different place than those with
dynamic storage duration. Also, you may need to have control over the
time when your application object is initialised. Statically allocated
objects are initialised at some point before the 'main' function is
called, which is not necessarily the right time.

V
David White
Guest
 
Posts: n/a
#5: Jul 23 '05

re: A question of style?


BigBrian wrote:[color=blue]
> new creates memory on the heap, declaring an automatic
> variable puts the object on the stack. Another difference is that
> with a pointer to an object created with new, you can use
> polymorphism.[/color]

You can use polymorphism with a pointer to an object on the stack as well.

DW


Lilith
Guest
 
Posts: n/a
#6: Jul 23 '05

re: A question of style?


On 5 Jul 2005 14:58:16 -0700, "BigBrian" <work@brianmielke.com> wrote:
[color=blue][color=green]
>> I've only recently started to learn Windows programming using VC++.
>> It's relatively easy to understand the basics fo attaching functions
>> to controls but I've been trying to learn Windows from the roots on
>> down.[/color][/color]
[color=blue]
>This has nothing to do with the C++ language.[/color]

No, it was by way of a small introduction and an explanation of where
I'm coming from.
[color=blue][color=green]
>> In looking at source code I'm finding a tendency for some
>> programmers to declare an object of their application type with a new
>> and subsequently use the pointer-member-of operator to access the
>> members of the class object. Considering it was obvious in these
>> instances that there was only going to be one application object, what
>> is the point of declaring a pointer and then go through the process of
>> allocating the object itself rather than simply declaring the object
>> and forgetting the allocation?[/color][/color]
[color=blue]
>I'm not exactly sure what your question is, but it sounds like you're
>asking "what the difference between creating an object with new and
>just declaring it as an automatic variable?" If that's what you're
>asking, new creates memory on the heap, declaring an automatic variable
>puts the object on the stack. Another difference is that with a
>pointer to an object created with new, you can use polymorphism.[/color]

No, I know the difference as regards to their creation. I was more
interested in the why of it in a particular instance. I didn't see
the purpose of creating the object dynamically when a statically
declared variable would have sufficed. I was also curious as to
whether or not there is a small impact on performance in using -> to
reference the members of the object.

--
Lilith
Lilith
Guest
 
Posts: n/a
#7: Jul 23 '05

re: A question of style?


On Tue, 05 Jul 2005 21:55:57 GMT, "Phlip" <phlip_cpp@yahoo.com> wrote:
[color=blue]
>Lilith wrote:
>[color=green]
>> I've only recently started to learn Windows programming using VC++.[/color][/color]
[color=blue]
>Sample VC++ code is about the most wretched style you can find. The only
>thing worse in our industry is Dr Dobb's Journal.[/color]

So, my earlier issues aren't going to bring much on eBay? Seriously,
it was Dr Dobbs that got me over my impass at understanding C in the
first place.
[color=blue][color=green]
>> ...In looking at source code I'm finding a tendency for some
>> programmers to declare an object of their application type with a new
>> and subsequently use the pointer-member-of operator to access the
>> members of the class object.[/color][/color]
[color=blue]
>You just mean -> notation. Not pointer to member.[/color]

Yes. I'm inclined to try to be concise in my wording and it sometimes
works against me.
[color=blue]
>But, yes, these programmers are using 'new' because "all the other
>programmers were doing it", not because they need an object to live longer
>than the current scope.[/color]

Someone had to start it. :-) I see little reason for declaring a
single object dynamically but I can see the need for doing it for
arrays and as part of a linked list. But I'm not the expert
programmer that many on this are and wanted to make sure I'm not
missing a bet somewhere.
[color=blue]
>Read books like /Design Patterns/ and /Effective C++/ to learn good style.[/color]


--
Lilith
Lilith
Guest
 
Posts: n/a
#8: Jul 23 '05

re: A question of style?


On Tue, 05 Jul 2005 17:57:14 -0400, Victor Bazarov
<v.Abazarov@comAcast.net> wrote:
[color=blue]
>Lilith wrote:[color=green]
>> I've only recently started to learn Windows programming using VC++.
>> It's relatively easy to understand the basics fo attaching functions
>> to controls but I've been trying to learn Windows from the roots on
>> down. In looking at source code I'm finding a tendency for some
>> programmers to declare an object of their application type with a new
>> and subsequently use the pointer-member-of operator to access the
>> members of the class object.[/color]
>
>You mean -> operator, yes?
>[color=green]
> > Considering it was obvious in these
>> instances that there was only going to be one application object, what
>> is the point of declaring a pointer and then go through the process of
>> allocating the object itself rather than simply declaring the object
>> and forgetting the allocation?[/color]
>
>The difference is where the object is allocated and _when_ it is
>initialised. Statically allocated objects (objects with static storage
>duration) are allocated potentially in a different place than those with
>dynamic storage duration. Also, you may need to have control over the
>time when your application object is initialised. Statically allocated
>objects are initialised at some point before the 'main' function is
>called, which is not necessarily the right time.[/color]

Many thanks,
Lilith

Phlip
Guest
 
Posts: n/a
#9: Jul 23 '05

re: A question of style?


Lilith wrote:
[color=blue]
> No, it was by way of a small introduction and an explanation of where
> I'm coming from.[/color]

Sorry. You mentioned "Windows" too high in your post. Knees jerked. We'll
try not to do it again.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand




Alf P. Steinbach
Guest
 
Posts: n/a
#10: Jul 23 '05

re: A question of style?


* Lilith:[color=blue]
> In looking at source code I'm finding a tendency for some
> programmers to declare an object of their application type with a new
> and subsequently use the pointer-member-of operator to access the
> members of the class object. Considering it was obvious in these
> instances that there was only going to be one application object, what
> is the point of declaring a pointer and then go through the process of
> allocating the object itself rather than simply declaring the object
> and forgetting the allocation?[/color]

Difficult to say without a concrete example, but one reason might be that
the single object is of a class that's derived from a class A that is
designed for dynamic allocation, e.g. an A-object will self-destroy when
a given "we're finished" message is processed. But that is speculation.
I've programmed extensively on the platform you mentioned and can not recall
ever needing to dynamically allocate an application object (window objects,
on the other hand...).

Perhaps Phlip's answer is the most likely, that they're doing this because
they see other programmers doing it (in various situations), and don't
understand the reasons but just copy what they think others do.

That's very common in general.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Lilith
Guest
 
Posts: n/a
#11: Jul 23 '05

re: A question of style?


On Wed, 06 Jul 2005 01:49:30 GMT, "Phlip" <phlip_cpp@yahoo.com> wrote:
[color=blue]
>Lilith wrote:[/color]
[color=blue][color=green]
>> No, it was by way of a small introduction and an explanation of where
>> I'm coming from.[/color][/color]
[color=blue]
>Sorry. You mentioned "Windows" too high in your post. Knees jerked. We'll
>try not to do it again.[/color]

And I was aware that that could happen. But I anticipated that the
practice I was questioning might be a necessity of the environment I
was programming for or at least a convenience.

--
Lilith
Closed Thread