By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 802 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Constructor(?) inside a struct

P: n/a
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) {}
};

(INT64 are UINT64 are not relevant to the discussion.)

I know some C, but very little C++ so I was somewhat puzzled. Is the
function inside the struct a constructor which will assign o to
offset and s to size?

So, I would write struct OFFSET_AND_SIZE toto(666, 999); and toto's
fields would be initialized as I expect, right?

What if I write struct OFFSET_AND_SIZE toto; would the compiler
complain, or would the fields remain undefined, as in C?

Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Grumble" <in*****@kma.eu.org> wrote in message news:br**********@news-rocq.inria.fr...
Hello all,

I came across the following definition (or is it declaration? I
never know):
It's a definition.

I know some C, but very little C++ so I was somewhat puzzled. Is the
function inside the struct a constructor
It is a constructor. structs are the classes in C++, they can have constructors.
The only difference is the access control is public by default in struct.
which will assign o to offset and s to size?
More correctly, it will initialize o and s with those values.

So, I would write struct OFFSET_AND_SIZE toto(666, 999); and toto's
fields would be initialized as I expect, right?
Yes.
What if I write struct OFFSET_AND_SIZE toto; would the compiler
complain, or would the fields remain undefined, as in C?


It will complain. Once you declare ANY constructor, the implicit
default constructor is gone. Your class has no default constructor
so you can't initalize
Jul 22 '05 #2

P: n/a
Ron Natalie wrote:
<snip>

Thank you, Ron.

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.