 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
slurper Guest
|
Posted: Tue Dec 30, 2003 12:34 am Post subject: struct constructor |
|
|
hi,
i'm studying some stl. i saw the pair implementation in a header-file but
what i wonder is if a struct can have constructors as it seems in following
snippet from the stl library. the snippets i wonder about are indicated
with --------> : i thought a struct doesn't have constructors (but here
there are four, actually three)
template <class _T1, class _T2>
struct pair {
typedef _T1 first_type;
typedef _T2 second_type;
_T1 first;
_T2 second;
#ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
---------> pair() : first(), second() {}
#else
---------> pair() : first(_T1()), second(_T2()) {}
#endif
---------> pair(const _T1& __a, const _T2& __b) : first(__a), second(__b)
{}
template <class _U1, class _U2>
--------> pair(const pair<_U1, _U2>& __p) : first(__p.first),
second(__p.second) {}
};
|
|
| Back to top |
|
 |
Cy Edmunds Guest
|
Posted: Tue Dec 30, 2003 1:02 am Post subject: Re: struct constructor |
|
|
"slurper" <slurper1234 (AT) hotmail (DOT) com> wrote
| Quote: | hi,
i'm studying some stl. i saw the pair implementation in a header-file but
what i wonder is if a struct can have constructors as it seems in
following
snippet from the stl library. the snippets i wonder about are indicated
with --------> : i thought a struct doesn't have constructors (but here
there are four, actually three)
|
[snip]
In C++, the only difference between a struct and a class is that all members
of a struct are public by default. Thus a struct can have a constructor or
anything else a class can have.
--
Cy
http://home.rochester.rr.com/cyhome/
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Tue Dec 30, 2003 1:02 am Post subject: Re: struct constructor |
|
|
"slurper" <slurper1234 (AT) hotmail (DOT) com> wrote
| Quote: | hi,
i'm studying some stl. i saw the pair implementation in a header-file but
what i wonder is if a struct can have constructors as it seems in following
snippet from the stl library. the snippets i wonder about are indicated
with --------> : i thought a struct doesn't have constructors (but here
there are four, actually three)
|
You thought wrong. Structs are classes. The only difference is that classes
defined with the struct keyword have public access by default. Classes defined
with the class keyword have private access by default.
That is:
struct x { .... };
is the same as:
class x { public: ... } ;
|
|
| Back to top |
|
 |
Nick Hounsome Guest
|
Posted: Tue Dec 30, 2003 9:11 am Post subject: Re: struct constructor |
|
|
"Cy Edmunds" <cedmunds (AT) spamless (DOT) rochester.rr.com> wrote
| Quote: | "slurper" <slurper1234 (AT) hotmail (DOT) com> wrote in message
news:3ff0c83f$0$20545$ba620e4c (AT) news (DOT) skynet.be...
hi,
i'm studying some stl. i saw the pair implementation in a header-file
but
what i wonder is if a struct can have constructors as it seems in
following
snippet from the stl library. the snippets i wonder about are indicated
with --------> : i thought a struct doesn't have constructors (but here
there are four, actually three)
[snip]
In C++, the only difference between a struct and a class is that all
members
of a struct are public by default. Thus a struct can have a constructor or
anything else a class can have.
|
I suspect that the poster was thinking that struct = POD (plain old (C
compatible) data).
Of course this isn't true but what may need pointing out is that even POD
can have some C++ stuff such as ctors and member functions.
Off the top of my head the only things it can't have (and still be POD) are:
- access specifiers
- virtual methods
- destructor
- references
- any member that isn't POD
There may also be restrictions on ctors
|
|
| Back to top |
|
 |
David White Guest
|
Posted: Tue Dec 30, 2003 9:53 am Post subject: Re: struct constructor |
|
|
"Cy Edmunds" <cedmunds (AT) spamless (DOT) rochester.rr.com> wrote
| Quote: | "slurper" <slurper1234 (AT) hotmail (DOT) com> wrote in message
news:3ff0c83f$0$20545$ba620e4c (AT) news (DOT) skynet.be...
hi,
i'm studying some stl. i saw the pair implementation in a header-file
but
what i wonder is if a struct can have constructors as it seems in
following
snippet from the stl library. the snippets i wonder about are indicated
with --------> : i thought a struct doesn't have constructors (but here
there are four, actually three)
[snip]
In C++, the only difference between a struct and a class is that all
members
of a struct are public by default.
|
And public inheritance by default (as against private for classes).
DW
|
|
| Back to top |
|
 |
Martijn Lievaart Guest
|
Posted: Tue Dec 30, 2003 10:40 am Post subject: Re: struct constructor |
|
|
On Mon, 29 Dec 2003 20:02:20 -0500, Ron Natalie wrote:
| Quote: | You thought wrong. Structs are classes. The only difference is that
classes defined with the struct keyword have public access by default.
Classes defined with the class keyword have private access by default.
|
s/access/access and inheritance/g
HTH,
M4
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Tue Dec 30, 2003 2:40 pm Post subject: Re: struct constructor |
|
|
"Nick Hounsome" <nh002 (AT) blueyonder (DOT) co.uk> wrote
User declared constructors, destructors, or copy-assignment operators.
| Quote: | - references
- any member that isn't POD
|
non-static members of reference or other non-pod type.
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Tue Dec 30, 2003 2:44 pm Post subject: Re: struct constructor |
|
|
"Martijn Lievaart" <m (AT) remove (DOT) this.part.rtij.nl> wrote
| Quote: | On Mon, 29 Dec 2003 20:02:20 -0500, Ron Natalie wrote:
You thought wrong. Structs are classes. The only difference is that
classes defined with the struct keyword have public access by default.
Classes defined with the class keyword have private access by default.
s/access/access and inheritance/g
|
Sorry, my statement is right as it stands. I didn't say anything that limits
the access control to members. Access refers to both members and
base classes.
Inheritance only exists in the concept of name lookup.
|
|
| Back to top |
|
 |
Martijn Lievaart Guest
|
Posted: Tue Dec 30, 2003 3:24 pm Post subject: Re: struct constructor |
|
|
On Tue, 30 Dec 2003 09:44:08 -0500, Ron Natalie wrote:
| Quote: | "Martijn Lievaart" <m (AT) remove (DOT) this.part.rtij.nl> wrote in message
news:pan.2003.12.30.10.40.06.338650 (AT) remove (DOT) this.part.rtij.nl...
On Mon, 29 Dec 2003 20:02:20 -0500, Ron Natalie wrote:
You thought wrong. Structs are classes. The only difference is
that classes defined with the struct keyword have public access by
default. Classes defined with the class keyword have private access
by default.
s/access/access and inheritance/g
Sorry, my statement is right as it stands. I didn't say anything that
limits the access control to members. Access refers to both members
and base classes.
Inheritance only exists in the concept of name lookup.
|
Yes, now you mention it, but I think that explicitly stating it applies to
inheritance as well makes it clearer. I don't think everyone will
immediately come your (correct) interpretation. I know I didn't. :-)
M4
|
|
| 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
|
|