![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 3
![]() |
This Pointer
I've read my book, and there's some topics where I feel weak about:
- inline functions - static members - this pointer - copy constructor I've read some tutorials online, but they use some complicated code, and I'm a beginner programmer, so I can't understand their explanations. Just wondering, if anyone could summarize (tell me their uses) and give me simple examples of those. That would really help me understand what I'm learning .. especially the this pointer! Thanks! |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 3
![]() |
Here was also an example of the this pointer that I didn't understand: (We're transitioning from structs to class)
#include <iostream>
using namespace std;
struct Target
{
private:
int pos;
string history;
public:
void init();
bool move(char dir);
void animateHistory()const;
};
void init (Target *tp)
{
tp -> pos = 0;
tp -> history = "";
}
bool move (Target* tp, char dir)
{
switch (dir)
{
default:
return false;
case 'L':
tp -> pos--;
break;
case 'R':
tp -> pos++;
break;
}
tp -> history += dir;
return true;
}
void animateHistory (const Target* tp)
{
for (int k =0; k != tp -> history.size(); k++)
cout << tp -> history[k] << endl;
}
int main()
{
Target t;
t.init();
t.move('R');
t.move('R');
t.animateHistory();
}became this: struct Target
{
private:
int pos;
string history;
public:
void init();
bool move(char dir);
void animateHistory()const;
};
void Target::init ()
{
this -> pos = 0;
this -> history = "";
}
bool Target::move (char dir)
{
switch (dir)
{
default:
return false;
case 'L':
this -> pos--;
break;
case 'R':
this -> pos++;
break;
}
this -> history += dir;
return true;
}
void Target::animateHistory () const
{
for (int k =0; k != this -> history.size(); k++)
cout << this -> history[k] << endl;
}
int main()
{
Target t;
t.init(); // &t will be passed as the value of the "this" pointer
t.move('R'); // &t will be passed as the value of the "this" pointerI don't understand why we had no parameters (for the second code) for some of the public member functions, or rather how Target* tp "disappeared" from each parameter. What is the cause, and why can we do that? from here (part of 2nd code): public:
void init();
bool move(char dir);
void animateHistory()const; |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Classes may be instantiated multiple times. Non-static variables are replicated for each instance. There is only one copy of methods, on the other hand. What instance to operate on, then? The one pointed to by the this pointer. It is real. It is actually passed, just as any other argument. Because it is always passed, in order to indicate which instance the method is to act on, it is merely not shown (it is implicit). If you were to look at the emitted assembly code, you would see it.
Now, a question for you: what do you see as the difference between classes and structs, since you're making the transition. What do you want to know about static members and copy constructors? Incidentally, have you tried looking for explanations via Google? A hit gives you immediate answers. It works well for tutorial material. The kinds of problems you might have with specific code that you write is another story, since it's often a problem specific to you. Forums are great for that.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 3
![]() |
Can someone explain how the 'implicitly pass variables' work in the code that I had? That's the idea I don't understand the most.
Also, in class, we don't use the this pointer |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5
![]() |
Every time a non-static member function is called, it receives a "this" pointer. You, the programmer, don't have to do that because the compiler takes care of the work of ensuring that. This is said to be an implicitly passed data, because the compiler does it for you, and it is not necessary for your code to explicitly pass data to a member function identifying the object being acted on.
The "this" pointer can be used in any non-static member function, because the compiler always implicitly passes it. For example; struct X
{
X();
void func();
}
void X::func()
{
// examine the this pointer
}
int main()
{
X x;
X y;
x.func(); // the "this" pointer in func() will be the address of x (&x)
y.func(); // the "this" pointer in func() will be the address of y (&y)
} |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
i talk
you talk bill talks we are all objects. you, me, and bill. if for some crazy reason we wanted to know WHO was talking at the time we would simply refer to the "this" pointer. or if we wanted to do something to the conversationalist. talk is a function, called by an object. when we want to refer to the "caller" of the function, we use "this", b/c oftentimes the caller is not explicitely stated as a function parameter. this way, we always have a way to reach him. "WHO SAID THAT!?!" well..."this" said it. most handy when overloading operators and such. if it doesn't make sense, study some more. try examples.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Combining languages | titaniumdecoy | Other Programming Languages | 12 | Jul 13th, 2006 3:03 PM |
| Pointer Problems | Eryk | C++ | 5 | Nov 26th, 2005 5:45 PM |
| HELP please!!! | hamacacolgante | C | 7 | Nov 21st, 2005 6:36 AM |
| Pointers in C (Part II) | Stack Overflow | C | 2 | Apr 29th, 2005 11:39 AM |
| Pointers in C (Part I) | Stack Overflow | C | 4 | Apr 28th, 2005 8:03 PM |