How to make multiple "objects" (classes question) easy.

This is a discussion on How to make multiple "objects" (classes question) easy. within the C++ Programming forums, part of the General Programming Boards category; for example Code: for(t = 0; t<100; t++){ sands[t] = new sand(10,t) } I was wondering how you can use ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    76

    How to make multiple "objects" (classes question) easy.

    for example
    Code:
    for(t = 0; t<100; t++){
    sands[t] = new sand(10,t)
    }
    I was wondering how you can use the same function like that. The above wont work. And yes it is defined:
    Code:
    class Dot
    {
        private:
        int x, y;
        int xVel, yVel;
        public:
        Dot(int x,int y);
        void fall();
        void move();
        void show();
        void setxy(int xx,int yy);
    };
    
    Then those functions are further defined
    I might have to explain my problem better...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    One way is to define a function object:
    Code:
    class MakeDot
    {
    public:
        MakeDot(int x) : x(x), y(0) {}
    
        Dot operator()()
        {
            return Dot(x, y++);
        }
    private:
        int x;
        int y;
    };
    Then you use the function object in conjunction with std::generate_n(), e.g.,
    Code:
    std::vector<Dot> sands;
    std::generate_n(std::back_inserter(sands), 100, MakeDot(10));
    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

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    76
    In my first example...
    Where would I define the "sands" thing?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Quote Originally Posted by azjherben
    Where would I define the "sands" thing?
    Um, you would define it before you use it.

    On a hunch, are you a Java programmer? I have a feeling that you are unnecessarily and incorrectly using new due to such a background.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy String position question...
    By Striph in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 08:48 PM
  2. Beginner Classes Question
    By kbro3 in forum C++ Programming
    Replies: 9
    Last Post: 08-14-2008, 07:43 AM
  3. Curiosity question about classes...
    By Raigne in forum C++ Programming
    Replies: 5
    Last Post: 03-10-2008, 12:34 PM
  4. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  5. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM

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