| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Total newbquestion
I have an array of objects of a simple class that stores variables. Ints and single charachters can be stored fine using a[4].letter = 'y'; Where a is the name of the object. I can store multiple charachters e.g. a name into a single object using a("name"), to pass the name as an argument to the constructor, which stores it in a string.
The problem is that i have no idea how to pass an argument such as name to one of an array of objects. Do i need to provide a default name to the constructor to initialize the array? Also on a more general note, what is the best approach to store a few variables that i want to use a letter to retrive. Should i store the letter as part of the object in an array of objects, or should i create an object for each letter and set of variables? If so how do you dynamically supply the object name used to create it? Alternatively should i use a multidimensional array? Help and advice much apreciated. |
|
#2
|
||||
|
||||
|
What do your class and object specifications specifically look like?
As far as multi-dimensional arrays, that depends on style. It will probably be a smiggin faster, but that will be undecernable. The main question is style, and how easy the different methods will be for you to implement and, if necessary, adjust in the future.
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
|
#3
|
|||
|
|||
|
const int MAXONE = 3;
//declare MAXTHREE const int MAXTHREE = 5; //prototype class to read values in amnioacids.txt //the amino acids class...may put it in an include file later class AminoAcids { public: AminoAcids(char* pLetter, char* pName) { cout << "constructing aminoacid\n" ; strncpy(letter, pLetter, MAXONE); letter[MAXONE - 1] = '\0'; strncpy(name, pName, MAXTHREE); name[MAXTHREE - 1] = '\0'; hp = 0; } //more public stuff, these are not protected char letter[MAXONE]; char name[MAXTHREE]; char hp; }; //to set the values in main AminoAcids y("v","val"); all the above works beautifully but as soon as you try to turn y into an array of y objects it breaks. How do i do the above when there are several y objects? y[5] for example? |
|
#4
|
|||
|
|||
|
When using class objects in arrays you need an default constructor which will be called for each object in the array.
E.g.: Code:
class CDemo
{
public:
CDemo(char cValue) // preferred constructor
{
strcpy(m_cVar, cValue); // must be strncpy, blabla
}
CDemo() // 'default' constructor
{
memset(m_cVar, 0, sizeof(m_cVar)); // Clear the var
}
char m_cVar[80]; // Just for demo
}
For one object you can do: CDemo y("test"); But for an array the declaration is: CDemo a[10]; And you see, no option to pass the constructor value. This is why the default constructor is used. But you can access the functions/vars easilly strcpy(a[1].m_cVar, "test"); What you often see is the use of pointers to objects in arrays: CDemo * p_a[10]; p_a[1] = new CDemo("test"); (You need to be carefull with uninitialized pointers though, but hey that's C )In your (and mine) example there is no real need for a class. You should only use classes when there are specialized functions (te be) written for the data containing the class. But that is the OOP story ... |
|
#5
|
|||
|
|||
|
Well its not so bad as i first thought. Turns out you can pass arguments to the constructor, but only when you initialize the array. This (i think) defeats the point. If your interesested the working code is below. It seems you need to access it using i as it thinks your trying to create another array. I am now moving on to creating the array with the default constructor as above and specifying the arguments with a function (my first method?).
Thanks for your reply MichaelSoft ![]() AminoAcids array[2]= { AminoAcids("v","val"), AminoAcids("t","ter") }; //display the data for (int i = 0; i < 2; i++) { cout <<array[i].name <<" " <<array[i].hp <<" " <<array[i].letter <<endl; } Code for the class is unchanged. |
|
#6
|
|||
|
|||
|
You're welcome
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Total newbquestion |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|