naming objects in realtime
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: naming objects in realtime

  1. #1
    Join Date
    Mar 2004
    Posts
    6

    naming objects in realtime

    is it possible to concatenate a count variable to the end of an object name so that for example a loop could create multiple objects automatically. eg if each object was called test, the loop would concatenate 1, 2, 3 etc on to the end of test so that objects called test1, test2 and test3 were created on each respective iteration of the loop? Basically I am reading in 4 lines of info at a time from a file using a loop, and using a storage class to hold the information. I want each object to have a different name as it is created in the loop...like above.

    I hope that explaination makes sense

    Justin

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,057
    Can't do that. Typically one would create an array of counters in that situation.

  3. #3
    Join Date
    Mar 2004
    Posts
    6
    array of counters?? im sorry i dont quite understand.

    What is mean is to create a new object each time but call it something different in each iteration of the loop. If the loop just contained: Object test(x,y,z); (where Object is the class being instantiated) then each interation would destroy the current instance of 'test'. Therefore to create multiple 'test' objects i need to call them something different each time the loop goes round. Adding a loop iteration indicator (like count) is just one possible way of doing it. This sort of thing is possible in MATLAB which is very similar to C so I was wondering how to do it in C/C++. I think in matlab the operator '+' is used? Maybe I could use a massive switch statement as the number of iterations in the loop is a known finite number, but this seems a rather inefficient way of doing it?

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,057
    Originally posted by Mr.JT
    array of counters?? im sorry i dont quite understand.

    What is mean is to create a new object each time but call it something different in each iteration of the loop. If the loop just contained: Object test(x,y,z); (where Object is the class being instantiated) then each interation would destroy the current instance of 'test'. Therefore to create multiple 'test' objects i need to call them something different each time the loop goes round. Adding a loop iteration indicator (like count) is just one possible way of doing it. This sort of thing is possible in MATLAB which is very similar to C so I was wondering how to do it in C/C++. I think in matlab the operator '+' is used? Maybe I could use a massive switch statement as the number of iterations in the loop is a known finite number, but this seems a rather inefficient way of doing it?
    You CAN'T do what you're asking in c++.

    As I said, you need an array of counters. Instead of accessing different counters by name (which you can't do) you access them by their position in the array. I don't really know how to put it more clearly than that.

  5. #5
    Join Date
    Oct 2003
    Location
    Romania
    Posts
    127
    Originally posted by Mr.JT
    array of counters?? im sorry i dont quite understand.

    What is mean is to create a new object each time but call it something different in each iteration of the loop. If the loop just contained: Object test(x,y,z); (where Object is the class being instantiated) then each interation would destroy the current instance of 'test'. Therefore to create multiple 'test' objects i need to call them something different each time the loop goes round. Adding a loop iteration indicator (like count) is just one possible way of doing it. This sort of thing is possible in MATLAB which is very similar to C so I was wondering how to do it in C/C++. I think in matlab the operator '+' is used? Maybe I could use a massive switch statement as the number of iterations in the loop is a known finite number, but this seems a rather inefficient way of doing it?
    MATLAB is better?
    So, what are you waiting for? Back to MATLAB.

  6. #6
    Join Date
    Jun 2003
    Location
    Gjøvik, Norway
    Posts
    204
    So you want to create X objects where X is a dynamic variable?

    I think a std::vector should do what you want: (Not tested)
    Code:
    #include <vector>
    
    class SomeClass
    	{
    	public:
    		SomeClass()
    			{}
    		SomeClass(const SomeClass& p_crCopy)
    			{(*this) = p_crCopy;}
    		~SomeClass()
    			{}
    	private:
    		// Member variables
    
    	public:
    		void DoYourThing();
    			{
    			// Do something here
    			}
    
    		SomeClass& operator = (const SomeClass& p_crCopy)
    			{
    			// Copy any member variables here
    			}
    	};
    
    int main()
    	{
    	std::vector<SomeClass> TheVector;
    	unsigned int X = 20;
    
    	// Create X SomeClass objects:
    	TheVector.resize(X);
    
    	// Access the 10th object:
    	TheVector[10].DoYourThing();
    	}

  7. #7
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Instead of test1, test2, test3 and such you can use test[1], test[2], test[3] and such. Any tutorial or book about C or C++ will explain the details.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  8. #8
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    Another alternative to the vector would be a linked list. Each "node" in the list contains a pointer to another "node" in the list. So inside the loop use the new operator to instantiate a new instance of a node. and make the previous node point to the new node. The node can contain anything...classes, plain old data types...whatever. Just have to remember how many nodes for which you allocated memory and then delete them when your done. Also need some type of "head" or some other reference to a known point in the list.

    The names of the nodes would not be unique (i.e. test1 test2 etc) but there would be a unique instance that could be created at run-time and you can make as many (or as few) nodes as your RAM and OS can handle.

    There is an STL for the linked list, but that would take all of the fun out of making your own.

    TDM

  9. #9
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    I think Justin has switched back to MATLAB. C++ seems so much more complicated.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  10. #10
    Join Date
    Apr 1999
    Posts
    27,423
    Originally posted by Sam Hobbs
    I think Justin has switched back to MATLAB. C++ seems so much more complicated.
    Too many assume that C++ is "similar to their language" and do not spend the time to actually learn C++. They pin their hopes that C++ has the same (very uncommon) constructs as their former language, such as what Justin is asking for.

    They find out that either C++ isn't like their former language and they give up, or they wind up doing line-by-line translations from their former language to C++, creating programs that are unorganized, inefficient, and in many cases, just plain wrong.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Originally posted by Paul McKenzie
    They find out that either C++ isn't like their former language and they give up
    Yes, that is what I am saying too. And perhaps Justin has done that simply because the samples are more complicated than they need to be.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  12. #12
    Join Date
    Mar 2004
    Posts
    6
    Hi,

    Thanks for all the replies...I will probably go down the vector route. I generally program in C/C++ but have only done 1 project in MATLAB and the functionality I described seemed useful and I wondered whether there was a similar thing I had overlooked in C/C++. As for this current project I will definately use C++ as I am using OpenGL and Microsoft's Speech development kit.

    Cheers

    Justin

  13. #13
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    I assume MATLAB is translated/interpreted, because the feature you describe requires translation/interpretation to implement.

    C/C++ is usually compiled and the language standard has nothing that requires translation/interpretation to implement. Therefore the syntax you describe that MATLAB uses is essentially not possible for C/C++.

    For those programmers that have become familiar with just a translated/interpreted development tool and that have become proficient with it, the absence of some features might be scary. Yet (I am sure) nearly all C/C++ programmers proficient with the langauge are confident they don't need them.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  14. #14
    Join Date
    Mar 2004
    Posts
    6
    correct...i am confident i dont need them! just wondered that all. And also correct MATLAB is an interpreted environment. However, MATLAB code can be automatically converted to C to be compiled, although this sometimes produces slightly erroneus results, no doubt due to the different features that MATLAB has that take advantage of its interpreted development environment.

    J

  15. #15
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    So what happens when you convert the code you asked about to C using the MATLAB conversion?
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center