Results 1 to 10 of 13
Hi all,
I am new to both linux and c++.
If I want to create an object lest say from the string class.This can be done through the following command:
...
- 09-13-2008 #1Just Joined!
- Join Date
- Sep 2007
- Posts
- 33
create on object whose name will be declared at run time.
Hi all,
I am new to both linux and c++.
If I want to create an object lest say from the string class.This can be done through the following command:
string str1;
The question is how can i create an object whose name is not know at compile time but during the run time. For example the str1 will be a name inserted by the user from the console. for example:
string str1;
cin>>str1;
string str1; <-- I want to create a new sting object whose name is str1. But it will be a different string object from str1.
Can that be done through a template?
Can you please write a short example to demonstrate that?
Many thanks in advance.Last edited by cbarmpar; 09-13-2008 at 10:08 PM. Reason: typo mistake
- 09-13-2008 #2What do you mean by name...do you mean the labelCode:
The question is how can i create an object whose name is not know at compile time but during the run time
i.e.
int x;
where x is the symbolic label the program uses to access the memory at x
- 09-13-2008 #3Just Joined!
- Join Date
- Sep 2007
- Posts
- 33
Yes i mean the labal
Yes friend I meant to say the label.
- 09-13-2008 #4
Probably the easiest way is to use a pointer and allocate memory as needed and use the pointer label to access this memory.
- 09-13-2008 #5Just Joined!
- Join Date
- Sep 2007
- Posts
- 33
Many thanks for the reply.
I dont thning pointers are suitable for what i want.
For example I would like to make a program where the user creates and executes sql queries. When the user creates a new query (and hence object). The result of this query will be saved in the object. So each query created at run time by the user (and hence object) will include the result and other information.
Kindest regards.
- 09-14-2008 #6Just Joined!
- Join Date
- Sep 2008
- Posts
- 20
why don't you use "Dynamic Array" instead??
- 09-15-2008 #7
I guess I'm confused on why this is necessary. I have never heard of something like this being necessary.
For instance, let's suppose that you have a Query class in your program. A query has a label, so that the user can come back and see its results later, as well as the query itself, and the results. You might imagine code like this:
Now the user can say "Show me the query named Query42", and all you have to do is go through G_QUERY_LIST and look for an entry with the name "Query42".Code:void create_new_query() { string name; string query; string results; cout << "Please enter a name for this query: "; cin >> name; cout << "Please enter the query you want to execute: "; cin >> query; results = execute_query(query); Query new_query(name, query, results); G_QUERY_LIST.push(new_query); }
Make sense?DISTRO=Arch
Registered Linux User #388732
- 09-15-2008 #8Just Joined!
- Join Date
- Sep 2007
- Posts
- 33
Many thanks friend.
I think i know what you mean.
- 09-20-2008 #9Just Joined!
- Join Date
- Sep 2007
- Posts
- 33
Many objects with the same name?
So the list will include infinite number of objects. Each of these objects has the same name(new_query)?
If i understand it correctly the only way to search for a particular query,given its name, is read each objects name string through a loop.
In conclusion we have a list with objects of the same name.
But then if I want to modify an object of the query how can i modify it if all objects have the same name?
If you could include code samples for each question that would be great!
if that is too much ignore it.
Many thanks in advance.
Regards.
- 09-22-2008 #10
Ah, no.
Are you familiar with arrays or linked lists? "new_query" is the name of a local variable, but the object persists past the function because we have added it to a global list. Imagine, for instance:
My interaction with such a program might look like this:Code:query_list G_QUERY_LIST; void create_new_query() { string name; string query; string results; cout << "Please enter a name for this query: "; cin >> name; cout << "Please enter the query you want to execute: "; cin >> query; results = execute_query(query); Query new_query(name, query, results); G_QUERY_LIST.push(new_query); } void find_query(string name) { query *q = G_QUERY_LIST.find_by_name(name); if(q == NULL) cout << "Sorry, no query exists by the name " << name << endl; else { cout << "Name: " << name << endl; cout << "Query: " << q->name << endl; cout << "Results: " << q->results << endl; } }
See how that works? There is a difference between the name of the variable (new_query) and the query's name attribute ("My First Query").Code:Please enter a name for this query: My First Query Please enter the query you want to execute: SELECT * FROM table What query should I find? Other Query Sorry, no query exists by the name Other Query What query should I find? My First Query Name: My First Query Query: SELECT * FROM table Results: [RESULTS]
Are you familiar enough with C++ to know about object-oriented programming? If not, I suggest learning about that so you can understand classes a bit better.DISTRO=Arch
Registered Linux User #388732


Reply With Quote