0

I am working through a c++ problem set for fun. I am creating a loop which will add objects to a class with random characteristics. One of these random characteristics is the object name. I have a program which randomly generates names for the class object, but I cannot figure out how to assign those names to the class objects.

Here is my class and it's constructor:

#include "stdafx.h"
#include <iostream>
#include "windows.h"
#include <string>
#include <sstream>
#include <array>
#include <time.h>
#include <conio.h>

class Bunny
{
public:
    char fname;
    int sex, age, color, status;
    Bunny(int, int, int, int);
    int s() { return (sex); }
    int a() { return (age); }
    int c() { return(color);}
    int st() { return (status);}
};

Bunny::Bunny(int s, int a, int c, int st)
{
    sex = s;
    age = a;
    color = c;
    status = st;
}

This is the Loop which generates "bunny" information.

std::string name;
for (n = 0; n < 1; n++)
{
    int num = std::rand() % 5;
    int s = std::rand() % 2;
    std::string f = "Susan"; //this is normally a function which randomly gives names
    name = f;
    int a = 0;
    int c = std::rand() % 5;
    int st;
    if (rand() % 50 == 43) st = 1; else st = 0;
    Bunny name(s, a, c, st);
}

if it was working right it should make a member like this:

   Bunny Susan(1, 0, 2, 0); 

This doesn't work because it creates a class object called "name" rather than "Susan". How can I assign a class object a name through a variable rather than manually assigning the name in the code.

5
  • C++ classes can't be modified at runtime. Commented Aug 27, 2014 at 22:15
  • 2
    Use a std::map to make strings like "Susan" to data. Commented Aug 27, 2014 at 22:16
  • 1
    Don't use single-letter variable names. It makes your code hard to read. Commented Aug 27, 2014 at 22:17
  • 2
    Also note that you seem to be confusing instances of a class with members of a class. I suggest rereading your C++ reference. Commented Aug 27, 2014 at 22:17
  • Did I get it right? Do you want to modify the source code from your program's execution and re-run it (for no reason, variable names are useless during program execution since everything gets translated with addresses and offsets)? Definitely not a good way though Commented Aug 27, 2014 at 22:19

3 Answers 3

2

You can't. The proper way to do this is to store the Bunny objects in a map in which the names are the keys, e.g.:

std::map<std::string, Bunny> bunnies;
std::string name;
for (n = 0; n < 1; n++) {
    ...
    // Assign a name to "name"
    ...
    bunnies[name] = Bunny(s, a, c, st);
}

bunnies["Susan"] will then contain the Bunny associated with the name "Susan", etc.

Sign up to request clarification or add additional context in comments.

3 Comments

You mean bunnies["Susan"]
@Barmar: Right; fixed.
how would I access class functions with the new object? From my example to access function a() id uses Susan.a(). would it be bunnies["Susan"].a()?
0

First of all, you're mixing up the use of the term 'member'. 'members' of a class are the methods and variables defined in the class, which in your case would be the variables 'fname', 'sex', 'age', 'color', and 'status' and the methods s(), a(), c(), and st(). What you're creating when you call 'Bunny name(s, a, c, st);' is not a 'member', it is an 'instance' of the class.

Second, there is no way to do what you're attempting to do. Variable names exist only for the programmer and have no meaning at runtime (unless you're debugging). Variable names must be able to be determined at compile-time, whereas your attempt to randomize happens at runtime.

When you create a 'std::string name' and later create a 'Bunny name(...)' you're creating two different variables with the name 'name', the fact that it's already in use when you reuse it doesn't have any effect on your new object's constructor. If anything, your compiler ought to be complaining to you that you're reusing an existing variable name, although the new one is restricted to the scope of the loop.

The real question however, is why would you want/need to do this at all? If your variable names were randomized, then how would you ever refer to those variables elsewhere in the program? If the reason for doing this is a need to create multiple class instances without knowing their identifiers or the total number needed in advance, then I would suggest looking at std::vector

2 Comments

The variable was just a way to move the name from the generator to the object. A list of the "Names" is generated so that I can recall the values later. Your answer would be more helpful if you included an example as jwodder did below.
I had thought about going back and adding an example after I first posted the reply, but jwodder's was more or less exactly the example I would have gone with, using a map of string values for identifiers to class instances
-1

If I understood you correctly you want to create variables not content of variables. This is not possible, the compiler generates a symbol which ultimately references a address in memory for a variable (the human readable name). At runtime these human-readable symbols have lost their use. Anyway, why would you want dynamic variable names?

1 Comment

I'm generating random class objects, up to 1000, and entering 1000 class object names seems tedious at best, so I was generating them with a loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.