PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > making a struct array NULL?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
making a struct array NULL?

Réponse
 
LinkBack Outils de la discussion
Vieux 16/10/2007, 08h22   #1
djm
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut making a struct array NULL?

hi,
lets say ive this struct array in my header file

public:
IndepHTable();
private:
struct
{
string word;
List defList;
} dictionary[TABLE_SIZE];

how can i make the elements in the array null from the constructor?

will this work?

IndepHTable::IndepHTable()
{

word.clear();
defList=NULL;

}
or something like
this?

IndepHTable::IndepHTable(dictionary l)
{
for(int i=0;i<29;i++)
{
l[i].word.clear();
l[i].defList=NULL;
}
}

i tried but none of the work. can someone me out?
thanks

  Réponse avec citation
Vieux 16/10/2007, 08h40   #2
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: making a struct array NULL?

"djm" <djmlog103@gmail.com> wrote in message
news:1192519330.761691.111890@k35g2000prh.googlegr oups.com...
> hi,
> lets say ive this struct array in my header file
>
> public:
> IndepHTable();
> private:
> struct
> {
> string word;
> List defList;
> } dictionary[TABLE_SIZE];
>
> how can i make the elements in the array null from the constructor?
>
> will this work?
>
> IndepHTable::IndepHTable()
> {
>
> word.clear();
> defList=NULL;
>
> }
> or something like
> this?
>
> IndepHTable::IndepHTable(dictionary l)
> {
> for(int i=0;i<29;i++)
> {
> l[i].word.clear();
> l[i].defList=NULL;
> }
> }
>
> i tried but none of the work. can someone me out?
> thanks


if your string is a std::string then it will already be "cleared". I don't
know what your List is, what does it's default constructor do?

You are saying defList = NULL, but that presumes that List is a pointer, is
it? Probably not, it's probably a class.

Most likely, you won't have to do anything, although, again, it depends on
what List is.



  Réponse avec citation
Vieux 16/10/2007, 08h47   #3
djm
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: making a struct array NULL?

On Oct 16, 12:40 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> "djm" <djmlog...@gmail.com> wrote in message
>
> news:1192519330.761691.111890@k35g2000prh.googlegr oups.com...
>
>
>
> > hi,
> > lets say ive this struct array in my header file

>
> > public:
> > IndepHTable();
> > private:
> > struct
> > {
> > string word;
> > List defList;
> > } dictionary[TABLE_SIZE];

>
> > how can i make the elements in the array null from the constructor?

>
> > will this work?

>
> > IndepHTable::IndepHTable()
> > {

>
> > word.clear();
> > defList=NULL;

>
> > }
> > or something like
> > this?

>
> > IndepHTable::IndepHTable(dictionary l)
> > {
> > for(int i=0;i<29;i++)
> > {
> > l[i].word.clear();
> > l[i].defList=NULL;
> > }
> > }

>
> > i tried but none of the work. can someone me out?
> > thanks

>
> if your string is a std::string then it will already be "cleared". I don't
> know what your List is, what does it's default constructor do?
>
> You are saying defList = NULL, but that presumes that List is a pointer, is
> it? Probably not, it's probably a class.
>
> Most likely, you won't have to do anything, although, again, it depends on
> what List is.


sorry for the lack of info.
the pruporse is to make a linked list with rehasing techniques. the
list is a class and that has nodes, the values of the objects in the
nodes are made in to initial values by thier constructors, as you said
i realise that i dont need to make the defList to NULL cuz its already
made null. but what about the string? what if i want to make it null
so that it wont have any garbage values when i declare them?

  Réponse avec citation
Vieux 16/10/2007, 08h57   #4
djm
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: making a struct array NULL?

On Oct 16, 12:40 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> "djm" <djmlog...@gmail.com> wrote in message
>
> news:1192519330.761691.111890@k35g2000prh.googlegr oups.com...
>
>
>
> > hi,
> > lets say ive this struct array in my header file

>
> > public:
> > IndepHTable();
> > private:
> > struct
> > {
> > string word;
> > List defList;
> > } dictionary[TABLE_SIZE];

>
> > how can i make the elements in the array null from the constructor?

>
> > will this work?

>
> > IndepHTable::IndepHTable()
> > {

>
> > word.clear();
> > defList=NULL;

>
> > }
> > or something like
> > this?

>
> > IndepHTable::IndepHTable(dictionary l)
> > {
> > for(int i=0;i<29;i++)
> > {
> > l[i].word.clear();
> > l[i].defList=NULL;
> > }
> > }

>
> > i tried but none of the work. can someone me out?
> > thanks

>
> if your string is a std::string then it will already be "cleared". I don't
> know what your List is, what does it's default constructor do?
>
> You are saying defList = NULL, but that presumes that List is a pointer, is
> it? Probably not, it's probably a class.
>
> Most likely, you won't have to do anything, although, again, it depends on
> what List is.


hmmm then do i actually need a constructor here?
cuz
1. the string wil have no initial value?
2. the constructor in List class also makes the contents of "deflist"
to NULL

  Réponse avec citation
Vieux 16/10/2007, 09h00   #5
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: making a struct array NULL?

"djm" <djmlog103@gmail.com> wrote in message
news:1192520873.134114.54960@i38g2000prf.googlegro ups.com...
> On Oct 16, 12:40 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
>> "djm" <djmlog...@gmail.com> wrote in message
>>
>> news:1192519330.761691.111890@k35g2000prh.googlegr oups.com...
>>
>>
>>
>> > hi,
>> > lets say ive this struct array in my header file

>>
>> > public:
>> > IndepHTable();
>> > private:
>> > struct
>> > {
>> > string word;
>> > List defList;
>> > } dictionary[TABLE_SIZE];

>>
>> > how can i make the elements in the array null from the constructor?

>>
>> > will this work?

>>
>> > IndepHTable::IndepHTable()
>> > {

>>
>> > word.clear();
>> > defList=NULL;

>>
>> > }
>> > or something like
>> > this?

>>
>> > IndepHTable::IndepHTable(dictionary l)
>> > {
>> > for(int i=0;i<29;i++)
>> > {
>> > l[i].word.clear();
>> > l[i].defList=NULL;
>> > }
>> > }

>>
>> > i tried but none of the work. can someone me out?
>> > thanks

>>
>> if your string is a std::string then it will already be "cleared". I
>> don't
>> know what your List is, what does it's default constructor do?
>>
>> You are saying defList = NULL, but that presumes that List is a pointer,
>> is
>> it? Probably not, it's probably a class.
>>
>> Most likely, you won't have to do anything, although, again, it depends
>> on
>> what List is.

>
> sorry for the lack of info.
> the pruporse is to make a linked list with rehasing techniques. the
> list is a class and that has nodes, the values of the objects in the
> nodes are made in to initial values by thier constructors, as you said
> i realise that i dont need to make the defList to NULL cuz its already
> made null. but what about the string? what if i want to make it null
> so that it wont have any garbage values when i declare them?


A std::string already gets initialized to "" by it's default consturctor.
So you don't have to do anything. If you really really really want to
though you could do:

IndepHTable::IndepHTable(): world("")
{
}

but "" is what the default constructor uses if you don't pass it anything
(which you don't by not calling a constructor specifically.

In main line this is the difference between:

std::string word;
std::string word("");

They both make word initialize to "". I.E. no net change with one over the
other.


  Réponse avec citation
Vieux 16/10/2007, 09h11   #6
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: making a struct array NULL?

"djm" <djmlog103@gmail.com> wrote in message
news:1192521446.417753.109310@e9g2000prf.googlegro ups.com...
> On Oct 16, 12:40 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
>> "djm" <djmlog...@gmail.com> wrote in message
>>
>> news:1192519330.761691.111890@k35g2000prh.googlegr oups.com...
>>
>>
>>
>> > hi,
>> > lets say ive this struct array in my header file

>>
>> > public:
>> > IndepHTable();
>> > private:
>> > struct
>> > {
>> > string word;
>> > List defList;
>> > } dictionary[TABLE_SIZE];

>>
>> > how can i make the elements in the array null from the constructor?

>>
>> > will this work?

>>
>> > IndepHTable::IndepHTable()
>> > {

>>
>> > word.clear();
>> > defList=NULL;

>>
>> > }
>> > or something like
>> > this?

>>
>> > IndepHTable::IndepHTable(dictionary l)
>> > {
>> > for(int i=0;i<29;i++)
>> > {
>> > l[i].word.clear();
>> > l[i].defList=NULL;
>> > }
>> > }

>>
>> > i tried but none of the work. can someone me out?
>> > thanks

>>
>> if your string is a std::string then it will already be "cleared". I
>> don't
>> know what your List is, what does it's default constructor do?
>>
>> You are saying defList = NULL, but that presumes that List is a pointer,
>> is
>> it? Probably not, it's probably a class.
>>
>> Most likely, you won't have to do anything, although, again, it depends
>> on
>> what List is.

>
> hmmm then do i actually need a constructor here?
> cuz
> 1. the string wil have no initial value?
> 2. the constructor in List class also makes the contents of "deflist"
> to NULL


No. You do not need a custom constructor here. The default constructor is
fine for your purposes. The default constructor for a class will call all
the default constructors for contained class instances.

You would need a custom constructor if you need to do more than that. I.E.
Call a non default constructor (for instance, initializing word to "None"
instead of "") or if a variable doesn't have a default constructor (floats,
ints, chars, etc...the built in types).

