Creating New Objects! (but not what you may think)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Creating New Objects! (but not what you may think)

  1. #1
    Join Date
    Dec 2008
    Posts
    7

    Talking 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!

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,050

    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.

  3. #3
    Join Date
    Jan 2009
    Posts
    596

    Re: Creating New Objects! (but not what you may think)

    You might want to look at using a Factory - try http://en.wikipedia.org/wiki/Factory_method_pattern

  4. #4
    Join Date
    Dec 2008
    Posts
    7

    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.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,050

    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.

  6. #6
    Join Date
    Jan 2009
    Posts
    596

    Re: Creating New Objects! (but not what you may think)

    Quote Originally Posted by Befall View Post
    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?

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,246

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Dec 2008
    Posts
    7

    Re: Creating New Objects! (but not what you may think)

    Quote Originally Posted by Peter_B View Post
    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

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    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.

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,050

    Re: Creating New Objects! (but not what you may think)

    Quote Originally Posted by Befall View Post
    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.

  11. #11
    Join Date
    Aug 2007
    Posts
    858

    Re: Creating New Objects! (but not what you may think)

    Quote Originally Posted by Befall View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center