| | |
auto_ptr implementation v.basic for learning purposes
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2008
Posts: 21
Reputation:
Solved Threads: 1
I have the following code and it wont compile, it says no matching function for call to `autop::autop(autop)' at this part
I don't understand why the copy constructor wont accept the autop copy Im sending it.
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
so question why adding an & makes the '*' match to the compiler
Does taking the address of anything gives me a pointer or what
C++ Syntax (Toggle Plain Text)
autop j = f();
I don't understand why the copy constructor wont accept the autop copy Im sending it.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class autop { public: autop(int * s){ p = s ; } //init the protectee autop(autop & s) { p = s.p ; } autop & operator=(autop & s){ p = s.p ; } //first indicates a ****ing reference ~autop(){delete p;} int * p; }; autop f(){ autop p (new int(7)); return p; } int main() { autop j = f(); system("pause"); }
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)
int * p int x; p = x //wont work AND compiler says: invalid conversion from int to int* //same applies to int ** pp; int * p pp = p;
Does taking the address of anything gives me a pointer or what
•
•
Join Date: Jul 2009
Posts: 972
Reputation:
Solved Threads: 213
•
•
•
•
I have the following code and it wont compile, it says no matching function for call to `autop::autop(autop)' at this partC++ Syntax (Toggle Plain Text)
autop j = f();
•
•
•
•
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
so question why adding an & makes the '*' match to the compilerC++ Syntax (Toggle Plain Text)
int * p int x; p = x //wont work AND compiler says: invalid conversion from int to int* //same applies to int ** pp; int * p pp = p;
Does taking the address of anything gives me a pointer or what
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
•
•
Join Date: Dec 2008
Posts: 21
Reputation:
Solved Threads: 1
Here are the results
writting
// I didn't quite got what you said in paragraph 2 but thinking it well it helps what you said on first paragraph , 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
it won't compile it says the familiar invalid conversion from int to int*
in this part 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
writting
C++ Syntax (Toggle Plain Text)
int ** pp; int * p; int v = 5; p = &v; //you say "pointers point to references " *pp = p; //Therefore, you could say *pp = p , because "p" //is a pointer and you are assigning it to a pointer cout << **p; //BUT this crashes when you run the program! //what did work was pp = &p; 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)
pointers point to references
well this so far makes sense but what happens in the next case
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class ppr{ public: ppr(int & r): p(r){} int * p; }; int main() { int x = 5 ; ppr rr(x); cout << *(rr.p); // shuts system("pause"); }
it won't compile it says the familiar invalid conversion from int to int*
in this part
C++ Syntax (Toggle Plain Text)
ppr(int & r): p(r){}
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.
![]() |
Similar Threads
- Fun Questions... (Python)
- Using auto_ptr or shared_ptr to allocate memory to a structure (C++)
- cyclic codes help (C++)
- need help with atm coding again (Python)
- Icons (Visual Basic 4 / 5 / 6)
- Modern Replacement for Basic (Legacy and Other Languages)
- Switching From Explorer.exe to Progman.exe (Windows NT / 2000 / XP)
- Anybody can help me make a game? (Java)
- multi-boot OS problems trying to boot to C drive I get error mess non sys disk nodisk (Windows NT / 2000 / XP)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: 2d Arrays e FUSSY - Store data in 1d area
- Next Thread: Variable # of args for a class template
Views: 264 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






LOL. That's a lot of PP, so if that was supposed to be a joke you got me.