 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
kiran Guest
|
Posted: Sun Nov 26, 2006 4:20 am Post subject: private data access in constructors |
|
|
Consider a member of function of certain class. Suppose an object of
same class type is passed as parameter to that function, for example:
Class X {
--- data & functions
}
void X::func(X& obj)
{
....
}
Now we know that private (or protected) data of obj are accessible
within 'func', however this is not so in the constructors. That is,
X::X(X& obj)
{
data = obj.data // illegal
}
Why is this so?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Thomas Rydzynski Guest
|
Posted: Sun Nov 26, 2006 8:18 am Post subject: Re: private data access in constructors |
|
|
kiran wrote:
| Quote: | Consider a member of function of certain class.
You mean a method? |
| Quote: | Suppose an object of same class type is passed as parameter to that
function, for example:
Class X {
--- data & functions
}
void X::func(X& obj)
{
...
}
Now we know that private (or protected) data of obj are accessible
within 'func',
Yes, because func is a method of X and as a part of class X it has access |
to all Xs members.
| Quote: | however this is not so in the constructors. That is,
X::X(X& obj)
{
data = obj.data // illegal
}
Why is this so?
It is not. Your example code, on the other hand, will not work anyway. You |
use 'data' first as a type, and then as a member, so I really don't know
what you mean.
Regards
--
Thomas 'QsoRiX' Rydzynski Linux Registered User #178082
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Daniel Krügler Guest
|
Posted: Mon Nov 27, 2006 1:17 am Post subject: Re: private data access in constructors |
|
|
Thomas Rydzynski schrieb:
| Quote: | kiran wrote:
Consider a member of function of certain class.
You mean a method?
|
The concept of "method" does not exist in C++ (It does exist in Delphi,
Java, etc.). The proper term is "function member" and that is basically
what the OP has expressed.
| Quote: | Now we know that private (or protected) data of obj are accessible
within 'func',
You use 'data' first as a type, and then as a member, so I really don't know
what you mean.
|
Although the OP's snippet is somewhat contrived, I don't think,
that the OP meant data to be used as a type. As I understand it,
the term data used in
--- data & functions
simply represents the sum of all data members (and 'functions' the
sum of all function members) while the identifier data used in
X::X(X& obj)
{
data = obj.data // illegal
}
stands for one concrete data member named data. Of course this is just
my interpretation and you are right, that the OP should have given a
better example.
There exist no general rule that the above assignment inside
the c'tor of X is illegal, but we can create examples, where this
is so, e.g. if data is a constant
class X {
const int data;
};
or of reference type,
class X {
int& data;
};
In these cases data has to be initialized in the c'tors member
initializer list and a second initialization inside the c'tor body is
not allowed (In case of a reference this would have another
meaning). On the other hand these cases would also
forbid a reassignment in any other member function of X, so these
examples would not prove the OP's assertion. So without any
more information from the OP's side I can only explain the expressed
assertion by a misunderstanding of C++ concepts.
Greetings from Bremen,
Daniel Krügler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Jiang Guest
|
Posted: Mon Nov 27, 2006 1:19 am Post subject: Re: private data access in constructors |
|
|
Greg Herlihy wrote:
| Quote: | Thomas Rydzynski wrote:
kiran wrote:
|
[snip]
| Quote: |
There are two ways to improve the constructor that I would suggest:
first, it is more efficient to initialize class members in the
constructor's initializer list than in its body. Therefore, moving
data's initialization out of the body would be my first suggestion.
|
So what if the passed in object is the object itself ?
| Quote: | Second, arguments passed by reference should be declared const whenever
possible. Since X's copy constructor does not modify the X object being
copied, the parameter should be declared a const reference.
|
Yes, but the copy constructor does accept X&, together with volatile X&
and const volatile X&.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Daniel Krügler Guest
|
Posted: Mon Nov 27, 2006 8:48 am Post subject: Re: private data access in constructors |
|
|
Abhishek Padmanabh schrieb:
| Quote: | kiran wrote:
Now we know that private (or protected) data of obj are accessible
within 'func', however this is not so in the constructors. That is,
X::X(X& obj)
{
data = obj.data // illegal
}
Why is this so?
What you have written effectively is one of the possible copy
constructors of a class. If that would have had been illegal how would
copy constructors work?
|
Despite the fact, that some kinds of data members *must* be initialized
in the c'tor mem-initializer list and cannot be reset in the c'tor body
(const data members or references, e.g.). From the standpoint of
access level limitations your observation is absolutely correct
and there should be no difference between the c'tor and an ordinary
member function.
Greetings from Bremen,
Daniel Krügler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Thomas Rydzynski Guest
|
Posted: Mon Nov 27, 2006 8:48 am Post subject: Re: private data access in constructors |
|
|
Alberto Ganesh Barbati wrote:
| Quote: | Thomas Rydzynski ha scritto:
X::X(X& obj)
{
data = obj.data // illegal
}
Why is this so?
It is not. Your example code, on the other hand, will not work anyway. You
use 'data' first as a type, and then as a member, so I really don't know
what you mean.
Where did you see "data" used as a type?
|
Here:
Class X {
--- data & functions
}
But now I see that kiran probably meant
// data and functions. :)
Regards
--
Thomas 'QsoRiX' Rydzynski Linux Registered User #178082
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Mon Nov 27, 2006 10:10 am Post subject: Re: private data access in constructors |
|
|
Lourens Veen ha scritto:
| Quote: |
struct X {
explicit X(int & data) : data(data) {}
// illegal, can't assign to a reference
X(const X & obj) {
data = obj.data;
}
|
Indeed this is illegal, but for a different reason, which is that a
reference member must be initialized with a ctor initializer (as you did
in the code below). The statement "data = obj.data;" is not illegal per
se, because you actually *can* assign to a reference, the effect being
to assign a value to the referenced object. What you can't do is to
"re-bind" a reference, but there's no syntax in C++ that allows that (in
fact it's illegal even if you hack it through casts).
| Quote: |
// legal, can initialise a reference
X(const X & obj) : data(obj.data) {}
int & data;
};
|
Ganesh
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Mon Nov 27, 2006 10:10 am Post subject: Re: private data access in constructors |
|
|
Jiang wrote:
| Quote: | Greg Herlihy wrote:
Thomas Rydzynski wrote:
kiran wrote:
[snip]
There are two ways to improve the constructor that I would suggest:
first, it is more efficient to initialize class members in the
constructor's initializer list than in its body. Therefore, moving
data's initialization out of the body would be my first suggestion.
So what if the passed in object is the object itself ?
|
Then the program would be attempting to copy an uninitialized object
and the program's behavior at that point becomes undefined - and
whether "data" is initialized in the body of the constructor or in an
initializer list makes not one bit of difference - the behavior is just
as undefined either way. So the bug would be in the object's
declaration - not in its constructor.
For example:
X x(x); // does this declaration look OK?
Now while an assignment operator for a class should expect
self-assignment (and handle it correctly), it is not reasonable for a
constructor to support "self-construction". A copy constructor requires
an initialized object from which to construct the copy. Clearly an
object under construction has not yet been initialized; so constructing
an object from itself (even though it successfully compiles) could only
be a programming error.
| Quote: | Second, arguments passed by reference should be declared const whenever
possible. Since X's copy constructor does not modify the X object being
copied, the parameter should be declared a const reference.
Yes, but the copy constructor does accept X&, together with volatile X&
and const volatile X&.
|
The point is that X(const X& ) is a better choice for a copy
constructor than X(X& x) whenever x is not modified from being copied.
Greg
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|