In C++ is perfectly legitimate to do:
bool x = "hi";
Because "hi" is translated by compiler to a char array and returns a pointer to that array, which is a number and number can be implicitly converted to bool
(0 is false, anything else is true).
So I have these ctor:
Exception(QString Text, bool __IsRecoverable = true);
Exception(QString Text, QString _Source, bool __IsRecoverable = true);
Sadly I figured out that calling
Exception *e = new Exception("error happened", "main.cpp @test");
It creates a new instance of "Exception" class which is created using Exception(QString Text, bool __IsRecoverable = true);
constructor, which is wrong to a point.
Is there a simple way to ensure that correct function is called, other than restructuring the constructors entirely, changing position of arguments, etc?
Firstly, I'm not sure why you're dynamically allocating an exception class. I'm not sure that's ever a good idea.
You can explicitly construct a QString
:
Exception e("error happened", QString("main.cpp @test"));
Or you can pass the third argument:
Exception e("error happened", "main.cpp @test", true);
Or you can add an additional constructor that takes const char*
and will be preferred over the conversion to bool
:
Exception(QString Text, const char* Source, bool IsRecoverable = true);
You can easily make this forward to the QString
version. Also note that names beginning with an underscore and a capital letter or with two underscores are reserved.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments