|
Prev: initialising built in types with curly braces
Next: Mapping Templates function betweeen 2 class
From: Pat on 23 Apr 2008 07:40 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.
From: Francis Glassborow on 23 Apr 2008 08:48 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.
From: "sk_usenet" sometechyguy at gmail dot on 23 Apr 2008 13:00 "Pat" <pkelecy@_REMOVETHIS_gmail.com> wrote in message >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? What about arrays? int *p = new int[10]; > Can you ever have, > > int * pointer = new double No, realize that new operator is returning a double*, and heance trying to assign to an int* is not legal. > or something like that? > Thanks for any feedback on this. -- http://techytalk.googlepages.com
From: pat on 23 Apr 2008 18:52 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.
From: pat on 23 Apr 2008 18:55 sk_usenet wrote: > "Pat" <pkelecy@_REMOVETHIS_gmail.com> wrote in message >> 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? > > What about arrays? > int *p = new int[10]; Yes, that's a good (and common) example of why the current syntax is needed. Thanks for the help.
|
Next
|
Last
Pages: 1 2 Prev: initialising built in types with curly braces Next: Mapping Templates function betweeen 2 class |