Calling an object's constructor?

This is a discussion on Calling an object's constructor? within the C++ Programming forums, part of the General Programming Boards category; Why not? Maybe you want a vector of mp_images or a map? But there's so many more containers in the ...

  1. #31
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    Why not?
    Maybe you want a vector of mp_images or a map?
    But there's so many more containers in the standard, too, of which all would be broken by auto_ptr.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  2. #32
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> What do you think of that idea?
    Still dangerous. Trying to roll your own solution is still going to leave you with problems.

    What about shared_array?

  3. #33
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by Daved View Post
    >> What do you think of that idea?
    Still dangerous. Trying to roll your own solution is still going to leave you with problems.

    What about shared_array?
    What I relized about shared_array is that it will always remove the pixel data after all mp_images using the data have been removed. My class has been given a constructor to create an image from already existing pixel data (such as from a SDL_Surface), meaning that not all mp_images do really own the pixel data, but they still have r/w access to it. Which means that some mp_images do not have permission to remove the pixel data when destroyed (for which I have a flag to tell), but would do it anyway if I use shared_array. I have thought of implementing a simple counter instead of a shared_array, though, and let the mp_image destructor decide when to delete the data or not.
    Last edited by TriKri; 08-17-2008 at 04:23 AM.
    Come on, you can do it! b( ~_')

  4. #34
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> meaning that not all mp_images do really own the pixel data
    If the mp_image doesn't own the data, then don't have it use new or delete at all and let whatever owns it take care of the memory management. All you have to do is make sure all the instances of your class don't try to use the image once you destroy it. Otherwise that makes it really easy, just store a pointer to the data.

  5. #35
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    It has other constructors as well, which allocates memory for new pixel data. In that case the object does become responsible for removal of the data.

    Just a question, what feels the most natural to you; assignment which copies the pointer and not the data (this would be the same as using memset on the two objects), or assignment which copies the data so you actually have two different but identical images?
    Come on, you can do it! b( ~_')

  6. #36
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    The first, with shared pointers (std::tr1::shared_ptr or boost::shared_ptr).
    But beware if you try to modify the pixel data later in the class, since it's shared.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #37
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> what feels the most natural to you
    They are equal, it depends on your situation.

    Are there any scenarios in which both copies will be active at the same time? If not, then copying the data is not a good solution.

    If there are those scenarios, then the question is if one object modifies the data should that change be reflected in the other object's data, or should the other object's data be unaffected. If the other object should be unaffected, then you need to copy the data. If the other object should be updated, then you need to share the data.

    If you can answer those questions, then you'll be closer to what you need. If the answers are different in different situations, then you might want to re-think your design and make it all work the same or come up with different objects for different scenarios.

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

Similar Threads

  1. Replies: 10
    Last Post: 06-02-2008, 08:09 AM
  2. Replies: 60
    Last Post: 12-20-2005, 10:36 PM
  3. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM
  4. Calling constructor of the base class of a derived class..
    By CaptainPenguin in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2003, 12:47 PM
  5. Can't find my constructor
    By Nippashish in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 07:17 PM

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