Thread: Users and characters

  1. #1
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156

    Users and characters

    Hi all.

    I have a User class with two member variables, m_sUsername and m_sPassword. I created this class only a few minutes ago, and before I know it i'm stumped. How do I link a User to the character created by it? Say I just registered and logged in. I've now created a character and want to logout. I logout and log back in again and I want to play as the same character. How would I go about saving the character to that specific User?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Keep a vector with (pointers to) characters in the user class?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are several methods, but if a user ALWAYS has ONE character, then you would store the character (or better, some form of ID or reference) inside the user. In this case, it may still be a good idea to store the user ID inside the character.

    If there are multiple characters per user, you definitely HAVE to store the user-ID inside the character.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    So I would create a reference to a character created by the user and store it in the User class? Sounds good, thanks!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, you would create an instance of a character class and store a pointer (best bet is a smart pointer, std::tr1::shared_ptr) in the appropriate user class.
    Pointers and references aren't the same thing in C++ - you had better remember that! A reference just won't work here!

    Just remember that you have to create instances and populate that array everytime a user logs in.
    For performance, you might consider storing IDs and creating instances of character classes as they are first needed and then cache them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Hmmm.. What if I create a reference in my User class to a pointer that points to character that's in my Character class? Or would that not work either?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What would be the point of that?
    You would want a vector of your character objects (not pointers).
    You could start with that and see if you get a big performance hit. It's good not to do premature optimization.

    Code:
    class User
    {
        std::vector<Character> m_Characters;
        // ...
    };
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I would advise against storing a pointer or reference to any instances of a character inside of a user class. I would store some type of character identifier that might return a ref counted pointer or perhaps a boost shared ptr to the user class from some type of character manager class.

  9. #9
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Ohhhhhh that looks and sounds better than my idea

    I'm still in the early learning stage and my code is still in the development stage, thanks a lot!

    I'm sure you will be hearing from me soon when another problem arises!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Displaying Numbers divisible by user's desired number
    By DaniiChris in forum C Programming
    Replies: 9
    Last Post: 07-07-2008, 02:06 PM
  2. cin.getline question (user's input too long)
    By JerryL_MB in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2002, 12:17 PM
  3. Basic C Programming Help Needed
    By Smurphygirlnz in forum C Programming
    Replies: 8
    Last Post: 09-26-2002, 07:12 PM
  4. Replies: 2
    Last Post: 05-05-2002, 01:38 PM
  5. limiting length of users input
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-20-2001, 09:55 AM