Results 1 to 12 of 12

Thread: Redim in c++

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2002
    Location
    Bosnia
    Posts
    9

    Redim in c++

    Is it possible to redim field in c++ like Redim or Redim Preserve in VB?
    I have a class with CString field but I dont know how many members I need for that field. (That depends and it is not allways same)

  2. #2
    Swatty
    Guest
    I don't think you can do it in C++.

    In VB there it is available but I think in the background VB creates a new array , and copy's the values allready in it.

    I think you'd better provide the maximum space you could think is needed.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2002
    Location
    Bosnia
    Posts
    9
    Thanks Swatty,
    I need it because I want to "save" RAM resources maximum is possible, and program will create objects only if they is needed.
    I did it in VB, I think must be a some way to do that in C++

  4. #4
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Maybe you could try the vector class.. that will allocate more space when needed...
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Of course there is a way, but it's more than one command. Use a CStringArray that is native to MFC and you'll be fine.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Lively Member
    Join Date
    Mar 2002
    Posts
    68
    you can use a pointer to create a dynamic array

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If you don't understand pointers and memory management, don't use MFC.

    Easiest way is a vector, like Jop says:
    Code:
    #include <iostream>
    #include <vector>
    #include <iterator>
    
    using namespace std;
    
    int main() {
            cout << "Please enter some numbers, end with -1: " << flush;
    
            vector<int> nums;
            int num = 0;
    
            while(!cin.eof()) {
                    cin >> num;
    
                    if(num == -1 || cin.fail()) {
                            // stop when we get either -1 or something
                            // that wasn't recognised as a number
                            break;
                    }
    
                    nums.push_back(num);
            }
    
            cout << endl << "You entered:" << endl;
    
            copy(nums.begin(), nums.end(), ostream_iterator<int>(cout, "\n"));
    
            cout << endl;
    
            return 0;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    If I was already using MFC, I'd stay with their containers. The MFC containers are actually easy to use and don't have the confusing (for new people) iterators and such.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    They do use iterators, after a fashion...at least from what I remember (with POS_TYPE and all that, or something...).

    I just like the way that everything in the STL works so seamlessly, for example you can copy a vector to a list with one line
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No they don't. The only thing: a linked list uses POSITION, which is just a disgused node pointer. I admit it doesn't offer as much functionality as the STL, but still... it's native to MFC (and supports serialization BTW, STL doesn't do that)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by CornedBee
    (and supports serialization BTW, STL doesn't do that)
    True.

    Although I doubt it would be too difficult to make some kind of generic operator>>/<< for an iterator range
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I think it wouldn't be easy. MFC's serialization mechanism is quite complicated.
    Well, you could probably override a function in the process and catch a specific byte sequence. Or you could create a dummy class that is created by the << iterator operator and serialized which in turn creates a STL object and fills it with the data when unserialized.
    But it wouldn't be trivial.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width