0

This is a sample code for memory mapped file share. The mapped_region is d class that is responsible for it Now me before digging in deep I cant follow why such declaration are used. can any one please explain this to me ?

class mapped_region
{
   // Non-copyable
   mapped_region(const mapped_region &);

   // Non-assignable
   mapped_region &operator=(const mapped_region &);

   public:

   typedef /*implementation-defined*/ offset_t;
   typedef /*implementation-defined*/ accessmode_t;
   static const accessmode_t          invalid_mode;
   static const accessmode_t          read_only;
   static const accessmode_t          read_write;
   static const accessmode_t          copy_on_write;

   mapped_region();

   mapped_region( const memory_mappable & mappable
                , accessmode_t            mode
                , offset_t                mappable_offset
                , std::size_t             size = 0
                , const void *            address = 0);

   mapped_region(mapped_region &&other);

   std::size_t get_size() const;

   void*       get_address() const;

   offset_t    get_offset() const;

   accessmode_t get_mode() const;

   void flush(std::size_t region_offset = 0, std::size_t numbytes = 0);

   void swap(mapped_region &other);

   ~mapped_region();
};

In this example the

// Non-copyable
mapped_region(const mapped_region &); 

what does that mean ?

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
Nisha
  • 1,783
  • 4
  • 18
  • 34

5 Answers5

3

Yes, it is possible to have a constructor with parameter name same as class.
Two situations are possible:

In your code:

mapped_region(const mapped_region &);

represents a Copy Constructor, while:

mapped_region(mapped_region &&other);

represents a Move Constructor

A copy constructor is used to create copies of your class object. Whenever you pass the class object as function argument by value or a copy of your class object is needed then the compiler calls the copy constructor to create this object.

If you want to restrict the users of your class from making copies of your class object then you declare copying functions(copy constructor & copy assignment operator =) as private, and this is what your code does in this case it restricts users of your code from creating any copies of your class mapped_region. Note that the default access specifier for an class is private.

Since your code declares a Move constructor, I assume you are using C++11 and hence a better way to achieve the desired functionality here is to use explicitly deleting special member functions provided in C++11.

For Eg:

class mapped_region{
    mapped_region & operator=(const mapped_region&) = delete;
    mapped_region(const mapped_region&) = delete;
    mapped_region() = default;
};
Alok Save
  • 202,538
  • 53
  • 430
  • 533
2

This is a copy-constructor. It is used to create a copy of an instance of a class.

mapped_region foo;
mapped_region bar(foo); // creates bar as copy of foo

If you declare the copy-constructor to be private, then the second line results in a compiler-error, because it tries to access the copy-cosntructor. This is done to prevent objects of that class from being copied. This is usually done when a class wraps a resource that cannot be copied (like a file or a thread).

Community
  • 1
  • 1
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • Yes, and it says "Non copyable" because the copy constructor is private in this case. – Mr Lister Feb 15 '12 at 10:55
  • @LuchianGrigore: Right, made that more specific. – Björn Pollex Feb 15 '12 at 10:57
  • @ Björn Pollex : Thanks for helping . But I still cant understand want You mean about "This is done to prevent objects of that class from being copied. This is usually done when a class wraps a resource that cannot be copied (like a file or a thread). " Sorry If I am asking something stupid. I am new c++ – Nisha Feb 15 '12 at 11:44
2

class members are private by default.

class mapped_region
{
   // Non-copyable
   mapped_region(const mapped_region &);

   // Non-assignable
   mapped_region &operator=(const mapped_region &);
public:

   //...
};

means you declare the copy-constructor and the assignment operator private. Which means you can't copy objects of the class between one another.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1
mapped_region(const mapped_region &); 

is declaration of copy constructor.

Sometimes you don't wish your class to be copyable (so you can create one object simply by copying another). By default compiler creates copy constructor and copying is enabled. In order to prevent compiler from creating this constructor you need to declare it as private and omit its definition (implementation). Trying to create a copy of one object will fail in that case:

mapped_region mr1;
mapped_region mr2(m1); // trying to call copy constructor call - error

or

mapped_region mr1;
mapped_region mr2 = m1; // trying to call copy constructor call - error

The comment:

// Non-copyable

applied to private declaration of copy constructor suggests that its purpose is solely to prevent compiler to create a default one. The same is with operator=. With both members made private you cannot copy or assign one object to another.

Bojan Komazec
  • 9,216
  • 2
  • 41
  • 51
0

Note that when you define a copy-constructor in the private section of the class, you then do not give it an implementation.

If within the class you attempt copying you will get a link error instead. So

void mapped_region::f()
{
   mapped_region other(*this); // will compile because I have access to private functions
}

but the code won't link because it can't find the copy constructor definition. (Note that if you do this it may be hard to find where you are copying).

An alternative way to disable copying is to derive your class from boost::noncopyable, thus:

class mapped_region : boost::noncopyable
{
   //etc.

};

and this prevents copy-assignment too (operator=). Almost always when you forbid copying, you also forbid copy-assignment.

CashCow
  • 30,981
  • 5
  • 61
  • 92