Thread: Accessing an object vs. accessing a reference to the object

  1. #1
    Registered User
    Join Date
    Dec 2006

    Accessing an object vs. accessing a reference to the object

    Everything seems to compile fine, but I'm getting a run-time error. What I don't understand is these are identical, the only difference being one is accessing an object directly and the other is accessing a reference to the object. For some reason I can declare the reference no problem, but as soon as I try to access it, it breaks.
    //part of cEntity.cpp
    void cEntity::SetWornHead(cArmor * head)
    {wornhead = head;}//wornhead declaration: cArmor * wornhead
    cArmor* cEntity::GetWornHead(void)
    {return wornhead;} 
    void cEntity::SetWornShoulders(cArmor shoulders)
    {wornshoulders = shoulders;} //wornshoulder declaration: cArmor wornshoulders
    cArmor cEntity::GetWornShoulders(void)
    {return wornshoulders;}
    cEntity::cEntity(char* thisname)
         GetWornHead()->GetArmorClass(); //threw that in just to see if it would work in the constructor, and it did.
    Everything up to here works, butwhen i want to do something like this, I get the error
    void cEntity::SetAC(void)
    	ac = GetWornHead()->GetArmorClass() + //doesnt work
                    //GetArmorClass() is a member of the class cArmor

  2. #2
    Registered User
    Join Date
    Jan 2003
    when gWornArmorTravelersHead falls out of scope wornhead is pointing to undefined behavior. I don't see any reason that wornhead needs to be a pointer.

    Prefixing an & to a variable returns the address, it does not turn it into a reference. declaring a type with the & creates a reference variable.
    cArmor & wornhead = helm;
    makes any changes to wornhead change helm, realistically wornhead is just another name for helm and if at all possible the compiler will just treat it as such.
    trivial a;
    trivial &b = a;
    std::cout << a <<", " << b << std::endl;
    could legally, and probably should generate exactly the same code as
    trivial a;
    std::cout << a <<", " << a << std::endl;
    The compiler usually can't quite do this, so it fakes it by using a pointer behind the scenes but it's a little bit more than just a "pointer that can never be null".

  3. #3
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    You don't have a reference in your code...
    SetWornShoulders copies the object to your class member.
    SetWornHead sets the pointer to the external object. And as it was said - in case that the object is destroyed by some reason you have a dangling pointer you cannot access.

    Note that the SetWornShoulders can be rewritten in the following way:
    void cEntity::SetWornShoulders(const cArmor& shoulders)
    {wornshoulders = shoulders;}
    The logic stays the same, but instead of copying object twice - once in the temporary variable put into function and second time into your member variable, - you avoid first copy and leave only the second one
    To be or not to be == true

  4. #4
    Registered User
    Join Date
    Dec 2006
    thx for the replies....for some reason I'm having a hard time getting a good grasp of referrers/pointers etc, most everything else has come w/ relative ease to me and I have been able to get it by just reading/using. Does anyone know of some good material on this subject specifically?(either excercises or reading material or anything really would be great).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Differene between reference object and value object
    By csonx_p in forum C++ Programming
    Replies: 17
    Last Post: 09-01-2008, 02:47 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. object missing in reference to `Window::border'
    By leeor_net in forum C++ Programming
    Replies: 7
    Last Post: 06-02-2008, 03:13 PM
  4. Managing a reference object
    By Mario F. in forum C++ Programming
    Replies: 4
    Last Post: 11-23-2006, 07:41 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM