473,794 Members | 2,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

initialization of a struct in a vector

hi all,

this should be basic, but I can't seem to get the right syntax.

i have a struct

struct PTStruct
{
int x;
int y;
};

class abc
{
public:
abc();
...

std::vector<PTS truct> mypoints;
}

void abc::abc() : mypoints(100)
{

}
and i'm trying to get all the PTStruct variables (x and y) in mypoints to be
initialized to zero, but I'm screwing up the syntax somehow.

Any suggestions how this should be done?

Thanks to all!

Jay
Jul 23 '05
13 4743
Real Name wrote:
And 'PTStruct()' is an instance of
'PTStruct' with both fields set to 0.


Why? PTStruct only contains PODs - their default ctors wont we called
implicit, you have to call them explicit in your own ctor.
...


'PTStruct' is a POD type. Initializer '()' for a POD type causes default
initialization being applied to all its members. Default initialization
for type 'int' is zero initialization. That's all.

BTW, it has nothing to do with any "constructo rs". Default
initialization process for POD types does not involve constructors.
Moreover, objects of type 'int' have no constructors at all, default or
not.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #11

"Andrey Tarasevich" <an************ **@hotmail.com> schrieb im Newsbeitrag
news:11******** *****@news.supe rnews.com...
Real Name wrote:
And 'PTStruct()' is an instance of
'PTStruct' with both fields set to 0.
Why? PTStruct only contains PODs - their default ctors wont we called
implicit, you have to call them explicit in your own ctor.
...


'PTStruct' is a POD type.


Its a struct/class of PODs - it not a POD itself.
Initializer '()' for a POD type causes default
initialization being applied to all its members. Default initialization
for type 'int' is zero initialization. That's all.
Ack. But has nothing to do with PTStruct(): the members wont be initialized.
If you have any other information please point me to the relevant part of
the standard.
BTW, it has nothing to do with any "constructo rs". Default
initialization process for POD types does not involve constructors.
Call it as you want.
Moreover, objects of type 'int' have no constructors at all, default or
not.


So you think we are unable to construct an integer?

regards,
me
Jul 23 '05 #12
Real Name wrote:
>> And 'PTStruct()' is an instance of
>> 'PTStruct' with both fields set to 0.
>
> Why? PTStruct only contains PODs - their default ctors wont we called
> implicit, you have to call them explicit in your own ctor.
> ...
'PTStruct' is a POD type.


Its a struct/class of PODs - it not a POD itself.


Huh? It _is_ a POD itself. You probably need to look up a definition of
POD type. See 3.9/10 in the standard.
Initializer '()' for a POD type causes default
initialization being applied to all its members. Default initialization
for type 'int' is zero initialization. That's all.


Ack. But has nothing to do with PTStruct(): the members wont be initialized.


Not true. See below.
If you have any other information please point me to the relevant part of
the standard.


See 8.5/7:

--
8.5/7
An object whose initializer is an empty set of parentheses, i.e. (),
shall be default-initialized.
--

Then 8.5/5 to find out what "default-initialized" means in this case of
POD-type:

--
8.5/5
[...]
- otherwise, the storage for the object is zero-initialized
--
BTW, it has nothing to do with any "constructo rs". Default
initialization process for POD types does not involve constructors.


Call it as you want.


That's, BTW, is an important detail, not just a matter of "calling it as
someone wants".
Moreover, objects of type 'int' have no constructors at all, default or
not.


So you think we are unable to construct an integer?
...


"Construct" ? No. You are unable to construct an integer. Only objects of
class types can be constructed. In the standard C++ terminology the
notion of "constructi on" is not applicable to non-class types.
Apparently, you are mixing the concept of "constructi on" with more
general concept of "initialization ". These are not the same in C++.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #13

"Andrey Tarasevich" <an************ **@hotmail.com> schrieb im Newsbeitrag
news:11******** *****@news.supe rnews.com...
Huh? It _is_ a POD itself. You probably need to look up a definition of
POD type. See 3.9/10 in the standard.
Thx - that helped a lot.
Ack. But has nothing to do with PTStruct(): the members wont be initialized.
Not true. See below.


I have not seen any compiler doing so...
If you have any other information please point me to the relevant part of the standard.


See 8.5/7:


Thx again.

Jul 23 '05 #14

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

Similar topics

4
1890
by: Russell Silva | last post by:
I have a class A with a member variable type vector<foo> which is initialized upon construction (no standard () constructor): class A { protected: vector<foo> foo_vector; public: A(const vector<foo>& init_foo_vector); }
3
5939
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
1
1373
by: Alex Vinokur | last post by:
Class Bar in a program below contains two static members: v1 and v2 of class Foo. v1 is initialized before creating an instance of Bar, v2 v1 is initialized after that instance. No Foo-constuctor for v2 was called before line "Bar b". However, (temporary ?) instance for v2 was created in Bar::Bar(). How was it created: without calling constructor?
15
3380
by: jamx | last post by:
How can you initialize an array, in the initialization list of a constructor ?? SomeClass { public: SomeClass() : *init here* { } private: int some_array; };
3
3207
by: Spoon | last post by:
Hello everyone, I want to create an array of objects at run-time. AFAIU, operator new will call the default constructor for each object in the array. In other words, the following program will print INSIDE DEFAULT CTOR five times. #include <vector> #include <cstdio>
4
3714
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and value-initialization. I think default-init calls default constructor for class objects and sets garbage values to PODs. Value-init also calls default constructor for class objects and sets 0s to POD types. This is what I've learned from the books (especially...
4
2299
by: pallav | last post by:
I have the following structs below. static PinDelayPtr unspecifiedPipoDelayModel(new PinDelay()); #define DELAY_VALUE_NOT_GIVEN -7654321.0 typedef boost::shared_ptr<struct PinDelayPinDelayPtr struct DelayTime {
2
3493
by: aaragon | last post by:
Hello everyone, Is this valid? template <class A> struct ClassA { typedef A value_type; value_type* data_;
5
3185
by: zr | last post by:
Hi, Is there a way to initialize a std::tr1::array with a pre-allocated built-in array in a copy-less assignment, such that both will point to the same memory? Vice-versa is easy to do, simply use std::t1::array::data() and assign the returned value to the c-style pointer; if STL does not support this, is there any other library that has such a container (maybe BOOST)?
0
9518
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
10433
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
10212
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
10000
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
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
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
6777
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
5436
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...
1
4112
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

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.