auto_ptr implementation v.basic for learning purposes

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 21
Reputation: namehere05 is an unknown quantity at this point 
Solved Threads: 1
namehere05 namehere05 is offline Offline
Newbie Poster

auto_ptr implementation v.basic for learning purposes

 
0
  #1
Aug 29th, 2009
I have the following code and it wont compile, it says no matching function for call to `autop::autop(autop)' at this part
  1. autop j = f();


I don't understand why the copy constructor wont accept the autop copy Im sending it.

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class autop
  5. {
  6. public:
  7. autop(int * s){ p = s ; } //init the protectee
  8. autop(autop & s) { p = s.p ; }
  9. autop & operator=(autop & s){ p = s.p ; } //first indicates a ****ing reference
  10. ~autop(){delete p;}
  11. int * p;
  12. };
  13.  
  14. autop f(){
  15. autop p (new int(7));
  16. return p;
  17.  
  18. }
  19.  
  20. int main()
  21. {
  22.  
  23. autop j = f();
  24.  
  25.  
  26. system("pause");
  27. }


Another Thing:

when used inside the function parameter list (i.e autop(autop & s) , whats the effect of the &, does it creates a ref, or is it passing an address, is it the same( I dont see how) , how should I understand it.

I also wonder why using & to get the address of anything adds a '*' i.e if I have
  1. int * p
  2. int x;
  3. p = x //wont work AND compiler says: invalid conversion from int to int*
  4. //same applies to
  5. int ** pp;
  6. int * p
  7. pp = p;
so question why adding an & makes the '*' match to the compiler
Does taking the address of anything gives me a pointer or what
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: auto_ptr implementation v.basic for learning purposes

 
0
  #2
Aug 30th, 2009
create one more construtor without argument
autop() { }
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: namehere05 is an unknown quantity at this point 
Solved Threads: 1
namehere05 namehere05 is offline Offline
Newbie Poster

Re: auto_ptr implementation v.basic for learning purposes

 
0
  #3
Aug 30th, 2009
it didnt work
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 903
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 144
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: auto_ptr implementation v.basic for learning purposes

 
0
  #4
Aug 30th, 2009
Originally Posted by namehere05 View Post
I have the following code and it wont compile, it says no matching function for call to `autop::autop(autop)' at this part
  1. autop j = f();
I got no compiler error with that code.

Another Thing:

when used inside the function parameter list (i.e autop(autop & s) , whats the effect of the &, does it creates a ref, or is it passing an address, is it the same( I dont see how) , how should I understand it.

I also wonder why using & to get the address of anything adds a '*' i.e if I have
  1. int * p
  2. int x;
  3. p = x //wont work AND compiler says: invalid conversion from int to int*
  4. //same applies to
  5. int ** pp;
  6. int * p
  7. pp = p;
so question why adding an & makes the '*' match to the compiler
Does taking the address of anything gives me a pointer or what
Because Pointers point to References, you need to resolve a variable to its reference when assigning to a pointer. Therefore, to say that int *p = &x is to say "p" points to reference "x". When you try int *p = x , you are attempting to say an address pointer points to an integer value, which is why you get an error because this code is suspect of being in error.

Part 2: Your **pp is a pointer to a pointer, so when you dereference "**pp" as *pp, it must be a pointer, but if you want to assign to "**pp", you must assign the equivalent pointer to a pointer. Therefore, you could say *pp = p , because "p" is a pointer and you are assigning it to a pointer (contents of **pp written as "*p").
Last edited by DdoubleD; Aug 30th, 2009 at 1:48 am. Reason: Part 2
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 903
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 144
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: auto_ptr implementation v.basic for learning purposes

 
0
  #5
Aug 30th, 2009
LOL. That's a lot of PP, so if that was supposed to be a joke you got me.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: namehere05 is an unknown quantity at this point 
Solved Threads: 1
namehere05 namehere05 is offline Offline
Newbie Poster

Re: auto_ptr implementation v.basic for learning purposes

 
0
  #6
Aug 30th, 2009
Here are the results
writting
  1. int ** pp;
  2. int * p;
  3. int v = 5;
  4.  
  5. p = &v; //you say "pointers point to references "
  6. *pp = p; //Therefore, you could say *pp = p , because "p"
  7. //is a pointer and you are assigning it to a pointer
  8. cout << **p; //BUT this crashes when you run the program!
  9.  
  10. //what did work was
  11. pp = &p;
  12. cout << **p;

// I didn't quite got what you said in paragraph 2 but thinking it well it helps what you said on first paragraph
  1. pointers point to references
, Im generalizing this as "any pter(single, pointer to pter, ppp, pppp , jaja... etc )" points to a reference of its pointee

well this so far makes sense but what happens in the next case

  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. class ppr{
  6. public:
  7. ppr(int & r): p(r){}
  8. int * p;
  9. };
  10.  
  11. int main()
  12. {
  13.  
  14. int x = 5 ;
  15. ppr rr(x);
  16.  
  17. cout << *(rr.p);
  18. // shuts
  19.  
  20. system("pause");
  21. }

it won't compile it says the familiar invalid conversion from int to int*
in this part
  1. ppr(int & r): p(r){}
now I wonder: Why the error. am I not giving the pter a reference to the int alredy!?

BTW about my previous error I can't believe you compiled that without problems, this thing of mine won't let me
Last edited by namehere05; Aug 30th, 2009 at 2:59 am.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC