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 13 4742
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<PTS truct> 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<PTS truct> 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<PTS truct> 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<PTS truct> 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********@warp mail.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 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 vector<foo>& init_foo_vector);
}
|
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 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?
|
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 print INSIDE
DEFAULT CTOR five times.
#include <vector>
#include <cstdio>
| |
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...
|
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
{
|
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 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)?
|
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...
|
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...
| |
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |