returning variables from blocks
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: returning variables from blocks

  1. #1
    Join Date
    Feb 2009
    Posts
    26

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,273

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    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.

  5. #5
    Join Date
    Feb 2009
    Posts
    26

    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

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: returning variables from blocks

    Forget what I said, then---that's a straight-up polymorphism problem, so go nuts with dynamic allocation.

  7. #7
    Join Date
    Jan 2009
    Posts
    596

    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:

    Code:
    if (i == 1)
    ...etc
    Otherwise you will just set i to 1 and then always call the first case

  8. #8
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,273

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center