473,321 Members | 1,667 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,321 software developers and data experts.

what does "cout<<*this;" do?!

this is a statement i have within a method(function)
May 15 '12 #1
2 5000
this is the pointer to itself, so *this refers to itself. And cout<<*this prints it to the standard output.

Eg,
Expand|Select|Wrap|Line Numbers
  1. class complex{
  2.   int x,y;
  3.    ....
  4.   ostream& operator<<(ostream&);//this defines the operator "<<"
  5.   complex& operator/(complex);
  6. }
  7.  
  8. complex complex::operator/(complex z){
  9.   if ((z==0)){//if divided by 0, print information
  10.     cout<<*this<<" can't be divided by "<<z;
  11.     return 0;
  12.   }
  13.   else{
  14.     ...;
  15.   }
  16. }
May 15 '12 #2
weaknessforcats
9,208 Expert Mod 8TB
You may not understand the this pointer.

C++ requires every function to receive all data using arguments.

If you look at your class member functions you will see that no member function has an argument for the class itself. Also yu will see that no member function has an argument for which object to work with.

What happens is that the address of the object is passed in by the compiler as an invisible first argument.

Expand|Select|Wrap|Line Numbers
  1. class MyClass
  2. {
  3. private:
  4.     int data;
  5. public:
  6.     void Add(int x);
  7. };
  8. void MyClass::Add(int x)
  9. {
  10.       data = x;
  11.  }
You can't tell what the "data" is. It might be a global variable ot it might be inside a MyClass object but you can't tell which one. The correct way to write this meber function is:
Expand|Select|Wrap|Line Numbers
  1. void MyClass::Add(int x)
  2. {
  3.       this->data = x;
  4.  }
This says that data is inside the MyClass object pointed at by this. That makes this a MyClass*. You don't see the argument for a MyClass* in the function but it is there as an invisible first argument.

So if this is a MyClass*, then *this must be a MyClass variable.

Therefore, cout << *this is a call to an operator<< function that has an ostream and a MyClass argument.

Whereas cout << this is a call to an operator<< function that has an ostream and a MyClass* argument.

I hope this helps.
May 16 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Ensoul Chee | last post by:
I used #include <iostream.h> int m; cout << "Hexadecimal == 0x" << hex << m << endl; to print value of m in hexadecimal mode. But I got the compile error like this couttest.cpp:20 `hex'...
4
by: karolina | last post by:
Hi I am playing around with visual studio 7.0. Why does the code below do like it does? Where do I set the character set to use in cout, properties->configuration properties->General->character...
16
by: Michael | last post by:
just a quickie: as cout << endl; is to cout << "\n"; is there an equivalent for cout << "\t";
2
by: Generic Usenet Account | last post by:
What exactly is the difference between the hex manipulator and the following statement: cout.setf(ios_base::hex)? According to Stroustrup, Third Edition, Section 21.4.4, "once set, a base is...
4
by: wangzhihuii | last post by:
Hi all, I'm really confused, can cout<<""; contribute anything to the routine ?!! my programm won't work properly without this trivial sentence. Sincerely vivian
12
by: Filipe Sousa | last post by:
Hi! Could someone explain to me why this operation is not what I was expecting? int main() { int x = 2; std::cout << x << " " << x++ << std::endl; return 0; }
5
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space...
4
by: ek | last post by:
I thought that the syntax for c++ in either winXP or linux was the same. In Ubuntu linux it works fine if I write: #include <string> using namespace std; int main() {
1
by: Sunny | last post by:
The way to overload operator << is : ostream& operator << (ostream& os, const Obj& obj); and this is a member function. My question is why do we need to provide a const reference of Obj as...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.