473,756 Members | 7,817 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_RESOLV E_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.seco nd) {}
};
Jul 22 '05 #1
8 5973

"slurper" <sl*********@ho tmail.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*********@ho tmail.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******@spaml ess.rochester.r r.com> wrote in message
news:c8******** **********@twis ter.nyroc.rr.co m...
"slurper" <sl*********@ho tmail.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******@spaml ess.rochester.r r.com> wrote in message
news:c8******** **********@twis ter.nyroc.rr.co m...
"slurper" <sl*********@ho tmail.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***@blueyond er.co.uk> wrote in message news:Ai******** *********@news-binary.blueyond er.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.th is.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.

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.th is.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
Jul 22 '05 #9

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

Similar topics

12
2119
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 constructor which appears to still exist. If this is not designed behavior, is it a bug or am I misusing my struct? If it is by design, could you please explain? I realize the behavior of a struct, as regards constructors, is different from that of...
2
5964
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 (o), size (s) {}
7
28840
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): nKey(nKey), nMode(nMode){}; //constructor int nKey; int nMode;
2
3569
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 struct does not have a constructor
8
2347
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
1919
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
16025
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 creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
10
2577
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) ); foo.cbSize = sizeof(MYSTRUCT);
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9873
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.