RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Blocking Undesirable Object Copying and Implicit Conversions-2 : Page 2

C++ doesn't offer a direct mechanism for blocking undesirable copying or preventing implicit conversion of arguments. However, with some code tweaks you can work around these problems.


Describing the Problem
Suppose you're designing a class whose objects must neither be copied nor assigned to. Because the compiler declares a copy constructor and an assignment operator implicitly, clients will still be able to copy and assign such objects:

class Myfile
 explicit Myfile(const char * name);
 virtual ~Myfile(); 
 virtual int open(const char * name);
 virtual int close();
int main()
 Myfile datafile("mydata.txt");
 Myfile otherfile(datafile);//undesirable copy ctor
 Myfile third;
 third=otherfile; //undesirable assignment

The implicitly declared copy constructor and assignment operator allow the program to copy construct and assign Myfileobjects, possibly causing undefined behavior. The problem is that you have no way to instruct the compiler to not declare these member functions implicitly.

Undesirable implicit conversions present a similar challenge for programmers. Take for example the standard class template std::complex. Its non-default constructor takes one or two floating point arguments:

std::complex <double> compnum2(NULL);//also compiles!

This is well and good. However, because C++ is lenient with respect to conversions of arithmetic types, you can easily get into trouble when the wrong type of argument is passed to the constructor:

std::complex <double> compnum=true;//compiles!
std::complex <double> compnum2=(NULL);//also compiles!

Not only does this weird code compile successfully, you won't even get a warning from your compiler. The problem is that the compiler implicitly converts arguments such as true and NULL to double implicitly. That doubleis then used as the initializer of a complex object. Such conversions most likely indicate a bug but they compile silently. The solution for both problems is based on the same technique. Let's take a look at it.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date