By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,864 Members | 1,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,864 IT Pros & Developers. It's quick & easy.

Inititialing list/vector inside a constructor

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.