object as pointer

when is object a pointer? I saw it in some book; a part of a method implementation in that class looked like so:

String& operator=(const String& rhs)
{
if(this == &rhs)
return *this;
//...
}

And also: when returning object(in class method of course), you can only return it with pointer or reference to it rigth? Theres no other way.
when is object a pointer?


Err.. this question doesn't really make sense in this context. Objects and pointers are two entirely different things.

Objects are "things". For example if I say: string foo;, that means that foo actually is a string. On the other hand, pointer are not objects themselves, but rather they tell you where to find / how to access other objects.

string* bar; bar is not a string, it tells you where to find a string. However, *bar is a string, assuming bar points to a valid object.

The this keyword gives you a pointer to the "current" object being acted on. Simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class ShowThis
{
public:
  void print()
  {
    cout << this;  // 'this' is the address of whatever object we're acting on
    cout << endl;
  }
};

int main()
{
  ShowThis a, b;  // create two objects

  a.print();  // since we're acting on 'a', 'this' will point to 'a'
  b.print();  // but now, 'this' will point to 'b' because we're acting on b

  // A simple way to test:
  cout << &a << " = "
  a.print();  // will print the same address twice

  cout << &b << " = "
  b.print();  // ditto
}



The code snippit you posted is a common technique:

1
2
3
4
5
6
String& operator=(const String& rhs)
{
  if(this == &rhs)
    return *this;
  //...
}


This is known as "preventing self assignment". If this == &rhs is true, that means that an object is being assigned to itself. For instance if you did this:

1
2
string a = "foo";
a = a;  // self assignment 


In this case, the assignment operator doesn't need to do anything (and probably shouldn't), so it can just exit early.


And also: when returning object(in class method of course), you can only return it with pointer or reference to it rigth? Theres no other way.


You can also return a copy of it by value, but if you want to return the original object (and not a copy), then yes it has to be by ref or by pointer.
Last edited on
tnx, the problem was ive forgotten that this is actually a pointer... Anyway, i have two more quastions; namely, if i use if(pointer), does that mean, if it is 0 (it points to nothing) it means false and any other address it points to is treated as true? And if you print object with cout - cout << object, what does this line print?
Last edited on
The first question is yes, if you use if(pointer) that will only be true if the pointer is a non zero value.

The second question is an error by default, you would have to define the behavior, either by extending cout, or by defining object to have an implicit cast to an object that cout can handle.
Last edited on
Sorry, i forgot to mention that object in this case is a pointer (because it is derived and thats why its using dynamic memory)...

Also, if a function has a parameter like so - const int& value, and the function does not change value, the const keyword is obvious, but why reference? We would only need to use it when we would like to change value and the variable to which it refers to; so why dont we use const int value instead, because we dont need references to variables passed in as parameters as opposed to objects and arrays where they are a must?
Last edited on
Topic archived. No new replies allowed.