Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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: ...
  1. #1
    Just 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

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Code:
    The question is how can i create an object whose name is not know at compile time but during the run time
    What do you mean by name...do you mean the label

    i.e.

    int x;

    where x is the symbolic label the program uses to access the memory at x

  3. #3
    Just Joined!
    Join Date
    Sep 2007
    Posts
    33

    Yes i mean the labal

    Yes friend I meant to say the label.

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Probably the easiest way is to use a pointer and allocate memory as needed and use the pointer label to access this memory.

  5. #5
    Just 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.

  6. #6
    Just Joined!
    Join Date
    Sep 2008
    Posts
    20
    why don't you use "Dynamic Array" instead??

  7. #7
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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:
    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);
    }
    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".

    Make sense?
    DISTRO=Arch
    Registered Linux User #388732

  8. #8
    Just Joined!
    Join Date
    Sep 2007
    Posts
    33
    Many thanks friend.

    I think i know what you mean.

  9. #9
    Just 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.

  10. #10
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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:
    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;
        }
    }
    My interaction with such a program might look like this:

    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]
    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").

    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

Page 1 of 2 1 2 LastLast

Posting Permissions

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