Re: List container passed as a reference
From: SaltPeter (SaltPeter_at_Jupiter.sys)
Date: 07/27/04
- Previous message: David White: "Re: List container passed as a reference"
- In reply to: James Dennett: "Re: List container passed as a reference"
- Next in thread: James Dennett: "Re: List container passed as a reference"
- Reply: James Dennett: "Re: List container passed as a reference"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 27 Jul 2004 07:17:51 -0400
"James Dennett" <jdennett@acm.org> wrote in message
news:HQlNc.2891$mg6.1310@fed1read02...
> 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.
yes, my bad, it invokes a constructor, whether that be a default cstor,
alternate cstor or copy cstor is irrelevent. Its still a cstor.
>
> > 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}; }
You can't initialize without
a) a default cstor which creates an instance with default values (Point p
could be anything)
b) an asiignment operator which then initializes p's contents
Yes, thats initialization. Which would be impossible without a cstor.
>
> 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.
No, you stated that Point p is neither a class nor a struct, since in your
opinion a cstor needs not be invoked to generate an instance (initialized or
not). Point is an abstract type, whether class or struct is irrelevent.
Thats not the issue here.
>
> struct Point { int x, y; };
>
> is exactly equivalent, with a conforming compiler, to
>
> class Point { public: int x, y; };
equivalent also in the fact that a cstor must be called, as i said,
regardless of which cstor actually gets called.
>
> > 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.
agregates can't initialize a non-existant object.
>
> (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" :-)
This proves the fact that 2 entities exist, although the function's Point is
nothing more than a temporary. If you were to define a d~stor in Point,
leaving the compiler to generate the cstors, you'ld still see 2 d~stors
called.
Explain this:
#include <iostream>
struct Point
{
int x;
int y;
~Point() { std::cout << "d~stor invoked"; }
};
int main()
{
Point p = {1,2};
return 0;
}
So, now explain to me why the d~stor is invoked when the above compiles?
Since you're convinced that initialization doesn't require a cstor. Frankly,
that baffles me.
>
> > 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?
the very same used to write:
int x = 5;
or int x[] = {1,2};
Initialization it is. You can't initialize a non-existant entity. Hence, you
can't initialize anything without a cstor.
>
> >
> > modifyPoint(p);
>
> The copy constructor will be used to pass p to the
> function.
>
> -- James
- Previous message: David White: "Re: List container passed as a reference"
- In reply to: James Dennett: "Re: List container passed as a reference"
- Next in thread: James Dennett: "Re: List container passed as a reference"
- Reply: James Dennett: "Re: List container passed as a reference"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|