Re: Coponent Question for C++ Language Expert
Reporter wrote:
I posted a question related to this on the Open Office Org forum.
Basically the Open Office API interface is defined as a component
interface and its native objects are called UNO objects.
"string" is a fundamental type in UNO.
In order to create UNO Component compatable strings in C++ a static
function "OUString::createFromAscii" is used as follows:
rInstance =rServiceManager-
createInstanceWithContext( OUString::createFromAscii("com.sun.star.bridge.UnoUrlResolver" ))
My Question:
Why not add a constructor to OUString which accepts a char * as a
parameter?
Why not an implied conversion?
Because implicit conversion is the root of all evil..just kidding. First
of all, this kind of type conversion operator should usually be declared
as explicit. Did you expect this to work?
class A{
public:
A(){}
A(int x){}
};
int main(){
A y;
y = 3; // y.operator=(const A & A(int));
}
or this is more like what you want?
class A{
public:
A(){}
explicit A(int x){}
};
int main(){
A y;
y = 3; // Error
}
With implicit conversion, compilers will do under-the-hood work for you
to generate compilable code, which may or may not be what you want. It's
all good if it is what you want, but otherwise it opens a can of worms
beneath your nose.