struct constructor

S

slurper

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) {}
};
 
C

Cy Edmunds

slurper said:
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.
 
R

Ron Natalie

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

Nick Hounsome

Cy Edmunds said:
slurper said:
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

 
D

David White

Cy Edmunds said:
slurper said:
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
 
M

Martijn Lievaart

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
 
R

Ron Natalie

Martijn Lievaart said:
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.
 
M

Martijn Lievaart

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top