Re: this pointer ?
- From: "Robby" <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 11 Aug 2005 11:22:03 -0700
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
>
>
>
.
- References:
- this pointer ?
- From: Robby
- Re: this pointer ?
- From: Scot T Brennecke
- this pointer ?
- Prev by Date: Re: this pointer ?
- Next by Date: Re: Organizing source in namespaces.
- Previous by thread: Re: this pointer ?
- Next by thread: Re: Windows Service -> Dependencies
- Index(es):
Relevant Pages
|