943,752 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 667
  • C++ RSS
Aug 29th, 2009
0

auto_ptr implementation v.basic for learning purposes

Expand Post »
I have the following code and it wont compile, it says no matching function for call to `autop::autop(autop)' at this part
C++ Syntax (Toggle Plain Text)
  1. autop j = f();


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

C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 11
Solved Threads: 1
Light Poster
namehere05 is offline Offline
25 posts
since Dec 2008
Aug 30th, 2009
0

Re: auto_ptr implementation v.basic for learning purposes

create one more construtor without argument
autop() { }
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
Rhohitman is offline Offline
81 posts
since Dec 2007
Aug 30th, 2009
0

Re: auto_ptr implementation v.basic for learning purposes

it didnt work
Reputation Points: 11
Solved Threads: 1
Light Poster
namehere05 is offline Offline
25 posts
since Dec 2008
Aug 30th, 2009
0

Re: auto_ptr implementation v.basic for learning purposes

Click to Expand / Collapse  Quote originally posted by namehere05 ...
I have the following code and it wont compile, it says no matching function for call to `autop::autop(autop)' at this part
C++ Syntax (Toggle Plain Text)
  1. autop j = f();
I got no compiler error with that code.

Quote ...
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
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Aug 30th, 2009
0

Re: auto_ptr implementation v.basic for learning purposes

LOL. That's a lot of PP, so if that was supposed to be a joke you got me.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Aug 30th, 2009
0

Re: auto_ptr implementation v.basic for learning purposes

Here are the results
writting
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 1
Light Poster
namehere05 is offline Offline
25 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: 2d Arrays e FUSSY - Store data in 1d area
Next Thread in C++ Forum Timeline: Variable # of args for a class template





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC