Thread: Class question

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    29

    Class question

    hi,
    I am working with simple classes. When setting a class for an animal I use this code:

    Code:
    animal someName;
    someName.info();
    So basically this creates a new animal class by the name of someName. Here's my problem: What do I do if I want the class name to be a variable? For example I want to be able to make a few animal classes, and each class name would be going up by 1.

    (animals1, animals2, animals3)

    to do this the name would have to be a variable. I tried creating a new class by using a pointer but it did not work. If anyone can shed some light on this it would be apreciated.

    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You seem to be heading for
    Cat cat("Tom");
    Dog dog("Butch");
    Mouse mouse("Jerry");

    Where I think you really want is say
    Animal animals[3] = { "Tom", "Butch", "Jerry" };

    The class represents the attributes common to all the members of your class (eg a name), and then you create instances of that class with specific names.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    29
    well the example is not what I'm realy trying to do. Basically I want to be able to create a class name by whatever input the user has given me.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You can't really do that with C++ nor would I see the reasoning behind why you would want to.
    Woop?

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    animal someName;
    someName.info();
    So basically this creates a new animal class by the name of someName. Here's my problem: What do I do if I want the class name to be a variable?
    Those lines don't create a new animal class by the name of someName. They create an object called someName that is a member of the animal class.

    Also, a class name is not a variable--it's a type just like int, double, string, etc. are types.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Mr.Pink
    well the example is not what I'm realy trying to do. Basically I want to be able to create a class name by whatever input the user has given me.
    If I'm understanding this right, you want a factory class or function.

    The following is a very simplistic example with the problem that, whenever you create a new class of Animal, you have to modify the function;
    Code:
    std::auto_ptr<Animal> AnimalFactory(const std::string &type, const std::string &name)
    {
        std::auto_ptr<Animal> retval;
        if (type == "Cat")
            retval = new Cat(name);
        else if (type == "Dog")
            retval = new Dog(name);
        //  other known animals
        return retval;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 06:08 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM