Pointers

When is it neccesary to implement a default constructor,a copy constructor,an overloded assignament function and a destructor for a class?. This is a quiestion that I think It can be responded saying that when the class has pointer as a member....but is there any other situation where the programmer has to create those things to keep the security in the programm??

a default constructor
Whenever you need that your object to be default constructed or default constructed in different way.

a copy constructor,an overloded assignament function and a destructor for a class?.
+ move constructor and move assigment operator.
Whenever you need to define one of them.
Usually it happens when you have a resource which needs special handling. Pointers are one of them. Some other example might be objects having unique ID which you need to enforce (copy constructor shold not copy an ID but querry new), or cache which is invalidated by copy/assigment. Or a buffer which needs flushing before destruction.
Thank you, I see...I just knew about the pointers...I can imagina that there must be other situations where It's needed but with your explication I cant see it properly...could you post some code where It happens...thanks!!
something like:

1
2
3
4
5
6
7
8
9
10
11
12
class logger
{
  //...
    std::vector<char> buffer; //We use this to buffer data to increase speed
    std::shared_ptr<std::ostream> out; //pointer to output stream
  //...
    logger(const logger& other) : 
        buffer(), //We do not need to copy state of logger's buffer
    {}
    
   ~logger() { write_data(buffer, out); } //We want that last data we wrote would actually sent somewhere
}
I see...Basically it's when It want a special treatment for with my members...there's no rule...maybe the pointer is the just single thing that we have to because of his idiosyncrasy...but the rest of cases the are inventions to carry out what we want..
> When is it necessary to implement a copy constructor, an overloded assignament...?.
> ... when the class has pointer as a member....

See The rule of three/five/zero http://en.cppreference.com/w/cpp/language/rule_of_three

Summary for the general case:
Rule of five: if the class encapsulates ownership and direct management of resources
Rule of zero: if it does not (rule of five defaults if it is designed for polymorphic use)

For an example, see: http://www.cplusplus.com/forum/beginner/167246/#msg840256
Ok, I'll have a good look, thanks!!
Topic archived. No new replies allowed.