Talk About Network

Google





Programming > C - C++ Learning > Re: Allocating ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 8 Topic 4126 of 4400
Post > Topic >>

Re: Allocating memory with "new"

by pat <pkelecy@[EMAIL PROTECTED] > Apr 23, 2008 at 06:52 PM

Francis Glassborow wrote:
> Pat wrote:
>> I have a question about the new operator.  The syntax for it is:
>>
>>     int * pointer = new int;
>>
>> which says that "pointer" points to a type int variable.
>>
>> My question is why is "int" needed twice?  I know it's needed from a 
>> syntax standpoint, but I don't understand what additional information 
>> the second int really provides.  Doesn't the first int already tell 
>> you that an int type value is going to be stored at the "pointer" 
>> address? Can you ever have,
>>
>>     int * pointer = new double
>>
>> or something like that?
>>
>> Thanks for any feedback on this.
> 
> So are you advocating:
> 
> int * pointer = new;
> 
> ??
> 
> Even if you are this only applies to fundamental types:
> 
> Given:
> 
> class A {
> // definition
> };
> 
> class B: public A {
> // definition
> };
> 
> It is perfectly legal to write:
> 
> A* a_ptr = new B;
> 
> and if A is the base of a polymorphic hierarchy (i.e. has a virtual 
> dtor) it would actually be quite common to do so.
>

Thanks for the reponse.

No, I wasn't advocating a different syntax, but rather just trying to 
understanding the rational for the current one.  I'm trying to teach 
myself C++ (with the help of your book, BTW :^) and all the examples I 
had seen (up until recently)  could have been served with an "int * 
point = new" type syntax, if allowed.  But your examples, as well as the 
one from sk_usenet, have showed why the other is needed.

I appreciate it.  Thanks.
 




 8 Posts in Topic:
Allocating memory with "new"
Pat <pkelecy@[EMAIL PR  2008-04-23 07:40:12 
Re: Allocating memory with "new"
Francis Glassborow <fr  2008-04-23 13:48:37 
Re: Allocating memory with "new"
pat <pkelecy@[EMAIL PR  2008-04-23 18:52:39 
Re: Allocating memory with "new"
"sk_usenet" <  2008-04-23 10:00:56 
Re: Allocating memory with "new"
pat <pkelecy@[EMAIL PR  2008-04-23 18:55:25 
Re: Allocating memory with "new"
barss <barss.dev@[EMAI  2008-04-24 03:19:49 
Re: Allocating memory with "new"
Francis Glassborow <fr  2008-04-24 11:38:30 
Re: Allocating memory with "new"
Ulrich Eckhardt <dooms  2008-04-26 08:11:08 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
localhost-V2008-12-19 Wed Jan 7 12:48:34 PST 2009.