Thread: Classes question...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Classes question...

    Ok, I am sorry for this question because the answer is most likely stupidly simply. ok well I did something similar to this
    Code:
    #include <iostream>
    
    using namespace std;
    
    class data_arc {
       data_arc()
       {}
       virtual void display() const = 0;
    };
    
    class data_main : public data_arc {
       data_main()
       {}
       virtual void display() const
       { //blablabla
       }
       //other stuff
    };
    
    
    int main() 
    {
       data_arc* data = new data_main;
       data->display();
       delete data;
       data_arc* data = new data_main;
       data->somethingelse();
       delete data;
    }
    basicly my question is how do I have my program automatically create instances of a class. because I need to be able to have alot of instances of classes (game), for different elements. Such as enemies, story elements, and other events. Thank you very much for any assistance

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    ok well I did something similar to this
    Your code might be different without you realizing it . . . .

    You could store the data in a file and create a vector (or whatever) of the objects.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    ... good point, would I still place the vector inside of a class or make a global vector?

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Whatever you want, although I see no need for a global variable. You'd probably want a static variable, though, depending on how your class is designed.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Ok, thank you very much, altough I am wandering how efficinet is the vector class?

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Almost certainly more efficient than the alternatives. And certainly safer and easier to use.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Write a class as a wrapper to the vector and use the class as a container class. All other classes which need access to the objects store a shared pointer to the container class. This way you still gain the functionality of a global vector and you get the benefit of controlled access to/from the vector. The container is the boss and nobody gets to his data unless they do it his way. You can also write simple serialization into the container class so that you can load/save objects to disk in a snap.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I understand the first part of that, but what do you mean by simple serialization? I didnt think the serialization was simple in any way, but if you could elaborate just a little then I will take your idea into full perspective, and be able to use it more efficiently. Thanks again for any help

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In order to take advantage of polymorphism, you will want to store pointers in your container. This is a little tricky because the memory must be managed somehow. Some of the better ways include the use of ptr_vector or shared_ptr, both available from boost. Otherwise you should probably make your wrapper class own the pointers and delete them when they are removed from the vector or before the vector is destroyed.

    BTW, don't forget to make the destructor virtual in data_arc. The code above gives undefined behavior.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    does boost require any specific OS, or compiler enviroment?
    Wow, this is starting to be complicated quickly. Thank you yet again for any assistance.

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Otherwise you should probably make your wrapper class own the pointers and delete them when they are removed from the vector or before the vector is destroyed.
    And this can be done in the destructor of the wrapper class. If you don't do this you will leak objects everywhere.

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I know this is probably asking to much, but can someone give me an example of a destructor that will delete the pointers/objects thanks

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    class anint {
    private:
        int *i;
    public:
        anint(int i) { this->i = new int; this->i = i; }
        int get() { return *i; }
        ~anint() { delete i; }
    };
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    lol, im stupid ( i figured it was more complicated ) thank you everyone for the help

  15. #15
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Remember the big three, though.

    dwks did not include a copy constructor or assignment operator, which are vital. anint as he wrote it would not be usable in an STL container. It is not copyable (as soon as one copy is destroyed, all other copies will have invalid pointers).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with winAPI in classes (newbie question)
    By Spyril in forum Windows Programming
    Replies: 3
    Last Post: 11-09-2007, 03:21 PM
  2. Replies: 2
    Last Post: 07-28-2006, 02:59 PM
  3. Simple Question about Classes
    By Loctan in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2006, 02:40 AM
  4. Classes and Win32 API, and another Question
    By philvaira in forum Windows Programming
    Replies: 10
    Last Post: 04-10-2004, 07:21 PM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 10:17 PM