Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 28th, 2006, 7:20 PM   #1
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 3 aznballerlee is on a distinguished road
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!
aznballerlee is offline   Reply With Quote
Old Nov 28th, 2006, 7:32 PM   #2
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 3 aznballerlee is on a distinguished road
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" pointer

I 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;
aznballerlee is offline   Reply With Quote
Old Nov 28th, 2006, 8:33 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 29th, 2006, 1:44 AM   #4
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 3 aznballerlee is on a distinguished road
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
unless we have pointers? Is that right?
aznballerlee is offline   Reply With Quote
Old Nov 29th, 2006, 6:46 AM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,318
Rep Power: 5 grumpy will become famous soon enough
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)
}
The physical mechanism by which the compiler passes the this pointer is compiler dependent.
grumpy is offline   Reply With Quote
Old Nov 30th, 2006, 4:08 AM   #6
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC