views:

107

answers:

2

Hi everyone! <-- Rookie C++ here again ^^

I have another theory question , as the title suggested it's to evaluate a build of code. Basically I'm considering using this template everywhere.

I am using VC++ VS2008 (all included)

Stapel.h

class Stapel
{
public:
    //local vars
    int x;

private:
public:
    Stapel();
    Stapel(int value);
    ~Stapel(){}

    //getters setters
    void set_x(int value)
    {
        x = value;
    }

    int get_x(int value)
    {   
        x = value;
    }   

void CleanUp();

private:
};

Stapel.cpp

#include "Stapel.h"

Stapel::Stapel()
{

}

Stapel::Stapel(int value)
{
    set_x(value);
}

void Stapel::CleanUp()
{
    //CleanUpCalls
}

The focal point here is the cleanup method, basically I want to put that method in all my files everywhere , and simply let it do my delete calls when needed to make sure it's all in one place and I can prevent delete's from flying around which , as a rookie, even I know is probably not something you want to mess around with nor have a sloppy heap.

What about this build?

Good bad ? why ?

And what about using destructors for such tasks?

+7  A: 

Use smart pointers and RAII instead. That will not center all the deletes in one place, but rather remove them from your code. If you need to perform any cleanup yourself, that is what destructors are for, use them as that is the convention in C++.

David Rodríguez - dribeas
How would I go about realizing that? Just hit it on google ?
Proclyon
+8  A: 

Boost provides several utilities for RAII-style heap-managment:

  1. Smart pointer (there are several implementations here for different scenarios)
  2. Pointer Containers

Drawbacks of your proposal:

  1. In your implementation, you still have to remember to place a delete in the CleanUp-method for every heap-allocation you do. Tracking these allocations can be very difficult if your program has any kind of non-linear control flow (some allocations might only happen under certain circumstances). By binding the deallocation of resources (in this case memory) to the lifetime of objects on the stack, you do not have to worry as much. You will still have to consider things like circular references.
  2. RAII helps you write exception-safe code.
  3. In my experience, RAII leads to more structured code. Objects that are only needed inside a certain loop or branch will not be initialized somewhere else, but right inside the block where they are needed. This makes code easier to read and to maintain.

Edit: A good way to start implementing that is to get Boost. Then search your code for raw pointers, and try to replace every pointer by

  1. A reference
  2. A smart-pointer
  3. A pointer container, if it is a container that owns pointers

If this is done, your code should not contain any deletes anymore. If you use make_shared, you can even eliminate all news. If you run into any problems that you cannot solve by yourself, check out stackoverflow.com ... oh wait, you know that one already ;)

Space_C0wb0y
sounds amazing actually, just hit google for boost and continue the search for a clean up there I guess?
Proclyon
@Proclyon: I do not understand your comment. Anyhow, I have boost bookmarked ;)
Space_C0wb0y
Drawback #2 is not really a drawback of *his* proposal, but +1 nevertheless.
You
basically what I meant was, where to continue digging for more info? as a question.
Proclyon
@Proclyon: The links I have provided will bring you directly to the boost libraries I mentioned. There is a lot of stuff explained there. The wikipedia article helps too, and should give you enough keywords for more googling.
Space_C0wb0y
@Proclyon: I added some hints on how to go about implementing this.
Space_C0wb0y
@Proclyon: the key concept here is really RAII. That is how you handle cleanup in C++ (and in short, can be summarized as "let the destructor do the work". Boost is just a collection of libraries which 1) use RAII themselves, and 2) provide some tools (such as smart pointers) to make it easier to add RAII semantics to your own code. The core concept you need to understand is just RAII.
jalf
Thank you very much! This helped a lot!
Proclyon