DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Variable class name

  1. #1
    Join Date
    Mar 2007

    Variable class name

    is there a way to make name of a declared class variable? Like:

    Say you have a class named Cat:

    Can i say: Cat name;

    where name = frisky or somthing the user inputs?
    or do i have to give the class a definent name

    What are classes most used for? I would think games would be a major place where classes are used but where else?

  2. #2
    Join Date
    Dec 2003
    you can NOT create a variable with a name from a users input. This would also not help, because your code could not reference the variable before it is named, making the created entity useless.

    You can however put the user's entered string into the variable and search against it or use it as with any other piece of data ... simply add "name" to the class and use it.

    Classes are used everywhere. They allow the creation of powerful tools that can be reused; the ability to lump functions and data together into a package that works and can be reused in many programs is very powerful for any set of tasks. The ability to inherit to build large tools from existing small ones is very powerful as well, and the additional support for making classes more generic in C++ provides amazing power (consider the built in vector type which can hold anything in an array like structure and perform many handy functions on it).

  3. #3
    Join Date
    Mar 2007
    I wasnt suggesting to create the variable after i named the class. But dont i have to define an object before i can pass variables to it. So i want to make the object name that i am defining variable.

    So basically i am asking the user to input a name and then i want the program to define an object from the class with the name they input. But i dont think i can just do

    Cat name;

    because it will think that name is the name of the object i am defining and not the string the user inputed. Is there a way around this?

  4. #4
    Join Date
    Dec 2003
    you cannot do what you are wanting to do in c++. You cannot even make a reference or a pointer that will do the trick.

    if you could, imagine...

    cin >> name; //user types "bob"
    cat name;
    bob.field = value; //how did you know he would type bob? what if it was sue?

    there would be no way to use the object by name in your code, because it would not exist at compile time only at run time.

    the reverse is very frustrating ... you cannot get the name of a variable if you want to print it (in particular, enums would be far more powerful if you could do that!)

  5. #5
    Join Date
    Mar 2007
    how then would i go about creating objects from classes on the fly?

    So, say i have a game and i want to make a object from my class Cat. What could i do to create the object on the fly if i cant use variables as the name.

  6. #6
    Join Date
    Apr 2007
    The 'new' operator.

  7. #7
    Join Date
    Nov 2003
    You're confusing two concepts here: creating objects on the fly and naming objects. You can create objects dynamically by using new. You don't have to read the name of the object from cin. You can also use an array of objects. In that case, each object is referred to by an index, not by its name.
    Danny Kalev

Similar Threads

  1. Replies: 3
    Last Post: 04-19-2007, 06:44 PM
  2. Help with Error in class
    By velkropie in forum Java
    Replies: 5
    Last Post: 03-24-2007, 11:09 AM
  3. Replies: 3
    Last Post: 04-26-2006, 04:20 AM
  4. Replies: 7
    Last Post: 01-16-2006, 05:04 PM
  5. Assembly class
    By Shailesh C.Rathod in forum .NET
    Replies: 2
    Last Post: 03-13-2002, 08:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center