[omniORB] string intialization
Sai-Lai Lo
S.Lo@orl.co.uk
18 Jun 1998 14:13:15 +0100
For backward compatiability with C, string constants in C++ are "char *"
and not "const char *" as you might expect. The use of (const char*)"Hello"
to force string copy is defined in the CORBA C++ mapping as the correct
way of assigning string constants to a CORBA string Var or string member.
See "Mapping for Struct Types" in the C++ mapping chapter for details.
Sai-Lai
"Dominic Chorafakis XE41 ext. 9049" <chorafad@access.bel.alcatel.be> writes:
> I have a question about the implementation of the
> class String_member.
>
> If I define: typedef sequence?string> MySeq;
> and in a client I do:
>
> {
> MySeq s;
> s.length(1);
> s[0] = "Hello";
> ...
> }
>
> the compiler (GNU) interpretes "Hello" as a char *
> (not const char *).
> As a result, the String_member assignment operator
> in CORBA.h
> line 213 is invoked:
>
> inline String_member? operator= (char *s) {
> if (_ptr) {
> string_free(_ptr);
> _ptr = 0;
> }
> _ptr = s;
> return *this;
> }
> When the variable "MySeq s" goes out of scope,
> the destructor of String_member
> invokes string_free to deallocate _ptr , which
> should not be done.
> The problem is solved by doing s[0] = (const char
> *)"Hello", which actually
> allocates a buffer for _ptr and copies the string.
>
> Is it intentional that the above assignment
> operator just copies the pointer ? If so,
> should the class String_member not call
> string_free ONLY if it has allocated _ptr
> by a call to string_alloc ?
>
> Sorry if this topic is already covered in the
> mailing archive but I have some
> problems searching it.
--
Dr. Sai-Lai Lo | Research Scientist
|
E-mail: S.Lo@orl.co.uk | Olivetti & Oracle Research Lab
| 24a Trumpington Street
Tel: +44 223 343000 | Cambridge CB2 1QA
Fax: +44 223 313542 | ENGLAND