Explicit keyword in C++

Ever wonder what is the meaning of explicit keyword in C++? I found good explanation from StackOverflow.

 

 

In C++, the compiler is allowed to make one implicit conversion to resolve the parameters to a function. What this means is that the compiler can use single parameter constructors to convert from one type to another in order to get the right type for a parameter. Here’s an example class with a constructor that can be used for implicit conversions:

class Foo
{
public:
  // single parameter constructor, can be used as an implicit conversion
  Foo (int foo) : m_foo (foo) 
  {
  }

  int GetFoo () { return m_foo; }

private:
  int m_foo;
};

Here’s a simple function that takes a Foo object:

void DoBar (Foo foo)
{
  int i = foo.GetFoo ();
}

and here’s where the DoBar function is called.

int main ()
{
  DoBar (42);
}

The parameter is not a Foo object, but an int. However, there exists a constructor for Foo that takes an int so this constructor can be used to convert the parameter to the correct type.

The compiler is allowed to do this once for each parameter.

Prefixing the explicit keyword to the constructor prevents the compiler from using that constructor for implicit conversions. Adding it to the above class will create a compiler error at the function call DoBar (42). It is now necessary to call for conversion explicitly with DoBar (Foo (42))

The reason you might want to do this is to avoid accidental construction that can hide bugs. Contrived example:

  • You have a MyString(int size) class with a constructor that constructs a string of the given size. You have a function print(const MyString&), and you call it with print(3). You expect it to print “3”, but it prints an empty string of length 3 instead.

Kepulangan ketiga

Kepulangan ke-tigaku bukan seperti kepulangan Ke-limamu
Bukan berarti telah tiga tahun perantauanku di ranah para raja
Kepulangan ke-tigaku tidak penuh perenungan kenangan masa lalu
Tidak ada kenangan senja yang sengaja kucerna saat ini

Tidak seperti kepulangan ke-limamu yang penuh kenangan
Kepulangan ke-tigaku tidak membicarakan pernikahan, kisah cinta atau putus harapan
Tidak pula perlu kujelaskan bagaimana jalanan suram atau tempat-tempat penuh romansa masa lalu

Kampungku indah dan kubuat indah
Tak ada kisah cinta yang suram di sini, tak ada memori pahit di tanah ini

Mulai dari munculnya matahari dari sudut langit timur
Hingga wangi rumput yang baru dipotong, kesan manis kotoran sapi ataupun ranting segar yang terbakar
Teriakan Ibu pada anaknya yang tak segera mandi
Sampai terasa hangat matahari pagi begitu masuk pagar rumah

Semua tentang hari ini, masa kini dan senyum orang-orang yang penuh kasih
Besok pagi mentari akan menyapa lagi

21:28 18/08/2014
Terinspirasi puisi Kepulangan Kelima Irwan Bajang dari Album Kepulangan Kelima

Angel Hair with sauteed broccoli

Last night I went to Hypermart to buy detergent, but suddenly I saw green and gorgeous broccoli so I picked it up plus a block of butter and a bottle pasta sauce.

I never cook with real butter (not margarine), after realize its richness I decided to simply saute the broccoli and add it with a clove to give extra taste.

Simple but delicious recipes :
1. Broccoli
2. Butter
3. Olive oil
4. Salt
5. Garlic

I love Angel Hair because it works great with light seasoning, but the broccoli didn’t has enough liquid to give taste to the pasta so I added bottled pasta sauce. The result was great, It would be better with cream based sauce.

Question :
Can I keep packed cream for long time without refrigerator?

SVN external tools in Visual Studio

Tag

, , , ,

If you use SVN and Visual Studio in your project, this tips can boost your efficiency.

I frequently use diff, log and blame command via TortoiseSVN context menu, so I have to open file explorer before invoking this command. Visual Studio external command will be really handy for this case. Put your external command in tab context menu or put it in toolbar, I use both :

context

Tab’s context menu

toolbar

Toolbar menu

Step : Baca lebih lanjut

Ikuti

Get every new post delivered to your Inbox.

Bergabunglah dengan 1.350 pengikut lainnya.