Skip to main content
gamedev.net
🔒 Locked

explicit keyword

Started by Darkor 2 replies 850+ views
Original Post
Darkor
Darkor
class trzGUI
{
public:
	explicit trzGUI();
	virtual ~trzGUI();

private:
	//Prevent illegal copying or assignments

	trzGUI(const trzGUI &gui);
	trzGUI &operator=(const trzGUI &gui);
};
This is a class template I have. All my class are created with these constructors and a destructor. I have a few queries. They say the explicit keyword is compiler specific. It prevents assignments during initilisation eg.
trzGUI gui = anotherGUI;
I've read in another article somewhere that another way to prevent this is to put the copy constructor and overloaded assignment operator in private. For extra measure I just include both ways, realising that somehow (though not exactly sure how) I've overlapped these two method's results. However, I do realise that putting the assignment operator in private prevents assigning it to anything. What can I remove without taking away the preventive benefits provided? Also, are there any side effects that can be caused if this is done? I haven't encountered any problems thus far though. Just a thought since I've been programming this way since starting on my project. =p [edited by - Darkor on November 21, 2003 11:51:00 AM]
dot
dot
quote:
Original post by Darkor
They say the explicit keyword is compiler specific. It prevents assignments during initilisation eg.


They are wrong. Explicit keyword is a standard c++ keyword.
quote:
Original post by Darkor
I''ve read in another article somewhere that another way to prevent this is to put the copy constructor and overloaded assignment operator in private.


Provide a copy constructor and an assignment operator if the default copy semantics do not apply. Make them private if you want to disable copy semantics. Otherwise, there''s no other rule to follow IMO.

On a side note, you might want to look into the boost::non_copyable. It is basically an empty class that makes its copy and private assignment operator private. It actually makes your class copy semantics clearer, then making your own private operators.

Jingo
Jingo
There is a small difference between

trzGUI gui = anotherGUI;

and

trzGUI gui(anotherGUI);

Which may be what the thing you read was referring to.

trzGUI gui = anotherGUI; requires an implicit conversion if anotherGUI is not of type trzGUI.

trzGUI gui(anotherGUI); on the other hand, is treated as an EXPLICIT conversion from the type of anotherGUI to type trzGUI.

What does this all mean? Well the explicit keyword prevents a single argument constructor that it is applied to from doing implicit conversions.

So for instance if your class looked like this

class trzGUI
{
...
trzGUI( int i);
...
};

trzGUI gui = 1; //this is legal

however if your class uses the explicit keyword

class trzGUI
{
...
explicit trzGUI( int i);
...
};

trzGUI gui = 1; //this is NOT legal as it requires an implicit conversion.

trzGUI gui(1); //this is legal even when the constructor is declared explicit, as it is treated as an explicit conversion.
Darkor
Darkor
Ah kay, so things are cleared up then. By using explicit constructors, private copy constructors and assignment operators I blocked out all the holes then?

I''m not that particular though I believe in pragmatic programming, so this might help without being too much of a hassle.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.