What is wrong with this reference?

This is a discussion on What is wrong with this reference? within the C++ Programming forums, part of the General Programming Boards category; let's say I have this struct: Code: struct H { K mK; // ... }; and this class: Code: class ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    What is wrong with this reference?

    let's say I have this struct:
    Code:
    struct H {
     K mK;
     // ...
    };
    and this class:
    Code:
    class C {
     private:
      K mK;
     public:
      void foo(const H &h);
      // ...
    };
    
    void foo(const H &h) {
     mK = h.mK;
    }
    I thought this would assign C's mK to a copy of h.mK, but this is not happening. C's mK is behaving like it's being assigned to newly allocated memory that has been deleted. For example, the value in mK changes at random points in the application without ever explicitly changing the value.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I'm not 100% sure, but it sounds like you want mK to be a reference to h.mK and whenever the external h.mK changes you want the local mK to change too.
    If that's what you're asking, then you need to make mK a pointer and assign the pointer values rather than copy by value as you are doing now.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    No, when I set C's mK to h.mK, I want C's mK to stay the same value as it was assigned. So, if h.mK changes at a later point, I don't want C's mK to change to that value. I want it to stay the value that it was assigned.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by yahn View Post
    No, when I set C's mK to h.mK, I want C's mK to stay the same value as it was assigned. So, if h.mK changes at a later point, I don't want C's mK to change to that value. I want it to stay the value that it was assigned.
    And it will, assuming that K operates in such a way that it follows the normal ideas of operator =. But we would need to see class K in order to tell this. Why not post the code you're actually using, so that we can see the real issue here?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Code:
    void foo(const H &h) {
    should be:
    Code:
    void C::foo(const H &h) {
    If it is already, then please post the exact code you're using.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    Bah, I did it again. That's what I have, but I didn't write the example correct.

    Code:
    template<class T>
    struct Quadtree<T>::NodeItem {
    	NodeItem(const T *item, const Coord &mCoord);
    	const T *mItem;
    	Coord mCoord;
    };
    
    template<class T>
    Quadtree<T>::NodeItem::NodeItem(const T *item, const Coord &coord) : mItem(item), mCoord(coord) {}
    
    template<class T>
    void Quadtree<T>::add(const T *item, const Coord &coord) const {
        if (rNode->getSpace().containsCoord(coord)) {
            rNode->traverse(coord)->add(NodeItem(item, coord));
        }
    }
    If you need to see more, let me know.

    Thank you

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Is Coord H or K in your example earlier?

    The actual code looks a lot more complicated than the example, which makes sense since this kind of problem is usually due to a problem with the copying of something in a complicated system.

    If you could describe from the real code what is missing and what isn't, that would help. For example, is containsCoord() returning the wrong value? Also, is Coord a simple structure with simple copying?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  2. OpenGL vs. DirectX ???
    By Grumpy_Old_Man in forum Game Programming
    Replies: 55
    Last Post: 08-18-2003, 12:52 PM
  3. Operator overloading in template classes
    By moejams in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2003, 06:16 PM
  4. OpenGL in devc++
    By Mipix in forum Game Programming
    Replies: 5
    Last Post: 07-21-2003, 02:21 PM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21