I have the following code and it wont compile, it says no matching function for call to `autop::autop(autop)' at this part

autop j = f();

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

#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 fucking 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

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;

so question why adding an & makes the '*' match to the compiler
Does taking the address of anything gives me a pointer or what
:confused:

create one more construtor without argument
autop() { }

I have the following code and it wont compile, it says no matching function for call to `autop::autop(autop)' at this part

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

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;

so question why adding an & makes the '*' match to the compiler
Does taking the address of anything gives me a pointer or what
:confused:

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").

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

Here are the results
writting

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

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

#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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.