Thread: newb question

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    newb question

    Ok. I ll note post all the code not to make things complicated. I have two classes:
    Code:
    class Character
    {
        ...
        Weapon rightHand;
        ...
    };
    
    class Weapon : public Item
    {
        ...
    }
    I want to do this:
    Code:
    Character newb;
    Weapon w;
    newb.rightHand = w;
    If I undestand correctly newb.rightHand will have a copy of w. Thus you will have double the memory for the a Weapon object. Is that bad programming? Would it make more sense having a pointer that will point to the object Weapon instead of copying the whole object?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I suppose it depends on what is going on within the Weapon function. Generally speaking this seems fine. If you are worried about the level of overhead in copying over a weapon then why not just use a (smart) pointer? Can I assume that rightHand is a public member of Character?

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    I'd suggest using a Factory.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could probably also do:

    Code:
    Character newb;
    newb.righthand = Weapon();
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-22-2008, 11:37 PM
  2. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  3. newb question: probs with this program
    By ajguerrero in forum C Programming
    Replies: 5
    Last Post: 04-19-2006, 08:04 AM
  4. newb question
    By Jan79 in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2003, 09:59 AM
  5. Win app, newb question
    By Blender in forum Windows Programming
    Replies: 9
    Last Post: 02-04-2003, 12:17 PM