Re: I need some help/pointers/comments to get me started, I am stuck

From: David White (no_at_email.provided)
Date: 12/09/03


Date: Tue, 9 Dec 2003 11:52:36 +1100


"Tommy Lang" <mums_se@yahoo.se> wrote in message
news:78dde37d.0312081544.527c9e64@posting.google.com...
> I am working on this project and I need some help/pointers/comments to
> get me started, I am stuck.
> The program will be used to store information in an array while it is
> running.
> I need to store objects of my classes Person(superclass),
> Student(inherit Person), Teacher(inherit Person) in that array.
> The name will be the unique key. These classes are all working ok. I
> want to be able to add, remove, find etc. objects.
>
> To all of this, I have created a menue class to print out a menue on
> the screen for the user where he can select what he wants to do(add,
> remove...) and a class "List" to tie it all together.
> In the menue class I have a reference(List &refList;) to the List
> class and in the listclass I have a pointer to array that will hold
> all the objcts(List *pList[10];).

So you have an array of 10 pointers to lists. It's not clear to me from your
description what these are for.

> I init the reference in the constructor of Menu class like
> menue::menue(const List &L):refList(L){}
>
> Questions:
> When I initiate the object List should I make sure to set all items in
> array to NULL in the constructor ?
> for(int i=0;i<10;i++)
> pList[i]=NULL;

For an array of pointers, you should initialize each element with either a
null pointer or a valid pointer to an object. But I still don't know why you
have 10 lists.

> Can I have only one array ex. List *pList[10]; that can hold all types
> of objects(Person, Student, Teacher)?

If you want an array that can hold all these types, then it needs to be an
array of Person*. For example:
Person *persons[10];
You can store a pointer to an object of class Person or any class derived
from Person in any element of this array. You cannot have an array that can
store actual objects of different classes.

> Do I need typecasting to identify objects before adding removing..?

You would only need to downcast from Person * to a derived class if you need
to access a member that is uniqe to the derived class. In general,
programmers try to avoid this, but sometimes it's necessary. Ideally, if you
have an array of Person * you would like to use only members that are in
class Person. These members are often virtual functions that are overridden
in the derived class, which results in behaviour suitable for each derived
class.

> What methodes would I put in List class? (insert, remove, find, print)

I don't know what your List is for.

> When I am searching for an object in array, should I return the object
> itself or a pointer to the object?

It will already be a pointer, so you can just return that, or you can return
a reference. You can't return an actual object because that would require
the type of object to be known at compile time, but that can't be since the
array has pointers to objects of different types.

> In case of returning a pointer, how do I return a pointer to the
> object I am searching for?

For a built-in array:
return persons[index];
where 'index' is the index of the element you want to return.

> Thankful for any help

If you want to locate a person based on a name (you said that the name is
the key), then a std::map<std::string, Person*> would be an appropriate type
to use, if you are allowed to. This type would find a person from a name for
you. It also will do insertions and deletions for you. Not as good, but
still better than a built-in array, is a std::list<Person*>. This will do
efficient insertions and deletions, but you'll have to do the search
yourself.

DW



Relevant Pages

  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: why cannot assign to function call
    ... hypothetical C-like languages, ... sizeof business would still indicate that a pointer was being passed. ... talk about variables of an array type. ... the earlier version of the standard didn't have numbered ...
    (comp.lang.python)
  • Re: multi dimensional arrays as one dimension array
    ... please - where does the standard say that such a conversion ... Pointer conversion yields a pointer to the same object as ... exist only where there are array declarations. ...
    (comp.lang.c)
  • Re: Pointer Equality for Different Array Objects
    ... Pointers to the same object are pointers to the same region of data storage. ... A pointer comaprison (for exact equality, ... we have the vague ("if the array is large enough"). ...
    (comp.lang.c)
  • Re: Evaluating unary *
    ... 'arr' exists, ... value can be used with the same syntax as would be used to access a 2D array of the kind you're referring to, but that 2D array is just a different way of looking as the same object that was already created by the definition of 'arr'. ... to me, it makes sense to return a pointer to the first value of an array, but to return the address of the pointer to the first value of an array, is not directly possible as such. ... lea eax, ...
    (comp.std.c)