tough (to me) c++ questions about pointer to class object reference and more
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
tough (to me) c++ questions about pointer to class object reference and more
my first confusion came out after reading an example posted at c++ faq lite:
class Wilma { };
class Fred {
public:
Fred() : p_(new Wilma()) { }
Fred(const Fred& f) : p_(new Wilma(*f.p_)) { }//the line confuses me
~Fred() { delete p_; }
Fred& operator= (const Fred& f)
{ // Bad code: Doesn't handle self-assignment!
delete p_; // Line #1
p_ = new Wilma(*f.p_); // Line #2
return *this;
}
private:
Wilma* p_;
};
I don't quite understand the meaning of the copy constructor at "the line confuses me".
Why is *f used here? Usually, when pass a reference such as "className& myRef", we can use
myRef to directly access the class members.
In addition, why can the copy constructor access Fred's private member?
I modified the original code this way: in class Fred, I replaced Wilma with int and
made p_ a public member. And wrote a function called func1(Fred &obj). In func1,
I just tried to assign an int pointer: int *ptr = new int(*obj.p_) which works fine.
But if p_ was a private member in class Fred, then compiler says:
int* Fred:: p_ is private
But in the original example, p_ was indeed private.
And if I do this way: int* ptr = new int *obj.p_; then the error becomes:
cannot convert int** to int* in initialization
Is this because we have to evaluate the right-hand from right to left?
Here, what's the meaning of int()? type conversion or something else?
I don't quite understand the meaning of the copy constructor at "the line confuses me".
Why is *f used here? Usually, when pass a reference such as "className& myRef", we can use
myRef to directly access the class members.
What you have discovered is that simple member access (“.”) binds more closely than pointer dereferencing (“*”). So in your example, this:
Code:
*f.p_
is parsed as this:
Code:
*(f.p_)
Quote:
Originally Posted by parv
In addition, why can the copy constructor access Fred's private member?
Privacy is not by object but by class. Another way to think of it is that every class is implicitly its own friend.
Quote:
Originally Posted by parv
I modified the original code this way: in class Fred, I replaced Wilma with int and
made p_ a public member. And wrote a function called func1(Fred &obj). In func1,
I just tried to assign an int pointer: int *ptr = new int(*obj.p_) which works fine.
But if p_ was a private member in class Fred, then compiler says:
int* Fred:: p_ is private
But in the original example, p_ was indeed private.
I think you did something wrong. The following works fine for me:
Code:
class Fred {
public:
void func(const Fred& f) {int *ptr = new int(*f.p_); delete ptr;}
private:
int* p_;
};
Thanks for your post.
For the last part, I added one statement in func(const Fred& f)
as shown in the code below. Then I encountered a problem.
Fred abc(20); //to set p_ points to 20
abc.func(abc);
I suppose to see 20 to be printed out, but in fact some weird value is printed.
The value of f.p_ is still correct.
So why cannot I deference src.ptr correctly? What am I missing?
I tried to remove the "const" in const Fred& f while passing argument, but no difference.
But when I tried to print out *ptr
I think you did something wrong. The following works fine for me:
Code:
class Fred {
public:
void func(const Fred& f) {int *ptr = new int(*f.p_); cout<<*ptr; delete ptr;}//new code
private:
int* p_;
};
I looked at the values of ptr and *ptr before and after calling the
copy constructor to create a new object.
Fred oldObj(20);
Fred newObj(oldObj);
The values of *ptr changed from 20 to a negative value which is
clearly incorrect. How could this happen? I even tried to let the
copy constructor to initialize the ptr to point to a const value, i.e.,
has nothing to do with the parameter actually. However, *oldObj.ptr
still changes.
Quote:
Originally Posted by parv
Thanks for your post.
For the last part, I added one statement in func(const Fred& f)
as shown in the code below. Then I encountered a problem.
Fred abc(20); //to set p_ points to 20
abc.func(abc);
I suppose to see 20 to be printed out, but in fact some weird value is printed.
The value of f.p_ is still correct.
So why cannot I deference src.ptr correctly? What am I missing?
I tried to remove the "const" in const Fred& f while passing argument, but no difference.
But when I tried to print out *ptr
I think you did something wrong. The following works fine for me:
Code:
class Fred {
public:
void func(const Fred& f) {int *ptr = new int(*f.p_); cout<<*ptr; delete ptr;}//new code
private:
int* p_;
};
found out the problem was I had the constructor Fred(int val).
After changing it to Fred(int &val), seems working.
Why did this make such a big difference?
Quote:
Originally Posted by parv
I looked at the values of ptr and *ptr before and after calling the
copy constructor to create a new object.
Fred oldObj(20);
Fred newObj(oldObj);
The values of *ptr changed from 20 to a negative value which is
clearly incorrect. How could this happen? I even tried to let the
copy constructor to initialize the ptr to point to a const value, i.e.,
has nothing to do with the parameter actually. However, *oldObj.ptr
still changes.
found out the problem was I had the constructor Fred(int val).
After changing it to Fred(int &val), seems working.
Why did this make such a big difference?
I still think you did something wrong. The following complete program should work fine:
As you can see the constructor takes an int (by value, not reference) as its argument. The copy constructor is there so you can have functions of the form of Fred::func2() which take a Fred as their argument by a temporary copy. In both cases, access to the private member of the Fred instances is allowed by the compiler.
Here’s the result (as expected):
Code:
$ ./fred
5
10
If I had added a third function with this signature:
Code:
void Fred::func3(Fred& f)
I would even be permitted to modify the private member of the argument (since it would be passed by reference, but not by const reference).
If you want more help with your problem, please give us the entire program that fails to compile (and the error messages).
after reading your reply, I figured out that I did not initialize one of the pointers
in one constructor. Now, I have removed all the problems and I feel I have a much
better understanding of several aspects.
Thanks so much for your kind help.
Have a good weekend.
Quote:
Originally Posted by osor
I still think you did something wrong. The following complete program should work fine:
As you can see the constructor takes an int (by value, not reference) as its argument. The copy constructor is there so you can have functions of the form of Fred::func2() which take a Fred as their argument by a temporary copy. In both cases, access to the private member of the Fred instances is allowed by the compiler.
Here’s the result (as expected):
Code:
$ ./fred
5
10
If I had added a third function with this signature:
Code:
void Fred::func3(Fred& f)
I would even be permitted to modify the private member of the argument (since it would be passed by reference, but not by const reference).
If you want more help with your problem, please give us the entire program that fails to compile (and the error messages).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.