| | |
This Pointer / Copy Constructor
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 73
Reputation:
Solved Threads: 0
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!
- 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!
•
•
Join Date: Oct 2006
Posts: 73
Reputation:
Solved Threads: 0
Here was also an example of the this pointer that I didn't understand: (We're transitioning from structs to class)
became this:
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):
C++ Syntax (Toggle Plain Text)
#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(); }
C++ Syntax (Toggle Plain Text)
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
from here (part of 2nd code):
C++ Syntax (Toggle Plain Text)
public: void init(); bool move(char dir); void animateHistory()const;
•
•
Join Date: Feb 2005
Posts: 15
Reputation:
Solved Threads: 1
Ok, I figured I'll just point you to a site and then offer my explanation as well. Here's a good link.
http://cplusplus.com/doc/tutorial/classes2.html
This link talks about the "this" keyword and static member data/functions of a class.
This "this" keyword just means "this class".
A Copy Constructor is a special kind of constructor. For example, lets say you have a simple linked list and you want to make a complete copy of it. Dynamic memory and all. Then you would need to make a copy constructor to do this. You could also do operator overloading for the '=' operator, but that's for a later discussion.
There are two types of copying. There's shallow copying where it just copies the members functions and member data and does not copy dynamic data and there's deep copying which does copy dynamic data. Here's a quick picture I whipped up in mspaint to better illustrate my point.
http://img297.imageshack.us/img297/240/copyao2.png
Copy constructors are REALLY useful when you have an array of pointers pointing to some dynamic data and you want to make a copy of it for various operations and such.
Now on to inline functions. Lets say you have two functions in your program. You have int main() and you have float FindAverage(/* your parameter list */). Now let's say you want to inline FindAverage(). What inlining a function does is tell the compiler to replace all function call of FundAverage with the code inside FindAverage. Here's some before and after compiler inline optimization.
This is the code before compiler optimization.
This is how the compiler would treat the code.
The benefits for this is that when you do a normal function call it pushes the current data to the stack and then jumps to the function being called. Afterwards it returns after destroying any data that the function created and pops the data from the stack. So basically the only advantage it has is in function overhead. Using inline doesn't require the stack. However it's only useful for small functions. It should only be used when the code from compiling is faster than the required overhead. It's just another technique for optimization of code.
Here's some links that will explain this concept. It probably does a better way of explaining it then I do.
http://cplusplus.com/doc/tutorial/functions2.html
[edit]I just noticed my explanation and code for inline function has some differentces but I think you should be able to get the point of it.
Hope this helps![/edit]
http://cplusplus.com/doc/tutorial/classes2.html
This link talks about the "this" keyword and static member data/functions of a class.
This "this" keyword just means "this class".
A Copy Constructor is a special kind of constructor. For example, lets say you have a simple linked list and you want to make a complete copy of it. Dynamic memory and all. Then you would need to make a copy constructor to do this. You could also do operator overloading for the '=' operator, but that's for a later discussion.
There are two types of copying. There's shallow copying where it just copies the members functions and member data and does not copy dynamic data and there's deep copying which does copy dynamic data. Here's a quick picture I whipped up in mspaint to better illustrate my point.
http://img297.imageshack.us/img297/240/copyao2.png
Copy constructors are REALLY useful when you have an array of pointers pointing to some dynamic data and you want to make a copy of it for various operations and such.
Now on to inline functions. Lets say you have two functions in your program. You have int main() and you have float FindAverage(/* your parameter list */). Now let's say you want to inline FindAverage(). What inlining a function does is tell the compiler to replace all function call of FundAverage with the code inside FindAverage. Here's some before and after compiler inline optimization.
This is the code before compiler optimization.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; inline void FindAverage(int, int, int&); int main() { int exam1; int exam2; int average; cout << "Enter exam1 score: "; cin >> exam1; cout << endl << "Enter exam2 score: "; cin >> exam2; FindAverage(exam1, exam2); cout << "Average has been computed" << endl; return 0; } inline void FindAverage(int exam1, int exam2, int& avg) { avg = (exam1 + exam2)/2; }
This is how the compiler would treat the code.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int exam1; int exam2; int average; cout << "Enter exam1 score: "; cin >> exam1; cout << endl << "Enter exam2 score: "; cin >> exam2; avg = (exam1 + exam2)/2; // The code for FindAverage has // been substituted in it's place. cout << "Average has been computed" << endl; return 0; }
The benefits for this is that when you do a normal function call it pushes the current data to the stack and then jumps to the function being called. Afterwards it returns after destroying any data that the function created and pops the data from the stack. So basically the only advantage it has is in function overhead. Using inline doesn't require the stack. However it's only useful for small functions. It should only be used when the code from compiling is faster than the required overhead. It's just another technique for optimization of code.
Here's some links that will explain this concept. It probably does a better way of explaining it then I do.

