|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Dynamic Object Name
Hi all, looking for a little help with dynamic object names.
Eg, i have a class for students. When the system is run, the user will be asked how many students they require, the program can then create X instances of the student class, student1, student2... studentX etc. Im just wondering how todo this. Is Student student[i](); correct for a constructor? If not, how do I do it ? Cheers Taz |
|
#2
|
|||
|
|||
|
the constructor should remain "standard":
Student(); to create the objects: Code:
Student student[];
...
for (i=0;i<students_number;i++) {
student[i]=new Student();
}
__________________
-- Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more. |
|
#3
|
|||
|
|||
|
thank you very much young sir, will report back when i balls it up
![]() Cheers Taz |
|
#4
|
|||
|
|||
|
one thing, I need to pass 2 arguments to my constructor.
So does... PHP Code:
become PHP Code:
|
|
#5
|
||||
|
||||
|
right, on compilng the following code I get the following errors..
Quote:
Errors: Quote:
Side note. The class works when calling normally, eg Students students(5,3); |
|
#6
|
||||
|
||||
|
Try declaring your array like this:
Code:
int num_students = 2; Student *students = new Student[num_students](5,3); ... ... do your stuff ... delete [] students; |
|
#7
|
|||
|
|||
|
From your error message it looks like you don't have a constructor defined that takes two arguments, but that is the way you would call the constructor. A constructor is like any other function except it doesn't and can't have a return type, and when you call any function, the parameters must match up with a function definition.
student[i]=new Student(MY ARGUMENTS?); --------------- Student student[];******what size array did you create here? for (i=0;i<students_number;i++) { student[i]=new Student(); } Last edited by 7stud : March 9th, 2003 at 02:06 PM. |
|
#8
|
|||
|
|||
|
Hi guys, the following fixed it, required pointers. Note, student is now monkey
![]() Regards Quote:
|
|
#9
|
|||
|
|||
|
7stud was right. some clarifications.
What you are trying to do, as you probably know, is to dynamically allocate an array of Student. You have to use pointers since you don't know the size of the array. before that you have some problems with the constructor. let's take the following class: PHP Code:
let's say you want to retrieve the number of the students, as well as the IDs, from the user and than construct these instances. This is the wrong way: PHP Code:
why? because when you try to allocare the "new Student[..]" the program tries to construct these instances. you have to possibilities as far as I know: 1. Use pointer to pointer (I will explain) 2. Add a function "Create(int i, int j)" and empty the constructor. Explaination: 1. Instead of allocating an array of students we allocate an array of pointers to Student PHP Code:
2. Add Create(int, int) that does the same thing as the constructor. make the constructor do NOTHING and do the following: PHP Code:
__________________
"Gravitation can NOT be responsible for people falling in Love" (one of the most significant characters in the history, can you guess?) Gmorph. |
|
#10
|
|||
|
|||
|
Your solution is good only if the number of monkeys is constant - but if you want to get this and the other values from the user it wont work i suspect.
|
|
#11
|
||||
|
||||
|
>> 2. Add Create(int, int) that does the same thing as the constructor. make the constructor do nothing
Or, you could use my example posted above and use the constructor just fine. BTW I tested my example with g++ before posting it here . |
|
#12
|
|||
|
|||
|
It would work - but i'm not sure it will satisfy all the requirements:
1. The number of Elements is CONSTANT and not dynamic 2. What if you want to construct the each element with different values?! |
|
#13
|
||||
|
||||
|
>> 1. The number of Elements is CONSTANT and not dynamic
Nope, you can get num_students from user input. >> 2. What if you want to construct the each element with different values?! Then you'd have to have a constructor with no arguments and set the values inside a for loop, like your example did . |
|
#14
|
|||
|