Re: List container passed as a reference
From: James Dennett (jdennett_at_acm.org)
Date: 07/27/04
- Next message: Alwyn: "Re: Need another pair of eyes to figure this one out"
- Previous message: SaltPeter: "Re: List container passed as a reference"
- In reply to: SaltPeter: "Re: List container passed as a reference"
- Next in thread: David White: "Re: List container passed as a reference"
- Reply: David White: "Re: List container passed as a reference"
- Reply: SaltPeter: "Re: List container passed as a reference"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 26 Jul 2004 22:35:45 -0700
SaltPeter wrote:
> "James Dennett" <jdennett@acm.org> wrote in message
> news:s31Nc.222$mg6.171@fed1read02...
>
>>SaltPeter wrote:
>>
>>>"Alwyn" <dt015a1979@mac.com.invalid> wrote in message
>>>news:250720040838569316%dt015a1979@mac.com.invalid...
>>>
>>>
>>>>In article <yEwMc.19430$Fj.936881@news20.bellglobal.com>, SaltPeter
>>>><SaltPeter@Jupiter.sys> wrote:
>>>>
>>>>
>>>>
>>>>>Thats without mentioning the fact that a constructor is called during a
>>>>>pass_by_value and the variable within the function is completely
>>>
>>>unrelated
>>>
>>>
>>>>>with the passed entity(other than common values). Example:
>>>>
>>>>That's not fair, my Point did not have a constructor!
>>>
>>>
>>>Point can't exist without a constructor, whether compiler generated or
>
> not.
>
>>>Thats true for any abstract type. The exact same applies to the
>
> destructor.
>
>>>Your suggestion is that an object can be generated without cstors. This
>
> is
>
>>>not possible in C++. An instance implies a constructor in C++. The same
>
> goes
>
>>>with copy cstors, if you don't supply one, the compiler must supply a
>>>default constructor.
>>
>>struct Point { int x, y; };
>>
>>int main() {
>> Point p = {1, 2};
>>}
>>
>>Which constructor do you think is being used here? I see
>>an instance p of class Point, but the only constructors
>>Point has are a default and a copy constructor, and neither
>>of those is used when p is initialized as above.
>>
>>-- James
>
>
> Thats a misguided statement.
I think not.
> Point p;
> is itself a call to a constructor,
Well, it causes an object of type Point to be created,
which uses a default constructor, but close enough.
> Point p = {1, 2};
> is a cstor with an assignment operator that initializes publicly accessible
> members x and y.
No, it is not. It is initialization, not assignment. No
assignment is involved. To illustrate that, for example,
make the members const:
struct Point { int const x, y; };
int main() { Point p = {1, 2}; }
All quite legal, though assignment to a Point is not
permitted.
> Which happen to be accessible directly since its a struct
> not a class.
Point is a class; the fact that it was defined using the
struct keyword is irrelevant.
struct Point { int x, y; };
is exactly equivalent, with a conforming compiler, to
class Point { public: int x, y; };
> If Point was a class, this initialization of the Point variable
> would fail (private members).
>
> To prove that a constructor indeed needs to be called, try either of the 2
> versions below:
>
> class Point { int x, y; };
>
> int main()
> {
> Point p = {1, 2}; // error, default cstor can't initialize privates.
> }
That proves no such thing -- you can't prove a falsehood
in a consistent logical system. All this shows is that
there are limits on which classes can be initialized as
aggregates.
(Incidentally, the conventional abbreviation for "constructor"
is "ctor", and similarly "destructor" is abbreviated to "dtor";
using the accepted abbreviations will make your posts easier
for others to read.)
> Note that 2 constructors are called in next example.
I'll note no such thing! I believe it's true that the copy
constructor should indeed be used for pass by value here,
though if modifyPoint were passed a temporary then a
compiler could legally elide the copy, so the "copy cstor"
comment is somewhat misplaced, and would be more accurate
at the call site. (But then again modifyPoint would be
better known as "doNothing" :-)
> Thats why the value in
> main doesn't get modified.
>
> #include <iostream>
>
> struct Point
> {
> int x;
> int y;
> };
>
> void modifyPoint(Point Pt) // copy cstor
> {
> Pt.x += 10;
> Pt.y += 10;
> }
>
> int main()
> {
> Point p = {1,2}; // cstor + assignment operator
No; this is initialization, but it does not use constructors
or assignment operators. (Quick test: if you think it uses
an assignment operator, from which expression (of which type)
does it assign?
>
> modifyPoint(p);
The copy constructor will be used to pass p to the
function.
-- James
- Next message: Alwyn: "Re: Need another pair of eyes to figure this one out"
- Previous message: SaltPeter: "Re: List container passed as a reference"
- In reply to: SaltPeter: "Re: List container passed as a reference"
- Next in thread: David White: "Re: List container passed as a reference"
- Reply: David White: "Re: List container passed as a reference"
- Reply: SaltPeter: "Re: List container passed as a reference"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|