http://cplusplus.com/doc/tutorial/functions2.html
[edit]I just noticed my explanation and code for inline function has some differentces but I think you should be able to get the point of it.
Hope this helps![/edit] Last edited by oRg; Nov 28th, 2006 at 11:08 pm.
•
•
•
•
Here was also an example of the this pointer that I didn't understand: (We're transitioning from structs to class)
became this:C++ Syntax (Toggle Plain Text)
#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(); }
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?C++ Syntax (Toggle Plain Text)
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
from here (part of 2nd code):
C++ Syntax (Toggle Plain Text)
public: void init(); bool move(char dir); void animateHistory()const;
The second code example works, and it's basically using this-> inside of its member functions in front of all the variables it wants to modify, however there's no real need to do this. The following will work just as well:
C++ Syntax (Toggle Plain Text)
struct Target { private: int pos; string history; public: void init(); bool move(char dir); void animateHistory()const; }; void Target::init () { pos = 0; history = ""; } bool Target::move (char dir) { switch (dir) { default: return false; case 'L': pos--; break; case 'R': pos++; break; } history += dir; return true; } void Target::animateHistory () const { for (int k =0; k != history.size(); k++) cout << 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 return 0; }
this-> code. The most useful thing for the this pointer is storing the address of the current instance on an object. For modifying variables, nah...Hope this helps
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Oct 2006
Posts: 73
Reputation:
Solved Threads: 0
So would you say that for classes, the is not useful unless we use pointers?
Also, someone said about passing stuff implicitly, so we have the () empty parameter. Can you explain that? That's confusing me a lot.
C++ Syntax (Toggle Plain Text)
this pointer
Also, someone said about passing stuff implicitly, so we have the () empty parameter. Can you explain that? That's confusing me a lot.
•
•
Join Date: Feb 2005
Posts: 15
Reputation:
Solved Threads: 1
the "this" keyword is a pointer to the class in which it's used. For example:
Is referring to the idNum data of the class your writing for.
So to answer your question, yes you would only need the "this" keyword if your working with pointers because it is essentially a pointer. It's the pointer of a class pointing to itself, if that makes any sense to you.
C++ Syntax (Toggle Plain Text)
this->idNum
So to answer your question, yes you would only need the "this" keyword if your working with pointers because it is essentially a pointer. It's the pointer of a class pointing to itself, if that makes any sense to you.
•
•
•
•
the "this" keyword is a pointer to the class in which it's used. For example:
Is referring to the idNum data of the class your writing for.C++ Syntax (Toggle Plain Text)
this->idNum
So to answer your question, yes you would only need the "this" keyword if your working with pointers because it is essentially a pointer. It's the pointer of a class pointing to itself, if that makes any sense to you.
Correction:
•
•
•
•
The this pointer is a pointer accessible only within the nonstatic member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.
An object's this pointer is not part of the object itself; it is not reflected in the result of a sizeof statement on the object. Instead, when a nonstatic member function is called for an object, the address of the object is passed by the compiler as a hidden argument to the function.
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- Copy Constructor help (C++)
- (C++) Writing a Copy Constructor?!? (C++)
- copy constructor problem (C++)
- need the usage of Copy Constructor (C++)
Other Threads in the C++ Forum
- Previous Thread: Should be an easy question to answer
- Next Thread: factors - 2 functions
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






