CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2007
    Posts
    33

    Predefining Object Names Before Initialization

    Hello all.

    First of all, I just want to thank everybody who helps noobs like me on this site. I know we can be a pain sometimes.

    Anyways, my problem is that I'm trying to create a program that, as far as I can see, is going to need to be able to create an indeterminate number of classes. I'm not sure how to accomplish this. It seems to me that I would need some way of systematically naming the objects, but I'm not sure how I would name an object indirectly - by means of a string or char array or something. From what I've read online, it seems that it's impossible to do this.

    My second thought is that I could initialize an array of objects, but I'd rather not have to initialize all of the objects at once for memory reasons; I'd like to be able to initialize the objects individually, as needed. Also, I'd have no way of recycling the members of the object array via deconstruction/reconstruction since, as far as I know, an object array would have to be deconstructed as a whole. I suppose I could always just recycle the objects without deconstructing the array, but I just don't want to believe that there's not a way to do this without needing to keep all of the available objects open all the time.

    So, I guess what I'd like to know is whether or not I'm completely wrong about the functionality of object arrays and, if I'm not, is there a way for me to do what I'm trying to do?

    Thank you very much for taking the time to read about my problem.
    Andy

  2. #2
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: Predefining Object Names Before Initialization

    Quote Originally Posted by hixidom View Post

    Anyways, my problem is that I'm trying to create a program that, as far as I can see, is going to need to be able to create an indeterminate number of classes.
    As long as you are not going to write a code generator that does not make any sense. A class is an abstraction for (complex) data types.
    I guess you want to have indeterminate count of instances of a class (or maybe some classes).

    I'd recommend:
    Learn what is a class, what is an object.
    Best way to do this is to buy or borrow a good contemporary c++ book.
    For holding an (at the beginning) indeterminate count of objects you should use a vector. That's what a vector is for. You can add objects (instances of classes) to a vector, and you can delete them from the vector later.

    Regards
    PA

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Predefining Object Names Before Initialization

    Quote Originally Posted by hixidom View Post
    Hello all.

    First of all, I just want to thank everybody who helps noobs like me on this site. I know we can be a pain sometimes.

    Anyways, my problem is that I'm trying to create a program that, as far as I can see, is going to need to be able to create an indeterminate number of classes. I'm not sure how to accomplish this. It seems to me that I would need some way of systematically naming the objects, but I'm not sure how I would name an object indirectly - by means of a string or char array or something. From what I've read online, it seems that it's impossible to do this.

    My second thought is that I could initialize an array of objects, but I'd rather not have to initialize all of the objects at once for memory reasons; I'd like to be able to initialize the objects individually, as needed. Also, I'd have no way of recycling the members of the object array via deconstruction/reconstruction since, as far as I know, an object array would have to be deconstructed as a whole. I suppose I could always just recycle the objects without deconstructing the array, but I just don't want to believe that there's not a way to do this without needing to keep all of the available objects open all the time.

    So, I guess what I'd like to know is whether or not I'm completely wrong about the functionality of object arrays and, if I'm not, is there a way for me to do what I'm trying to do?

    Thank you very much for taking the time to read about my problem.
    Andy
    First of all, you need to distinguish the difference between a "class", and a "class instance" or "object". a "class" is a definition, a description of objects. These cannot just be "created on the fly".

    Then, there are objects, which are just instances of the class:

    Code:
    class some_class {} ;
    
    some_class instance_1;
    some_class instance_2;
    Here, there is 1 class, and 2 instances of that class.

    ----

    Your notion of arrays is completely correct. Although C++ usually uses std::vector as a way to automatically manage the size of the arrays: It can start out small, and then grow as needed. You can also predefine the capacity of a vector, and then add objects into that vector, without having to ever re-locate.

    Another alternative would be to use an std::list.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: Predefining Object Names Before Initialization

    Quote Originally Posted by hixidom View Post
    Anyways, my problem is that I'm trying to create a program that, as far as I can see, is going to need to be able to create an indeterminate number of classes.
    Maybe you're making a mountain out of a molehill. It's perfectly normal for a program to create an indeterminate number of objects.

    I don't know but you seem to have overlooked the possibility of creating objects on the heap and handling them by pointer.

    An easy way to establish an association between names and objects is to use a map (or an unordered_map). The key holds the unique name (an int or a string or whatever) and the value holds the associated object, either directly or indirectly via pointer. If objects are held by pointer the overhead is just the size of a pointer. If a name in the map is associated with the nullptr pointer you know the object hasn't yet been created. This allows you to initially enter all predefined names into the map and then create the associated objects "on demand" when requested.

    It's hard to tell what you want to accomplish without more detail.
    Last edited by nuzzle; October 20th, 2011 at 10:50 AM.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Predefining Object Names Before Initialization

    Quote Originally Posted by hixidom View Post
    It seems to me that I would need some way of systematically naming the objects, but I'm not sure how I would name an object indirectly - by means of a string or char array or something. From what I've read online, it seems that it's impossible to do this.

    My second thought is that I could initialize an array of objects, but I'd rather not have to initialize all of the objects at once for memory reasons; I'd like to be able to initialize the objects individually, as needed.
    Any container will serve this purpose. STL provides several containers, each with different properties. A std::vector acts like a resizable array; a std::list acts like a linked list; a std::set or std::multiset acts like a balanced binary tree. There are others.

    If you would like to refer to objects by name, consider a std::map or std::multimap. These are tree-like as well, but allow searches based on a key which is separate from the object being stored, unlike sets.

  6. #6
    Join Date
    Jul 2007
    Posts
    33

    Re: Predefining Object Names Before Initialization

    Thank you very much for all of the responses; they were all very helpful.

    I have read about vectors before but have never really used them, and I've never even heard of lists or maps before. I think I'm going to go ahead and take the vector route here. I know I should probs learn about lists and maps as well, but necessity is what drives my learning. I guess the problem is that I'm satisfied with using what seems like simple brute force function-based coding because, for all intents and purposes, I've always been able to accomplish what I needed to with just that, until now that is. And yet, I realized that I could solve this problem by formatting the data in a certain way and writing it to systematically-named files instead of using objects at all, but I feel like it's time I grow up and learn how to implement classes, and hopefully the filling in of the rest of the gaps in my knowledge of C++ will fill in shortly hereafter.

    Anyways, I'm pretty confident about how this is going to work now (with a vector of class objects).
    Thanks again for the insights.
    Andy

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

Featured