By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

create a objects vector class

P: 21
I am newbie to c++ and I would like to build a vector class that returns a vector of objects. I have done some thing like this:

student class that contains some of student's properties such as name, address, phone etc.

I also have a registered students class which I like to have it as a vector of student objects. The way I have done it so far is like this.
Expand|Select|Wrap|Line Numbers
  1. RegisteredStduents class{
  2.    RegisteredStudents(no_of_students);
  3.   vector<studnet> get_students(by_age){
  4.    vector<student>  results;
  5.    student stud_obj=new student();
  6.    for(query students based on age{
  7.         stud_obj->set_name(blahblah);
  8.         stud_obj->set_address(blahblah);
  9.         results->push_back(stud_obj);
  10.     }
  11.     delete stud_obj;
  12.     return results;   
  13.    }
  14.    void set_no_students(no_of_students){
  15.        return no_of_students;
  16.    }
  17. }
This class works fine but I thought there might be a better way to do this. Instead of having a function "get_students" that returns a vector of objects, I can have the whole class to be a vector and in my get_students I can push_back the students objects back to the class vector. I am not really positive on how to do that.

So I will be grateful if someone tell me how to do that or send me a link to an example on the web. I have been searching all over the place but no luck so far. Also it will be helpful if you can show me in how to call this class from a driver (i.e ).
Jun 11 '07 #1
Share this Question
Share on Google+
5 Replies


DeMan
100+
P: 1,806
Not quite sure what you mean.....
Or how push_back is implemented.....

Incedently, you have a void function (set_no_students) which aooears to return an int......
Jun 11 '07 #2

P: 21
Not quite sure what you mean.....
Or how push_back is implemented.....

Incedently, you have a void function (set_no_students) which aooears to return an int......
Sorry for not being clearer in explaining what I want to do but here is what I have. I am showing two functions in this class to illustrate my class :set_no_students and get_all_students. The set_no_students is a separate function within that class which is not important in here. I should not have included it in here but please ignore it for now. However, the get_all_students is my concerned function that returns a vector of objects students. In that function I create an object and then I push it into the vector and then I return that vector back whenever called. I thought there might be a better approach than this.

In fact, this class works fine. I have tested it using c++ driver and everything is great. The issue us when I try to wrap java around this class using SWIG. I am having hard time accessing the vector objects from java once I call this function. So I thought this may work if I implement this class differently. So instead of having this function returns a vector it should push these objects to the main class itself. And this is where I want help or different approach. Does this make any sense to you?
Jun 12 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
I think there's a wrong approach here.

Instead of a RegisteredStudents class, there should be a vector of Students:
Expand|Select|Wrap|Line Numbers
  1. vector<Student> RegisteredStudents;
  2.  
You can use the remove_copy_if() algorithm to copy all Students to another vector<Students> based on a specified age.
Jun 12 '07 #4

P: 21
So how do you recommend I go about building that class?
Jun 12 '07 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
So how do you recommend I go about building that class?
You don't have to build a class. You just create a vector<Student> and then use vector methods or maybe write your own functions taking vector<Student::iterator arguments.

In this approach, the students are registered because they are in the vector<Student> object and not because they are objects of a RegisteredStudent class.
Jun 14 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.