473,770 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constructor(?) inside a struct

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
2 5965

"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
Ron Natalie wrote:
<snip>

Thank you, Ron.

Jul 22 '05 #3

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

Similar topics

4
2150
by: thecrow | last post by:
I am trying to pass a connected db link into a class constructor. In the calling module, the link opens successfully and passes the is_resource test. If I pass the same link into a class constructor, inside the constructor it fails the is_resource test. I am new to php. Why is the act of passing this value fundamentally altering it? This isn't happening with variables holding text or numeric values. Am I missing something here? ...
28
20342
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()', this.pinginterval); - but is there no way to do this without using the literal ObjectName? If I write 'this.methodName()' I get "Line 1 Char 1: Object doesn't support this property or method." in IE, and nothing happens in Firebird.
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;
3
4178
by: Marcus Kwok | last post by:
Is it in the standard that one cannot declare a member of a struct that uses a non-default constructor? Here is my simplified code; the Matrix class was taken from the FAQ, and works well otherwise (error checking was removed for simplicity). #include <iostream> #include <vector> const int NUM_SEASONS = 4;
2
3570
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
7
1887
by: gopal | last post by:
Can one constructor of a class call another constructor of the same class to initialize the this object? I read in the FAQ , to the above question the following ans was given Nope. Let's work an example. Suppose you want your constructor Foo::Foo(char) to call another constructor of the same class, say Foo::Foo(char,int), in order that Foo::Foo(char,int) would help initialize the this object.
8
1837
by: silversurfer2025 | last post by:
Hello everybody, I had this problem several times now and did not yet get the reasoning behind it. I have a class with a pointer as member variable, lets say a float array. Furthermore, I have two constructors of which the first is calling the second one... Here an easy example (so don't worry about the semantics ;) ) Member variables: float* pFeatVec;
7
2109
by: sachinc.biradar | last post by:
Hi, I am pretty new to C++, I have following doubt. Why can't we pass an argument to constructor as pointer? As per my knowledge referance is like const pointer. Any help will be highly appreciated. Regards, Sachin
2
2067
by: =?ISO-8859-1?Q?Konrad_M=FChler?= | last post by:
Hi, I've a struct: struct POINT { int x,y; }; And I want to commit a variable of type POINT in a function
0
10259
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10101
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...
0
9906
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6710
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
5354
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
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.