Whenever you add a variable to a class think, "Will the default constructor
be fine for my purposes?" If not, add it to the custom constructor
(creating it if it doesn't exist).

Some people may initialize an instance in the custom constructor to the same
as the default constructed just to be explicit, to say, "Yeah, I REALLY want
this string to be "" ". But is not required.


  Réponse avec citation
Vieux 16/10/2007, 09h27   #7
djm
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: making a struct array NULL?

On Oct 16, 1:11 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> "djm" <djmlog...@gmail.com> wrote in message
>
> news:1192521446.417753.109310@e9g2000prf.googlegro ups.com...
>
>
>
> > On Oct 16, 12:40 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> >> "djm" <djmlog...@gmail.com> wrote in message

>
> >>news:1192519330.761691.111890@k35g2000prh.google groups.com...

>
> >> > hi,
> >> > lets say ive this struct array in my header file

>
> >> > public:
> >> > IndepHTable();
> >> > private:
> >> > struct
> >> > {
> >> > string word;
> >> > List defList;
> >> > } dictionary[TABLE_SIZE];

>
> >> > how can i make the elements in the array null from the constructor?

>
> >> > will this work?

>
> >> > IndepHTable::IndepHTable()
> >> > {

>
> >> > word.clear();
> >> > defList=NULL;

>
> >> > }
> >> > or something like
> >> > this?

>
> >> > IndepHTable::IndepHTable(dictionary l)
> >> > {
> >> > for(int i=0;i<29;i++)
> >> > {
> >> > l[i].word.clear();
> >> > l[i].defList=NULL;
> >> > }
> >> > }

>
> >> > i tried but none of the work. can someone me out?
> >> > thanks

>
> >> if your string is a std::string then it will already be "cleared". I
> >> don't
> >> know what your List is, what does it's default constructor do?

>
> >> You are saying defList = NULL, but that presumes that List is a pointer,
> >> is
> >> it? Probably not, it's probably a class.

>
> >> Most likely, you won't have to do anything, although, again, it depends
> >> on
> >> what List is.

>
> > hmmm then do i actually need a constructor here?
> > cuz
> > 1. the string wil have no initial value?
> > 2. the constructor in List class also makes the contents of "deflist"
> > to NULL

>
> No. You do not need a custom constructor here. The default constructor is
> fine for your purposes. The default constructor for a class will call all
> the default constructors for contained class instances.
>
> You would need a custom constructor if you need to do more than that. I.E.
> Call a non default constructor (for instance, initializing word to "None"
> instead of "") or if a variable doesn't have a default constructor (floats,
> ints, chars, etc...the built in types).
>
> Whenever you add a variable to a class think, "Will the default constructor
> be fine for my purposes?" If not, add it to the custom constructor
> (creating it if it doesn't exist).
>
> Some people may initialize an instance in the custom constructor to the same
> as the default constructed just to be explicit, to say, "Yeah, I REALLY want
> this string to be "" ". But is not required.


thanks a lot for your . i got a pretty good knowledge about when
to use a constructor. and in my case the default one does the the job.
thanks!

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h56.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18054 seconds with 15 queries