LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-10-2008, 03:49 PM   #1
parv
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora Core 10, Ubuntu 8.10, CentOS 5
Posts: 180

Rep: Reputation: 30
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?

Many thanks for clearing my doubts.
 
Old 01-10-2008, 08:12 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 64
Quote:
Originally Posted by parv View Post
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 View Post
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 View Post
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_;
};
 
Old 01-11-2008, 12:00 PM   #3
parv
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora Core 10, Ubuntu 8.10, CentOS 5
Posts: 180

Original Poster
Rep: Reputation: 30
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_;
};
[/quote]
 
Old 01-11-2008, 12:09 PM   #4
parv
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora Core 10, Ubuntu 8.10, CentOS 5
Posts: 180

Original Poster
Rep: Reputation: 30
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 View Post
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_;
};
[/quote]
 
Old 01-11-2008, 12:33 PM   #5
parv
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora Core 10, Ubuntu 8.10, CentOS 5
Posts: 180

Original Poster
Rep: Reputation: 30
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 View Post
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]
 
Old 01-11-2008, 06:52 PM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 64
Quote:
Originally Posted by parv View Post
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:
Code:
#include <iostream>

class Fred {
	public:
		Fred(int val = 0)   : p_(new int(val))   { }
		Fred(const Fred& f) : p_(new int(*f.p_)) { }
		void func1(const Fred& f) {
			int *ptr = new int(*f.p_);
			std::cout << *ptr << "\n";
			delete ptr;
		}
		void func2(Fred f) { std::cout << *f.p_ + 5 << "\n"; }
		~Fred() { delete p_; }
	private:
		int* p_;
};

int main() {
	Fred f, g(5);
	f.func1(g);
	f.func2(g);
}
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).
 
Old 01-11-2008, 09:26 PM   #7
parv
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora Core 10, Ubuntu 8.10, CentOS 5
Posts: 180

Original Poster
Rep: Reputation: 30
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 View Post
I still think you did something wrong. The following complete program should work fine:
Code:
#include <iostream>

class Fred {
    public:
        Fred(int val = 0)   : p_(new int(val))   { }
        Fred(const Fred& f) : p_(new int(*f.p_)) { }
        void func1(const Fred& f) {
            int *ptr = new int(*f.p_);
            std::cout << *ptr << "\n";
            delete ptr;
        }
        void func2(Fred f) { std::cout << *f.p_ + 5 << "\n"; }
        ~Fred() { delete p_; }
    private:
        int* p_;
};

int main() {
    Fred f, g(5);
    f.func1(g);
    f.func2(g);
}
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).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Object Reference problem in Java servlets Hockeyfan Programming 1 06-16-2005 01:36 AM
Talk to an object in a diferent class bastl Programming 9 03-17-2005 01:36 PM
C++ class-object? shivaligupta Programming 2 01-06-2005 02:25 AM
Event driven object-to-object: C++ template class mecanism ( NOT STL or STDC++) bretzeltux Programming 2 12-23-2003 02:45 PM
returning a reference to Object C++ chens_83 Programming 3 10-10-2003 09:16 PM


All times are GMT -5. The time now is 05:36 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration