Re: what wrong ??
- From: "collay" <mosan1981@xxxxxxx>
- Date: Fri, 8 Jul 2005 01:30:55 +0800
Thank you!
Yes,I have already known that I should use "->" operator.
yet I have another two questions:
[1]use "new" operator will allocate the memory space in heap area,I think.Am
I right?
[2]use"A A1" or "A* A2 = new A",it will allocate memory and invoke the class
A`s constructor.
but what is the action when I use "A* A3"? Does the compiler only
allocate the memory and do not invoke the constructor??
"Scot T Brennecke" <ScotBspamhater@xxxxxxxx> дÈëÏûÏ¢ÐÂÎÅ
:eIateTxgFHA.1248@xxxxxxxxxxxxxxxxxxxxxxx
> A2 and A3 are allocated on the stack as pointers to objects of class A.
For A2, you actually
> created a new A object, and assigned its address to the A2 pointer. But
you left A3 uninitialized.
> It is a pointer, but its value was never set to the address of any A
object. Its value remains
> whatever random "garbage" happened to be on the stack at that point.
>
> Your statements at (2) and (3) will not compile. A2 and A3 are pointers,
and you should use the ->
> operator to access their members. Even so, you'll get an access violation
in statement (3), because
> you are trying to access a non-existent object with your A3 pointer.:
> > cout<<A2->a<<endl;//right (2)
> > cout<<A3->a<<endl;//wrong,Unhandled exception:0xC0000005,access
violation??
> > (3)
>
>
> "collay" <mosan1981@xxxxxxx> wrote in message
news:uvDtt0wgFHA.3868@xxxxxxxxxxxxxxxxxxxxxxx
> > #include <iostream.h>
> > class A
> > {
> > public:
> > int a;
> > };
> >
> > void main(void)
> > {
> > A A1;
> > A* A2 = new A;
> > A* A3;
> > cout<<A1.a<<endl;//right (1)
> > cout<<A2.a<<endl;//right (2)
> > cout<<A3.a<<endl;//wrong,Unhandled exception:0xC0000005,access
violation??
> > (3)
> > }
> >
> > what`s wrong?
> > what did the new operator do when it allocated the memory?
> > what is the different between line(2) and line(3)??
> > Thank you!
> > --
> > collay
> >
> >
>
>
.
- Follow-Ups:
- Re: what wrong ??
- From: Jochen Kalmbach [MVP]
- Re: what wrong ??
- From: Scot T Brennecke
- Re: what wrong ??
- References:
- what wrong ??
- From: collay
- Re: what wrong ??
- From: Scot T Brennecke
- what wrong ??
- Prev by Date: Re: what wrong ??
- Next by Date: Re: what wrong ??
- Previous by thread: Re: what wrong ??
- Next by thread: Re: what wrong ??
- Index(es):
Relevant Pages
|