Connecting Tech Pros Worldwide Forums | Help | Site Map

struct constructor

slurper
Guest
 
Posts: n/a
#1: Jul 22 '05
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) {}
};

Ron Natalie
Guest
 
Posts: n/a
#2: Jul 22 '05

re: struct constructor



"slurper" <slurper1234@hotmail.com> wrote in message news:3ff0c83f$0$20545$ba620e4c@news.skynet.be...[color=blue]
> 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)[/color]

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: ... } ;

Cy Edmunds
Guest
 
Posts: n/a
#3: Jul 22 '05

re: struct constructor


"slurper" <slurper1234@hotmail.com> wrote in message
news:3ff0c83f$0$20545$ba620e4c@news.skynet.be...[color=blue]
> 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[/color]
following[color=blue]
> 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)
>[/color]

[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/


Nick Hounsome
Guest
 
Posts: n/a
#4: Jul 22 '05

re: struct constructor



"Cy Edmunds" <cedmunds@spamless.rochester.rr.com> wrote in message
news:c84Ib.14968$q55.1324@twister.nyroc.rr.com...[color=blue]
> "slurper" <slurper1234@hotmail.com> wrote in message
> news:3ff0c83f$0$20545$ba620e4c@news.skynet.be...[color=green]
> > hi,
> >
> > i'm studying some stl. i saw the pair implementation in a header-file[/color][/color]
but[color=blue][color=green]
> > what i wonder is if a struct can have constructors as it seems in[/color]
> following[color=green]
> > 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)
> >[/color]
>
> [snip]
>
> In C++, the only difference between a struct and a class is that all[/color]
members[color=blue]
> of a struct are public by default. Thus a struct can have a constructor or
> anything else a class can have.
>[/color]

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

[color=blue]
> --
> Cy
> http://home.rochester.rr.com/cyhome/
>
>[/color]


David White
Guest
 
Posts: n/a
#5: Jul 22 '05

re: struct constructor


"Cy Edmunds" <cedmunds@spamless.rochester.rr.com> wrote in message
news:c84Ib.14968$q55.1324@twister.nyroc.rr.com...[color=blue]
> "slurper" <slurper1234@hotmail.com> wrote in message
> news:3ff0c83f$0$20545$ba620e4c@news.skynet.be...[color=green]
> > hi,
> >
> > i'm studying some stl. i saw the pair implementation in a header-file[/color][/color]
but[color=blue][color=green]
> > what i wonder is if a struct can have constructors as it seems in[/color]
> following[color=green]
> > 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)
> >[/color]
>
> [snip]
>
> In C++, the only difference between a struct and a class is that all[/color]
members[color=blue]
> of a struct are public by default.[/color]

And public inheritance by default (as against private for classes).

DW



Martijn Lievaart
Guest
 
Posts: n/a
#6: Jul 22 '05

re: struct constructor


On Mon, 29 Dec 2003 20:02:20 -0500, Ron Natalie wrote:
[color=blue]
> 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.[/color]

s/access/access and inheritance/g

HTH,
M4
Ron Natalie
Guest
 
Posts: n/a
#7: Jul 22 '05

re: struct constructor



"Nick Hounsome" <nh002@blueyonder.co.uk> wrote in message news:AibIb.5666$z43.2535@news-binary.blueyonder.co.uk...
[color=blue]
> - destructor[/color]

User declared constructors, destructors, or copy-assignment operators.
[color=blue]
> - references
> - any member that isn't POD[/color]

non-static members of reference or other non-pod type.

Ron Natalie
Guest
 
Posts: n/a
#8: Jul 22 '05

re: struct constructor



"Martijn Lievaart" <m@remove.this.part.rtij.nl> wrote in message news:pan.2003.12.30.10.40.06.338650@remove.this.pa rt.rtij.nl...[color=blue]
> On Mon, 29 Dec 2003 20:02:20 -0500, Ron Natalie wrote:
>[color=green]
> > 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.[/color]
>
> s/access/access and inheritance/g[/color]

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.

Martijn Lievaart
Guest
 
Posts: n/a
#9: Jul 22 '05

re: struct constructor


On Tue, 30 Dec 2003 09:44:08 -0500, Ron Natalie wrote:

[color=blue]
> "Martijn Lievaart" <m@remove.this.part.rtij.nl> wrote in message
> news:pan.2003.12.30.10.40.06.338650@remove.this.pa rt.rtij.nl...[color=green]
>> On Mon, 29 Dec 2003 20:02:20 -0500, Ron Natalie wrote:
>>[color=darkred]
>> > 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.[/color]
>>
>> s/access/access and inheritance/g[/color]
>
> 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.[/color]

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
Closed Thread