473,394 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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) {}
};
Jul 22 '05 #1
8 5943

"slurper" <sl*********@hotmail.com> wrote in message news:3f***********************@news.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)


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

Jul 22 '05 #2
"slurper" <sl*********@hotmail.com> wrote in message
news:3f***********************@news.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.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #3

"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:c8******************@twister.nyroc.rr.com...
"slurper" <sl*********@hotmail.com> wrote in message
news:3f***********************@news.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

--
Cy
http://home.rochester.rr.com/cyhome/

Jul 22 '05 #4
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:c8******************@twister.nyroc.rr.com...
"slurper" <sl*********@hotmail.com> wrote in message
news:3f***********************@news.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

Jul 22 '05 #5
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

HTH,
M4
Jul 22 '05 #6

"Nick Hounsome" <nh***@blueyonder.co.uk> wrote in message news:Ai*****************@news-binary.blueyonder.co.uk...
- destructor
User declared constructors, destructors, or copy-assignment operators.
- references
- any member that isn't POD


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

Jul 22 '05 #7

"Martijn Lievaart" <m@remove.this.part.rtij.nl> wrote in message news:pa****************************@remove.this.pa rt.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.

Jul 22 '05 #8
On Tue, 30 Dec 2003 09:44:08 -0500, Ron Natalie wrote:

"Martijn Lievaart" <m@remove.this.part.rtij.nl> wrote in message
news:pa****************************@remove.this.pa rt.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
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Peter van der Goes | last post by:
When a struct is created in C# and a parameterized constructor defined, the IntelliSense editor shows only the syntax for the parameterized constructor in tooltips, but not for the default...
2
by: Grumble | last post by:
Hello all, I came across the following definition (or is it declaration? I never know): struct OFFSET_AND_SIZE { INT64 offset; UINT64 size; OFFSET_AND_SIZE (INT64 o, UINT64 s) : offset...
7
by: Vince | last post by:
I recently learned that it's possible to put a constructor inside a struct. My question is : Is it possible to do the following : typedef struct _TRecInfo { _TRecInfo(int nKey, int nMode):...
2
by: Carl Rosenberger | last post by:
Hi all, I would like to create an instance of a struct with reflection. The way I am used to doing this with classes: - get a ConstructorInfo from the Type - call Invoke() However, if a...
8
by: Pent | last post by:
Hi All, Why is this code valid? How can the static ctor be used? It doesn't act as class ctor at all. struct A { static A() { } }
3
by: Karl M | last post by:
Hi everyone, I just notice some strange behaviors on the MS C++ compiler regarding struct default constructor, see the example bellow: struct MyStruct { int a; }; class MyClass { public:
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
10
by: Mosfet | last post by:
Hi, I would like more info about deriving from an existing C struct. Let's say I am fed up with always writing the same code shown below : MYSTRUCT foo; memset( &foo, 0, sizeof(MYSTRUCT)...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.