Design Issue.

This is a discussion on Design Issue. within the C++ Programming forums, part of the General Programming Boards category; I have found this problem in my programs a lot, so I will post it here to hear your opinions. ...

  1. #1
    Unregistered
    Guest

    Design Issue.

    I have found this problem in my programs a lot, so I will post it here to hear your opinions. Currently Im having a class that handles files (CFileManager). What it does is: Open file, alloc memory for file buffer, return buffer. (this buffer will be used by other derived classes to, for example, handle images or sounds).
    The problem, as you might guess is the allocation of memory. If this class allocates memory, some other class is responsible and must deallocate it. I found this horrible to mantain, and its an easy thing to forget.

    I will tell you the solutions I have in my mind, and tell me what do you think:
    1) Do not let the file manager class to allocate memory, pass a buffer with memory already allocated. This has the drawback that you need to know the size of the file previously.
    2) Create a CFileBuffer class with two functions: AllocMem and DeleteMem.

    Thx in advance. All comments are welcome.

  2. #2
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Just recently I started a thread here where I was asking exactly what's the best way to deal with dynamic data members. You can find it here

    Without disregard to any other reply I got there, take notice of Sorensen's solution further down to the end of the thread. You may find it useful.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Actually, the solution is simpler than you thought! See, when a derived class is being destroyed, the base class destructor is called too! Compile and run this:

    Code:
    
    class Foo{ public:
    
    ~Foo(){ cout << "Foo Destructor Being Called" << endl; getch();}
    
    };
    
    
    
    class Bar : public Foo{ public:
    
    ~Bar(){ cout << "Bar Destructor Being Called" << endl; getch();}
    };
    
    
    
    
    class Tee : public Bar{ public:
    
    ~Tee(){ cout << "Tee Destructor Being Called" << endl; getch();}
    };
    
    
    
    
    
    int main(int argc, char *argv[])
    {
    
    
     Tee s;
    
    
    
      return 0;
    }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    By the way: there is another useful mechanism in C++: the pure virtual function. In essence, declaring one in the base class forces each derived class to define it's own definition of the function!

    Compile this and see what I mean:

    Code:
    
    
    
    
    
    class Foo{ public:
    
    virtual void Required() = 0;
    
    };
    
    
    
    class Bar : public Foo{ public:
    
    
    };
    
    
    
    
    
    
    
    
    
    int main(int argc, char *argv[])
    {
    
    
     Bar s;
    
    
    
      return 0;
    }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  5. #5
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    There is auto_ptr, which is pretty nice for transferring ownership and making damn sure the owner is responsible for deletion, but it makes sharing objects impossible.

    http://www.gotw.ca/publications/usin...ffectively.htm
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Software Design issue
    By zacs7 in forum Tech Board
    Replies: 1
    Last Post: 04-22-2009, 05:34 AM
  2. Design Issue, Your Thoughts Wanted
    By IdioticCreation in forum C++ Programming
    Replies: 5
    Last Post: 02-02-2008, 06:28 PM
  3. Replies: 3
    Last Post: 11-16-2006, 03:23 AM
  4. dll : design issue!
    By dug in forum C++ Programming
    Replies: 8
    Last Post: 02-17-2005, 10:48 AM
  5. Brainstorming: How to design this issue?
    By Cristian Negres in forum C++ Programming
    Replies: 5
    Last Post: 08-22-2002, 02:17 AM

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