473,320 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Inititialing list/vector inside a constructor

Hello Group,

I have a class

class Simple2 {
publica:
Simple2();
list<int> *l1;
list<int> l2;
};

I am writing the constructor

Simple2::Simple2 {

// It is easy to initialize l1

l1 = new list<int>;

}

How do I initialize l2 data member? The same thing I would like to ask
for vector class, if
it is an object and not a pointer how do you initialize template
classes.

Thanks for your help.

Nagrik

May 11 '06 #1
8 1893
arun wrote:
I have a class

class Simple2 {
publica:
Simple2();
list<int> *l1;
list<int> l2;
};

I am writing the constructor

Simple2::Simple2 {
Simple2::Simple2() {

// It is easy to initialize l1

l1 = new list<int>;
That's not initialisation. It's assignment. Initialisation would be
in the initialiser list. Yours is empty.

}

How do I initialize l2 data member?
Why do you think you need to? It's default-initialised for you.
The same thing I would like to
ask for vector class, if
it is an object and not a pointer how do you initialize template
classes.


Put them in the constructor initialiser list. What book on C++ are you
reading that doesn't explain this extremely basic feature of the language?

Simple2::Simple2() : l1(new list<int>), l2()
{
}

V
--
Please remove capital As from my address when replying by mail
May 11 '06 #2
arun wrote:
Hello Group,

I have a class

class Simple2 {
publica:
public:
Simple2();
list<int> *l1;
list<int> l2;
};

I am writing the constructor

Simple2::Simple2 {

// It is easy to initialize l1
Then do so. Below, you're not initializing. You are assigning.
l1 = new list<int>;

}

How do I initialize l2 data member?
With what? If the answer is "nothing", you're done, since it is
automatically defaul-initialized. That's the glory of classes.
The same thing I would like to ask for vector class, if
it is an object and not a pointer how do you initialize template
classes.


It has nothing to do with it being a template instance. You initialize it
like any other class.

May 11 '06 #3

Victor Bazarov wrote:
arun wrote:
The same thing I would like to
ask for vector class, if
it is an object and not a pointer how do you initialize template
classes.


Put them in the constructor initialiser list. What book on C++ are you
reading that doesn't explain this extremely basic feature of the language?

Simple2::Simple2() : l1(new list<int>), l2()
{
}


but of course you cannot initialise them with data in a way you would
expect thus:

class HasVector
{
vector <int > v;

public:

HasVector() : v( { 1, 2, 3, 4, 5 } ) // illegal
{
}
};

The workaround is to define the initialisation array in anonymous
namespace.

namespace {
const int vecInit[] = { 1, 2, 3, 4, 5 };
const size_t vecInitSize = sizeof( vecInit ) / sizeof( vecInit[0] );
}

HasVector::HasVector()
: v( &vecInit[0], &vecInit[vecInitSize] )
{
}

May 11 '06 #4
Earl Purple wrote:
Victor Bazarov wrote:
arun wrote:
The same thing I would like to
ask for vector class, if
it is an object and not a pointer how do you initialize template
classes.
Put them in the constructor initialiser list. What book on C++ are
you reading that doesn't explain this extremely basic feature of the
language?

Simple2::Simple2() : l1(new list<int>), l2()
{
}


but of course you cannot initialise them with data in a way you would
expect thus:

class HasVector
{
vector <int > v;

public:

HasVector() : v( { 1, 2, 3, 4, 5 } ) // illegal
{
}
};

The workaround is to define the initialisation array in anonymous
namespace.

namespace {
const int vecInit[] = { 1, 2, 3, 4, 5 };
const size_t vecInitSize = sizeof( vecInit ) / sizeof( vecInit[0] );
}

HasVector::HasVector()
: v( &vecInit[0], &vecInit[vecInitSize] )


I think it was just recently discussed that '&array[itssize]' has UB.
What's wrong with writing simply

v(vecInit, vecInit + vecInitSize)

?
{
}


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 11 '06 #5
Earl Purple wrote:
Victor Bazarov wrote:
arun wrote:
The same thing I would like to
ask for vector class, if
it is an object and not a pointer how do you initialize template
classes.


Put them in the constructor initialiser list. What book on C++ are you
reading that doesn't explain this extremely basic feature of the language?

Simple2::Simple2() : l1(new list<int>), l2()
{
}


but of course you cannot initialise them with data in a way you would
expect thus:

class HasVector
{
vector <int > v;

public:

HasVector() : v( { 1, 2, 3, 4, 5 } ) // illegal
{
}
};

The workaround is to define the initialisation array in anonymous
namespace.

namespace {
const int vecInit[] = { 1, 2, 3, 4, 5 };
const size_t vecInitSize = sizeof( vecInit ) / sizeof( vecInit[0] );
}

HasVector::HasVector()
: v( &vecInit[0], &vecInit[vecInitSize] )
{
}


Assuming you fix the undefined behavior, that's not *the* work-around
so much as *a* work-around. See this post for another option:

http://groups.google.com/group/comp....fe5982913d4414

Cheers! --M

May 11 '06 #6
Hello Group,

I asked this question because I read it, that if a class itself is a
data member in another class then it must
be instantiated explicitely. However, according to replies, which I
got on this post it appears that if
a class/template has a default constructor and it is a data member in
another class, then that default
constructor will be called regard less when you call the container
class constructor and you don't have
to do the following

B has a data member C then you don't have to do this explicitely

B() : C() //

and following will work fine.

B() {// code goes here }

If this is the case then why does not the compiler initializes
primitive data members like ints to 'zero'
values. When I don't explicitely initialize ints then their values
after the constructor is called is
definitely not int but some thing else.

Kindly explain this behaviour.

thanks.

arun

May 12 '06 #7
arun wrote:
[...] it appears that if
a class/template has a default constructor and it is a data member in
another class, then that default
constructor will be called regard less when you call the container
class constructor and you don't have
to do the following

B has a data member C then you don't have to do this explicitely

B() : C() //

and following will work fine.

B() {// code goes here }

If this is the case then why does not the compiler initializes
primitive data members like ints to 'zero'
values.
Because the language Standard says so.
When I don't explicitely initialize ints then their values
after the constructor is called is
definitely not int but some thing else.


Primitive data types are left uninitialised for optimization. If you
don't care for a member of a primitive type to be initialised to
anything, then the compiler is not the one to decide what to set it to.
Classes that do have constructors must have their constructors invoked.
That's just how class objects are created. Primitive types do not have
constructors and therefore don't need to be initialised to anything.
It's essentially the same behaviour that you'd get if you write

int i;

versus

someclass obj;

In the former case, 'i' is left uninitialised, whereas 'obj' is actually
_constructed_ by invoking the [default] constructor from 'someclass'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 12 '06 #8
arun wrote:
If this is the case then why does not the compiler initializes
primitive data members like ints to 'zero'
values. When I don't explicitely initialize ints then their values
after the constructor is called is
definitely not int but some thing else.


For compatibility with C structs, and not use different rules depending if
the int is a member of a struct compatible with C or not. You can read "The
design and evolution of C++", for example, for the history and reasoning of
that type of things.

--
Salu2

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
May 12 '06 #9

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

Similar topics

14
by: Dennis | last post by:
Hi I have a little problem as stated above (haven't found any solution on the www). I have a list of objects (particles) where I have a member of a template vector (array1d from TNT). I...
1
by: Michael Jasn | last post by:
I had a question about memory management. Please look at the two functions below. Can you answer the two questions in the comments below. Thanks so much. -Mike func1(std::vector<Point>&...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
12
by: ypjofficial | last post by:
Hello all, I have encountered with following strange problem. I am coding in C++ and using VC++ 6 compiler. I have a class strvector containing char * cstr as a private member and i have...
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
18
by: lchian | last post by:
Hi, I have a vector of class Foo. When I do a push_back(), I expect stl to call the default constructor I wrote for Foo. But instead, stl makes up its own default that is initialized with...
6
by: daveb | last post by:
I'm trying to write some code that calls the constructors of STL containers explicitly, and I can't get it to compile. A sample program is below. One compiler complains about the last two lines...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.