-
February 13th, 2009, 01:56 PM
#1
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!
-
February 13th, 2009, 01:57 PM
#2
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.
-
February 13th, 2009, 02:00 PM
#3
Re: Creating New Objects! (but not what you may think)
-
February 13th, 2009, 02:10 PM
#4
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.
-
February 13th, 2009, 02:18 PM
#5
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.
-
February 13th, 2009, 02:18 PM
#6
Re: Creating New Objects! (but not what you may think)
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.
Why not just make a constructor for your class, then make instances of it using new?
-
February 13th, 2009, 02:21 PM
#7
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.
-
February 13th, 2009, 02:33 PM
#8
Re: Creating New Objects! (but not what you may think)
Originally Posted by Peter_B
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
-
February 13th, 2009, 02:33 PM
#9
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.
-
February 13th, 2009, 02:36 PM
#10
Re: Creating New Objects! (but not what you may think)
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.
-
February 13th, 2009, 04:54 PM
#11
Re: Creating New Objects! (but not what you may think)
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|