-
March 19th, 2009, 09:07 AM
#1
returning variables from blocks
I am trying to define
if (i = 1)
{
std::vector<myclass> a(memory, constructorformyclass(details))
}
else
{
std::vector<myotherclass> b(memory,constructorformyotherclass(details))
}
then use whichever i create afterwards using another if statement i.e
b(or a)[memory].showinfo()
however if i define them within the block code they are not passed out. Is there anyway to get around this
cheers
-
March 19th, 2009, 09:18 AM
#2
Re: returning variables from blocks
What is the bigger picture of the problem that you are trying to solve? With the given info, most of what I can say is: construct them before the if statement. However, the fact that showinfo() is called on the elements of the vectors in either case suggests that say, polymorphism can be used here.
-
March 19th, 2009, 09:18 AM
#3
Re: returning variables from blocks
Are myclass and myotherclass related? Then you can use a vector of pointers to the base type, and of course, declared only once.
-
March 19th, 2009, 09:25 AM
#4
Re: returning variables from blocks
Another polymorphic option if you don't want to deal with lots and lots of pointers:
1) Derive both from a base class
2) Declare both vectors as they are outside of the ifs
3) Also declare a base class pointer and size integer along with them
4) In the ifs, resize the appropriate vector using the appropriate constructor as the default object
5) Then (still in the ifs) point the base class pointer at &a[0] or &b[0] and set the size value appropriately.
-
March 19th, 2009, 09:32 AM
#5
Re: returning variables from blocks
i have a student class, then i have a physicsstudent class and a computersciencestudentclass. The later two inherit details from student class.
Each student has a unique id i.e. 1C,2C for computer science or 1P,2P for a physics student
i have a two functions that returns the number and the letter separately. The numbers is the memory location and the letter refers to whether i create a computersciencestudent or physics student.
instead of usually using
physicsstudet a(details), physicsstudent b(details) i wanted to use physicsstudent a[memory](details) so that the id had something to do with how to create the object of each class
i have to define it within the if statement because to check wether the student is physics or computerscience
-
March 19th, 2009, 09:37 AM
#6
Re: returning variables from blocks
Forget what I said, then---that's a straight-up polymorphism problem, so go nuts with dynamic allocation.
-
March 19th, 2009, 10:41 AM
#7
Re: returning variables from blocks
Code:
if (i = 1)
{
std::vector<myclass> a(memory, constructorformyclass(details))
}
else
{
std::vector<myotherclass> b(memory,constructorformyotherclass(details))
}
I guess this should be:
Otherwise you will just set i to 1 and then always call the first case
-
March 19th, 2009, 01:58 PM
#8
Re: returning variables from blocks
i have a student class, then i have a physicsstudent class and a computersciencestudentclass. The later two inherit details from student class.
The you can do something like this:
Code:
std::vector<student*> students;
students->push_bach(new physicsstudent(...));
students->push_bach(new computersciencestudent(...));
students->push_bach(new physicsstudent(...));
students->push_bach(new computersciencestudent(...));
students[index]->CallSomeMethod();
// make sure at the end you delete all the students
for(size_t i = 0; i < students.size(); ++i)
delete students[i];
students.clear(); // not necessary if this clean up code goes into a destructor
Basically you need to leverage the polymorphism.
-
March 19th, 2009, 02:03 PM
#9
Re: returning variables from blocks
Instead of std::vector<student*>, I suggest std::vector<std::tr1::shared_ptr<student> > or boost::ptr_vector<student>. If you do want to use a std::vector<student*>, then be careful about manual memory management.
-
March 19th, 2009, 02:54 PM
#10
Re: returning variables from blocks
std::tr1::shared_ptr is a good idea, but works only if he uses Visual Studio 2008 SP1. As for boost might be too much for a homework assignment.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|