Easy question: Keep creating objects

This is a discussion on Easy question: Keep creating objects within the C++ Programming forums, part of the General Programming Boards category; I named the start of the topic with "Easy question" because this sould be answered by only 1 or 2 ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    43

    Easy question: Keep creating objects

    I named the start of the topic with "Easy question" because this sould be answered by only 1 or 2 members, so that you could concentrate on other more important problems.

    So, the user enters some numbers, which will be the elements of a vector from a class. I want that whenever the user finishes entering them (by pressing Enter), a new object to be created. So if I am entering:

    1 3 4 4 21 3
    1 2 -1 88 9 23
    42 9 8 4

    there should be 3 objects created with those numbers. How can I dynamically create objects and still be able to access them after?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,602
    It isn't very difficult
    Devoted my life to programming...

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,602
    You get input from console, and when six are counted, you allocate the object
    Devoted my life to programming...

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,602
    Quote Originally Posted by Veneficvs View Post
    How can I dynamically create objects and still be able to access them after?
    Not sure what you mean by that. You can access them just as you would access a static object ( almost the same way )
    Devoted my life to programming...

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    I suggest the use of std::getline() to read the line into a std::string, then you make use of a stringstream to extract the numbers from that line into the given member vector.

    You presumably want a vector of these objects.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,602
    Yeah, i didn't notice that not all of them are six!!
    I suppose the way laserlight suggests if much better...
    Devoted my life to programming...

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,602
    is much better...
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    I am afraid I am unable to put this in an understandable way.. I will give some examples here.
    When you declare a CBox object, for example, you write:
    CBox abox; or CBox *abox = new CBox;
    But as you see, I have declared names for them.

    When I enter 1 2 3 4 56 7, a CBox object has to be created. When I enter other series of numbers, another one has to be created. And so on. I cannot refer to those object..

    I could create a vector of CBox classes.. and whenever a new series of numbers is entered, to resize the vector so that it could contain as many objects as entered.. but if I declare it as static, then I could not make it have another dimension.. and if I make it dynamically and then readjust its dimension, would not I lose the data so far?

    I am making a little project by myself, just to practice my skills, but still, I am a memory-saving paranoic one. And I want to become more skilled in dynamically allocation as well.
    Last edited by Veneficvs; 04-30-2010 at 01:30 PM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Quote Originally Posted by Veneficvs
    I could create a vector of CBox classes.. and whenever a new series of numbers is entered, to resize the vector so that it could contain as many objects as entered.. but if I declare it as static, then I could not make it have another dimension.. and if I make it dynamically and then readjust its dimension, would not I lose the data so far?
    As you stated, each object as a vector. This does not conflict with the idea of having a vector of these objects:
    Code:
    #include <istream>
    #include <vector>
    #include <string>
    #include <sstream>
    
    class Box
    {
    public:
        // ...
    
        friend std::istream& operator>>(std::istream& in, Box& box);
    private:
        std::vector<int> numbers;
    };
    
    int main()
    {
        std::vector<Box> boxes;
        // ...
        return 0;
    }
    
    std::istream& operator>>(std::istream& in, Box& box)
    {
        // Read a line from in into a std::string.
        // Initialise a std::stringstream with that std::string.
        // Extract from the std::stringstream into box.numbers.
        // ...
        return in;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Thank you very much ! I will try it tonight and tell you if it worked.

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    I made the vector of boxes statically in the end. I am afraid I would like a complete representation of that >> overloading, please.

  12. #12
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    I named the start of the topic with "Easy question" because this sould be answered by only 1 or 2 members,
    I made the vector of boxes statically in the end. I am afraid I would like a complete representation of that >> overloading, please.
    I suggest you put forth some effort and show some code. Laserlight has been gracious enough to show you code, more than I would have given your original vague post, so do some research on your own and figure it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Creating a map of objects with variable width and height
    By MrSparky in forum C++ Programming
    Replies: 6
    Last Post: 07-30-2007, 03:06 PM
  3. Creating array of objects w/o default constructor
    By QuestionC in forum C++ Programming
    Replies: 19
    Last Post: 05-02-2007, 08:03 PM
  4. Creating Objects + Array
    By Cpro in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2007, 11:14 PM
  5. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21