473,507 Members | 6,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

this pointer

Some questions about "this" pointer:

1. To make an assignment function,

Stribg &String::operator=(const String &other)
{
...
return *this;
}

I have hard time to understand why "*this" matches a "String &" here. Could
someone shed some light on this?

2. In a static member function aren't there "this" pointer accessible inside
it?

3. In a const member function is the "this" pointer of const type while
non-const type in a non-const member?

Thanks in advance!


Jul 19 '05 #1
2 2852
> Some questions about "this" pointer:

1. To make an assignment function,

Stribg &String::operator=(const String &other)
{
...
return *this;
}
The return type is a String & (String reference) and the assignment
operator is passing back a reference to itself one the assignment is done.
I have hard time to understand why "*this" matches a "String &" here. Could someone shed some light on this?

2. In a static member function aren't there "this" pointer accessible inside it?
No there is no this pointer in a static member function.
3. In a const member function is the "this" pointer of const type while
non-const type in a non-const member?
Yes the this pointer is a const pointer in a const member function.
Thanks in advance!


Jul 19 '05 #2
"stub" <st**@asof.com> wrote in message news:1qzob.22753
$E**********@bgtnsc05-news.ops.worldnet.att.net...
Some questions about "this" pointer:

1. To make an assignment function,

Stribg &String::operator=(const String &other)
{
...
return *this;
}

I have hard time to understand why "*this" matches a
"String &" here. Could someone shed some light on this?
You return *this in an assignment operator so that
it's possible to do things like:

a=b=c; // a,b,c are String's and
// b returns (*this) so that a can be assigned
2. In a static member function aren't there "this" pointer
accessible inside it?


Static methods have no "this" pointer, because they
are not associated with a particular instance of an object.

HTH,
- J.
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2310
by: ali | last post by:
Hi, I'm trying to understand the reason for different output on the following codes Code1: #include <iostream.h> int main()
9
2504
by: iceColdFire | last post by:
HI, I have a function as void f(int p) { return p++; } now I have created a function pointer as
20
6525
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
67
4389
by: Ike Naar | last post by:
Hi, Asking your advice on the following subject: Suppose I want to find out whether a given pointer (say, p) of type *T points to an element of a given array (say, a) of type T. A way to...
33
4836
by: dough | last post by:
Is it possible in C to declare and initialize a pointer that points to itself? Why or why not?
1
3078
by: Jeff | last post by:
I am struggling with the following How do I marshal/access a pointer to an array of strings within a structure Than Jef ----------------------------------------------------------------
16
10349
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
15
3723
by: khan | last post by:
Hi, I read that pointer representation can non-zero bit pattern, machine specific.Compiler when comes accross value '0' in pointer context, converts it to machine specific null pointer...
6
2294
by: lithiumcat | last post by:
Hi, maybe you remember me, some time ago I asked about how to store an integer value into a void*, and I learned that doing pointer arithmetic yeilding a pointer outside of an object (except the...
14
1958
by: Szabolcs Borsanyi | last post by:
Deal all, The type typedef double ***tmp_tensor3; is meant to represent a three-dimensional array. For some reasons the standard array-of-array-of-array will not work in my case. Can I...
0
7313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7029
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1537
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.