Thread: Object not showing Polymorphic Behaviour

  1. #16
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    If I use layout* member variable as a smart pointer e.g.
    using
    Code:
    public:
      auto_ptr<Layout*> layout;
    instead of
    Code:
    public:
      Layout* layout;
    Would it automatically delete the previous value upon reassignment ??

    e.g. first this->layout = l1 willbe used
    and then this->layout = l2 will be used.
    so when l2 has been assigned would l1 be removed from memory automatically If I use smart pointer overthere ??

    remember l1 and l2 are not smart pointers they are normal Layout* Pointers.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    More correctly, it would be: auto_ptr<Layout> layout;

    However, I avoid using auto_ptrs for members since they have unusual copy semantics, which means that my own class would have unusual copy semantics (unless I simply disable copying altogether).

    so when l2 has been assigned would l1 be removed from memory automatically If I use smart pointer overthere ??
    It would not even compile, since you cannot directly assign a raw pointer to an auto_ptr.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    So Can I extend auto_ptr Class and add a overload for = operator for assignment that accepts a Raw Pointer and either Reinterpret Casts or makes a temporary auto_ptr from that raw pointer and return that ??

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So Can I extend auto_ptr Class
    Yes you can, no you should not, since auto_ptr is not designed to be a base class and does not have a virtual destructor.

    add a overload for = operator for assignment that accepts a Raw Pointer and either Reinterpret Casts or makes a temporary auto_ptr from that raw pointer and return that ??
    That is entirely unnecessary. If l1 and l2 are pointers to a dynamically allocated object, and own their respective objects, then the simple solution is to create an auto_ptr out of each of them, and then assign auto_ptrs.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Then whats the solution to automatically delete the previous value after setting a new value ??

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Then whats the solution to automatically delete the previous value after setting a new value ??
    What exactly are you trying to do?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    a Layout* pointer will be provided through Constructor.
    which will be used inthe App Class
    How ever There is a scope of changing the Layout using setLayout(Layout*) methd.

    So if somebody call's setLayout() the previous value of Layout* member variable (which is set by the constructor) will be overwritten by the new one provided by setLayout() but that previous one still stays in memory.

    I want the previous one will be automatically deleted from memory when the new One will be assigned.

  8. #23
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I think the reset method of auto_ptr is what you're asking for.
    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"

  9. #24
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    But How can I invoke that automatically If I dont have a =operator overload ??

    EDIT

    However I can Invoke that from setValue() too.
    But would that delete the previously assigned value from memory ??

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But would that delete the previously assigned value from memory ??
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Texture Management in OpenGL
    By Brafil in forum Game Programming
    Replies: 13
    Last Post: 07-16-2009, 04:32 PM
  2. Include Problems
    By loopshot in forum Game Programming
    Replies: 13
    Last Post: 02-17-2006, 03:22 PM
  3. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM