The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Dynamic Object Name
Discuss Dynamic Object Name in the C Programming forum on Dev Shed. Dynamic Object Name C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 9th, 2003, 10:48 AM
|
|
Contributing User
|
|
Join Date: Nov 2002
Posts: 236
Time spent in forums: 1 h 43 m 35 sec
Reputation Power: 11
|
|
|
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
|

March 9th, 2003, 10:56 AM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
the constructor should remain "standard":
Student();
to create the objects:
Code:
Student student[];
...
for (i=0;i<students_number;i++) {
student[i]=new Student();
}
|

March 9th, 2003, 11:02 AM
|
|
Contributing User
|
|
Join Date: Nov 2002
Posts: 236
Time spent in forums: 1 h 43 m 35 sec
Reputation Power: 11
|
|
thank you very much young sir, will report back when i balls it up
Cheers
Taz
|

March 9th, 2003, 11:07 AM
|
|
Contributing User
|
|
Join Date: Nov 2002
Posts: 236
Time spent in forums: 1 h 43 m 35 sec
Reputation Power: 11
|
|
one thing, I need to pass 2 arguments to my constructor.
So does...
PHP Code:
Student student[];
...
for (i=0;i<students_number;i++) {
student[i]=new Student();
}
become
PHP Code:
Student student[];
...
for (i=0;i<students_number;i++) {
student[i]=new Student([b]MY ARGUMENTS?[/b]);
}
|

March 9th, 2003, 11:16 AM
|
|
Contributing User
|
|
Join Date: Nov 2002
Posts: 236
Time spent in forums: 1 h 43 m 35 sec
Reputation Power: 11
|
|
right, on compilng the following code I get the following errors..
Quote:
int num_students = 2;
Students students[];
// Generate required number of monkey objects
int i;
for(i = 0; i < num_students; i++)
{
students[i]=new Students(5,3);
} |
Errors:
Quote:
stage3.cc: In function `int main()':
stage3.cc:24: array size missing in `students
stage3.cc:24: no matching function for call to `Students::Students()'
monkey.h:14: candidates are: Students::Students(const Students&)
students.h:25: Students::Students(int, int)
stage3.cc:30: no match for `Students& = Students*&' operator
students.h:14: candidates are: Students& Students::operator=(const Students&) |
Side note. The class works when calling normally, eg Students students(5,3);
|

March 9th, 2003, 01:34 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
Try declaring your array like this:
Code:
int num_students = 2;
Student *students = new Student[num_students](5,3);
...
... do your stuff
...
delete [] students;
|

March 9th, 2003, 01:36 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
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.
|

March 9th, 2003, 02:25 PM
|
|
Contributing User
|
|
Join Date: Nov 2002
Posts: 236
Time spent in forums: 1 h 43 m 35 sec
Reputation Power: 11
|
|
Hi guys, the following fixed it, required pointers. Note, student is now monkey
Regards
Quote:
// Create pointer array
int num_monkeys = 2;
Monkey *monkey[num_monkeys];
// Generate required number of monkey objects
for(int i = 0; i < num_monkeys; i++)
{
monkey[i] = new Monkey(5,3);
} |
|

March 9th, 2003, 03:05 PM
|
|
Contributing User
|
|
Join Date: Sep 2001
Location: ISRAEL
Posts: 35
Time spent in forums: < 1 sec
Reputation Power: 12
|
|
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:
class Student
{
public:
Student(int i, int j)
{
nClassID = i;
nStudentID = j;
}
private:
int nClassID;
int nStudentID;
}
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:
int nNumberOfStudents;
Student *pStudentsArray;
// get the number of students from the user and put it in nNumberOfStudents
// than dynamically allocate the array
pStudentsArray = new Student[nNumberOfStudents];
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:
typedef Student *PStudent;
// get number of students from the user
PStudent *ppStudentsArray = new PStudent[nNumberOfStudents];
for (int i=0; i<nNumberOfStudents; i++)
{
int nClsID, nStudentID;
//get values from the user to nClsID and nStudentID
ppStudentsArray[i] = new Student(nClsID, nStudentID);
}
2. Add Create(int, int) that does the same thing as the constructor. make the constructor do
NOTHING and do the following:
PHP Code:
// get number of students from the user
Student *pStudentsArray = new Student[nNumberOfStudents];
for (int i=0; i<nNumberOfStudents; i++)
{
int nClsID, nStudentID;
//get values from the user to nClsID and nStudentID
pStudentsArray[i].Create(nClsID, nStudentID);
}
__________________
"Gravitation can NOT be responsible for people falling in Love"
(one of the most significant characters in the history, can you guess?)
Gmorph.
|

March 9th, 2003, 03:06 PM
|
|
Contributing User
|
|
Join Date: Sep 2001
Location: ISRAEL
Posts: 35
Time spent in forums: < 1 sec
Reputation Power: 12
|
|
|
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.
|

March 9th, 2003, 03:09 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
>> 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  .
|

March 9th, 2003, 03:18 PM
|
|
Contributing User
|
|
Join Date: Sep 2001
Location: ISRAEL
Posts: 35
Time spent in forums: < 1 sec
Reputation Power: 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?!
|

March 9th, 2003, 03:27 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
>> 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  .
|

March 9th, 2003, 03:47 PM
|
|
Contributing User
|
|
Join Date: Sep 2001
Location: ISRAEL
Posts: 35
Time spent in forums: < 1 sec
Reputation Power: 12
|
|
Quote: Originally posted by Scorpions4ever
>> 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 . |
I have checked it on VC++ and you cannot get num_students from the user.
if you have succeeded please post the code - prove me wrong.
(the previous code you posted doesn't work)
|

March 9th, 2003, 04:44 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
Scorpions4ever is right, as he/she well knows, and has always been the case in my experience.  It's pretty trivial to prove, but here's an example anyway (using int as the type rather than Student) compiled and run in VC++6:
Code:
#include <iostream>
using namespace std;
int main()
{
int students = 0;
cout<<"Enter the number of students:\n";
cin>>students;
int* scores = new int[students];
for(int i=0; i<students; i++)
scores[i] = i;
for(int ii=0; ii<students; ii++)
cout<<scores[ii]<<endl;
delete [] scores;
return 0;
}
Normally, an array size must be determinable at compile time, so the memory can be set aside, however when you're allocating memory dynamically at run time, the size of an array doesn't need to be known until run time.
Last edited by 7stud : March 9th, 2003 at 05:01 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|