Creating New Objects! (but not what you may think)
Okay, I don't mean creating objects like "int a = 1;" kinda thing. Here's what I'm curious about:
What code or process could I use to make it possible to create/name objects? For example:
cout << "Input your character's name: ";
cin >> name;
*new object which is named by the cin* = string 'name'
OR even going as far as creating classes, to allow player.strength, player.rage, you know, for easy organization.
I could use this in the simplest explanation possible, but if you know where I can just look at a site for good help, that'd be equally good.
Thanks everyone!
Re: Creating New Objects! (but not what you may think)
You can't give variables names at runtime. You can use a map to map your object to a string or whatever if you need to reference them that way.
I don't understand the second part of your question.
Re: Creating New Objects! (but not what you may think)
Re: Creating New Objects! (but not what you may think)
Okay, I'll try to be a wee bit less complicated than my main post.
Say you wanted to create a character. The character has three stats: Strength, Agility, and Wisdom. You also can name him.
So, is there something I can use to fill out a class like this:
class Character
{
public:
int strength;
int agility;
int Wisdom;
};
See what I mean? Having the ability to make an infinite amount of different, user-defined instances of a class. Or simply objects. If I'm making any sense whatsoever haha.
Re: Creating New Objects! (but not what you may think)
Sure. Just use new to create as many as you want. Overload the constructor to take the appropriate arguments. As I said in the first reply, if you want to be able to refer to them by some kind of name, use a map to map the pointers to the string name.
Re: Creating New Objects! (but not what you may think)
Quote:
Originally Posted by
Befall
Okay, I'll try to be a wee bit less complicated than my main post.
Say you wanted to create a character. The character has three stats: Strength, Agility, and Wisdom. You also can name him.
So, is there something I can use to fill out a class like this:
class Character
{
public:
int strength;
int agility;
int Wisdom;
};
See what I mean? Having the ability to make an infinite amount of different, user-defined instances of a class. Or simply objects. If I'm making any sense whatsoever haha.
:confused: Why not just make a constructor for your class, then make instances of it using new?
Re: Creating New Objects! (but not what you may think)
Think about how you post on this forum: you have some post content, maybe a post title, timestamp, etc.
Basically, you provide a user interface. Maybe the user has this "character management panel" where characters can be created. You might take the user input and create a Character object, perhaps to be stored in some container, and maybe also stored in a database.
Re: Creating New Objects! (but not what you may think)
Quote:
Originally Posted by
Peter_B
:confused: Why not just make a constructor for your class, then make instances of it using new?
Hehe... I'm kind of a giant n00b at programming. So I suppose I'll have to look into what a constructor is, because I'm oblivious. :P
Re: Creating New Objects! (but not what you may think)
I think what the OP is looking for is STL collections, such as std::map and std::vector.
Re: Creating New Objects! (but not what you may think)
Quote:
Originally Posted by
Befall
Hehe... I'm kind of a giant n00b at programming. So I suppose I'll have to look into what a constructor is, because I'm oblivious. :P
That's a fundamental concept to the language and to OOP. Unfortunately there's a pretty high learning curve.
Re: Creating New Objects! (but not what you may think)
Quote:
Originally Posted by
Befall
Okay, I'll try to be a wee bit less complicated than my main post.
Say you wanted to create a character. The character has three stats: Strength, Agility, and Wisdom. You also can name him.
You could either set up a system which matched strings (names) with Character objects, like a std::map, or simply make the name just another attribute of the Character.