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.


|