473,394 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 2844
> 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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.