Re: this pointer ?

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hey guys, once again you have helped me so much and thanks a million!

Oh yeah, you bet and rest assure that I am learning this stuff character by
character and now do understand the meaning of the line:

void SetLenght(int lenght) {*this->itsLenght = lenght;}

its all because itsLenght is a pointer to the heap that it is to be
dereferenced. Basically I understand it this way which by the way also
compiles fine.

*(this->itsLenght)

All constructive feedback is welcome!

--
Best regards
Robert


"Scot T Brennecke" wrote:

> [FYI, the correct spelling is "length", not "lenght"]
>
> Your statement
> itsLenght = new int(2)
> is questionable. Besides missing the trailing semicolon (I imagine you figured that out on your
> own), I'm not sure you are getting what you intended. the "new int(2)" creates an integer variable
> on the heap, and sets its initial value to 2 -- is this what you wanted to do?
>
> You can allocate members on the heap, regardless of how the object itself is stored. Just make
> sure you clean up, as Dan pointed out.
>
> "Robby" <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:BAFB91AC-7B8B-43D1-BCA9-9F421ABAC9E2@xxxxxxxxxxxxxxxx
> > Hi,
> >
> > I ahve a 2 part question.
> > [1]- I am reading a C++ example using the this pointer. It is used in an
> > inline function where they are using the this pointer to assing data to one
> > of the classe's member variable called "itsLenght" see code below.
> >
> > class Rectangle
> > {
> > public:
> > Rectangle();
> > void SetLenght(int lenght) {this->itsLenght = lenght}
> >
> > private:
> > int itsLenght;
> > };
> >
> > int main()
> > {
> > Rectangle theRect;
> > theRect.SetLenght(5);
> > return 0;
> > }
> >
> > The above code fragment works fine, however, what if the classes's member
> > data was created on the heap. Can I use the this pointer to access member
> > data on the heap. I guess not since the following code does not compile. But
> > what if I really wanted to use the this pointer this way, what other
> > alternitive would I have?
> >
> > class Rectangle
> > {
> > public:
> > Rectangle();
> > void SetLenght(int lenght) {this->*itsLenght = lenght}
> >
> > private:
> > int * itsLenght;
> > };
> >
> > Rectangle::Rectangle()
> > {
> > itsLenght = new int(2)
> > }
> > int main()
> > {
> > Rectangle theRect;
> > theRect.SetLenght(5);
> > return 0;
> > }
> >
> >
> > Obviously the {this->*itsLenght = lenght} line doesn't make sence, how would
> > I otherwise do this.
> >
> > [2]-Are we allowed to declare only the classes's member data on the free
> > store or must the class aswell be on the freestore?
> >
> > --
> > Best regards
> > Robert
> > --
> > Best regards
> > Robert
>
>
>
.



Relevant Pages

  • Re: Zoom relative to mouse position
    ... The red rectangle in my code was located at 100,100 point in the initial ... Also, where ever I try to zoom, the red ... This is the behavior I want to see: When I place the mouse pointer over ...
    (comp.lang.java.programmer)
  • Screen Capture question
    ... What I want to do for the user is to change the mouse pointer into a rectangle ... about 100x50 pixels (I may want to change this, but I'm sure you see what I ... picturebox, form has no titlebar, size the picturebox to the size of the form, ...
    (microsoft.public.vb.general.discussion)
  • Re: question about pointers.
    ... same for me that I redeclare the pointer to point again to somewhere ... > danymic memory allocation by now. ... > that fact by placing an arrow in it. ... > rectangle and ends at the thing 'pointed to'. ...
    (comp.lang.cpp)
  • Re: Drawing Graphics and undo this
    ... Regards ... > Look for double buffering, drawing on images, why stuff disappears and ... > Find great Windows Forms articles in Windows Forms Tips and Tricks ... >> when the Right Mouse Button is Pressed and the Rectangle should Resize, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A question on the RECT struct
    ... respectively of the rectangle. ... Aren't they located on the stack, like values, and isn't NEW ... using the new keyword doesn't imply heap allocation. ... Structs are not ALWAYS located on the heap? ...
    (microsoft.public.dotnet.languages.csharp)