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<PTStruct> 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 13 4706
Jayw710 wrote: 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;
add a ctor here. Ctors are always useful, even in structs:
PTStruct(int initX, int initY) : x(initX), y(initY) { }
};
class abc { public: abc(); ...
std::vector<PTStruct> mypoints; }
void abc::abc() : mypoints(100)
change this to:
void abc::abc() : mypoints(100, PTStruct(0, 0))
Hope this helps,
-shez-
{
}
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
Jayw710 wrote: ... i have a struct
struct PTStruct { int x; int y; };
class abc { public: abc(); ...
std::vector<PTStruct> mypoints; }
void abc::abc() : mypoints(100)
'void'??? Constructors have no return type. Should be just
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? ...
You don't need to do anything. The code above already initializes all
field in all structs to zero. If your compiler doesn't do that, it's broken.
--
Best regards,
Andrey Tarasevich
Andrey Tarasevich wrote: Jayw710 wrote: ... i have a struct
struct PTStruct { int x; int y; };
class abc { public: abc(); ...
std::vector<PTStruct> mypoints; }
void abc::abc() : mypoints(100) 'void'??? Constructors have no return type. Should be just
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? ...
You don't need to do anything. The code above already initializes all field in all structs to zero. If your compiler doesn't do that, it's
broken.
Doesn't it depend where the vector gets its memory from?
Shezan Baig wrote: Andrey Tarasevich wrote: Jayw710 wrote: > ... > i have a struct > > struct PTStruct > { > int x; > int y; > }; > > class abc > { > public: > abc(); > ... > > std::vector<PTStruct> mypoints; > } > > void abc::abc() : mypoints(100)
'void'??? Constructors have no return type. Should be just
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? > ...
You don't need to do anything. The code above already initializes all field in all structs to zero. If your compiler doesn't do that, it's broken. Doesn't it depend where the vector gets its memory from?
No. Why?
Initializing this vector as 'mypoints(100)' is equivalent to
initializing it as 'mypoints(100, PTStruct(),
std::allocator<PTStruct>())' (default arguments get implicitly
filled-in), which means that all freshly created elements are
copy-initialized from 'PTStruct()'. And 'PTStruct()' is an instance of
'PTStruct' with both fields set to 0.
--
Best regards,
Andrey Tarasevich
> add a ctor here. Ctors are always useful, even in structs:
PTStruct(int initX, int initY) : x(initX), y(initY) { }
Thank you, this is what I was missing. I knew I had left something out (my
compiler VC++ doesn't initialize anything to zero in the structs).
Thanks again!
Jay
Jayw710 wrote: add a ctor here. Ctors are always useful, even in structs:
PTStruct(int initX, int initY) : x(initX), y(initY) { }
Thank you, this is what I was missing. I knew I had left something
out (my compiler VC++ doesn't initialize anything to zero in the structs).
Thanks again!
Jay
Also you might want to set default arguments, like this:
PTStruct(int initX = 0, int initY = 0) : x(initX), y(initY) { }
And you can simplify your containers constructor to how it was
originally:
abc::abc() : mypoints(100) { }
That way, even if you don't use the standard allocator, it will always
initialize to zero.
Hope this helps,
-shez-
Shez, what does the allocator have to do with the values used to
initialize concrete data members? Nothing... /david da********@warpmail.net wrote: Shez, what does the allocator have to do with the values used to initialize concrete data members? Nothing... /david
Yes, you are right. Sorry, for some reason, my mind was on dynamic
memory :)
-shez-
> 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.
regards,
me
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 "constructors". 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
"Andrey Tarasevich" <an**************@hotmail.com> schrieb im Newsbeitrag
news:11*************@news.supernews.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 "constructors". 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
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 "constructors". 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 "construction" is not applicable to non-class types.
Apparently, you are mixing the concept of "construction" with more
general concept of "initialization". These are not the same in C++.
--
Best regards,
Andrey Tarasevich
"Andrey Tarasevich" <an**************@hotmail.com> schrieb im Newsbeitrag
news:11*************@news.supernews.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: jut_bit_zx |
last post by:
class A
{
public:
A();
virtual ~A(){}
....
private:
int m_iarray;
}
|
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...
|
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;
};
|
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...
|
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...
|
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...
|
by: aaragon |
last post by:
Hello everyone,
Is this valid?
template <class A>
struct ClassA {
typedef A value_type;
value_type* data_;
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
| |