How objects are stored

This is a discussion on How objects are stored within the C++ Programming forums, part of the General Programming Boards category; I was wondering about this: Code: MyObject obj1; MyObject obj2; obj1 = obj2; In Java (which is the OO programming ...

  1. #1
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198

    How objects are stored

    I was wondering about this:

    Code:
    MyObject obj1;
    MyObject obj2;
    obj1 = obj2;
    In Java (which is the OO programming language I am used to), this would cause both variables to point to the same object in memory, and cause obj2 to be garbage collected.

    A don't know if in C++ it will do what Java does or make obj2 an independent duplicate of obj1? And if so, how do you make two variables point to the same object?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Assuming that MyObject has normal copying semantics, that would cause obj1 to be a copy of obj2. They are nonetheless different objects.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    So, for example, if MyObject had a field "x", then continuing from my above example setting obj1.x = 5 would not make obj2.x == 5, right?

  4. #4
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by MTK View Post
    So, for example, if MyObject had a field "x", then continuing from my above example setting obj1.x = 5 would not make obj2.x == 5, right?
    Right. It's copy constructed. If you wanted that behavior, you'd have to explicitly state it with a reference (&) or pointer (*) or something. Also given these are normal objects they will be destroyed at the end of the scope, freeing the memory. If you allocated the memory with malloc or new then you'd have to worry about collecting your own garbage (or use a smart pointer, etc). You get the idea.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Quote Originally Posted by Dae
    It's copy constructed.
    In this case copy assignment, not copy construction, is used.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    And in a case where I need the Java-like behavior, I guess should use pointers.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Quote Originally Posted by MTK View Post
    And in a case where I need the Java-like behavior, I guess should use pointers.
    To get closer to Java you probably want reference-counted smart-pointers, like shared_ptr.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    Quote Originally Posted by MTK View Post
    And in a case where I need the Java-like behavior, I guess should use pointers.
    Depends. You should prefer references to pointers where possible. References have the limitation that they cannot be reassigned to refer to something else.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    I read about references, and it says that once the scope of the originally declared variable runs out, all references will be invalid. Is there a way to avoid this?

  10. #10
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by MTK View Post
    I read about references, and it says that once the scope of the originally declared variable runs out, all references will be invalid. Is there a way to avoid this?
    How is that different from a pointer? A reference is just a pointer with different, sometimes-nicer-to-use language semantics.

    If you pass a pointer to a local variable to a function that runs in another thread, and original function finishes execution, the local variable will be destroyed and what the pointer in the thread then contains is undefined.
    Same thing with references.

    Is that not what you're referring to?
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  11. #11
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    I'm just very strongly used to Java's way where all objects are actually references, and the object is destroyed only when all references are gone, not when the original reference went out of scope.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,535
    That is exactly what smart pointers do.

    I find Java's references to be a real pain and a mess. They act like some reference-pointer hybrid.
    C++ is the real deal, I would say, since using the == operator can actually compare the objects instead of the pointers (comparing two references compares the two objects).
    You can also choose whether to check the pointers for equality or the objects themselves.
    Last edited by Elysia; 11-29-2009 at 02:46 PM.
    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.

  13. #13
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Doesn't Java's way use less CPU and memory because it doesn't do all that copying?

  14. #14
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by MTK View Post
    Doesn't Java's way use less CPU and memory because it doesn't do all that copying?
    Oh lord. All it does is copy a 4 byte pointer. That's nothing compared to what Java does behind the scenes. C++ is quick, as in very quick. You don't really need to second guess the compiler very often.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  15. #15
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    I thought C++ copies the whole object, not just a pointer.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector of pointers to vector of objects
    By Litz in forum C++ Programming
    Replies: 10
    Last Post: 11-06-2009, 02:29 PM
  2. Objects as attributes of other objects.
    By kbro3 in forum C++ Programming
    Replies: 10
    Last Post: 08-15-2009, 03:46 PM
  3. Replies: 60
    Last Post: 12-20-2005, 10:36 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM

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