-
March 24th, 2004, 12:08 PM
#1
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
-
March 24th, 2004, 12:18 PM
#2
Can't do that. Typically one would create an array of counters in that situation.
-
March 24th, 2004, 01:06 PM
#3
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?
-
March 24th, 2004, 01:14 PM
#4
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.
-
March 24th, 2004, 01:17 PM
#5
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.
-
March 24th, 2004, 01:22 PM
#6
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();
}
-
March 24th, 2004, 08:24 PM
#7
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.
-
March 24th, 2004, 08:57 PM
#8
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
-
March 24th, 2004, 09:13 PM
#9
I think Justin has switched back to MATLAB. C++ seems so much more complicated.
-
March 24th, 2004, 11:01 PM
#10
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
-
March 25th, 2004, 01:10 AM
#11
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.
-
March 25th, 2004, 07:31 AM
#12
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
-
March 25th, 2004, 02:25 PM
#13
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.
-
March 25th, 2004, 03:46 PM
#14
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
-
March 25th, 2004, 04:42 PM
#15
So what happens when you convert the code you asked about to C using the MATLAB conversion?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|