Re: [gtk-list] Re: Gtk--: Stupid string-->char conversion question



Robert,

You might try using the new cast operators in C++, in this case the
const_cast <> operator, like this.

char*    fName = const_cast <char*> (d_fileSelection->get_filename ().data
());
This casts away the const attribute of the return type of the data method. I
may be wrong, but I don't see how the other way can work.
>      char *fname = d_fileSelection->get_filename().data().c_str();
Since data returns a const char* (which isn't a class), it doesn't have the
c_str method. I am going to assume that the line was a typo and that it
should have read:
>      char *fname = d_fileSelection->get_filename().c_str();
Unfortunately, this also returns a const char* which leaves you in the same
boat of assigning a const pointer to a non-const variable. If you don't like
casting (and the const_cast operator is not very far from the C-style
casting) and you are not going to modify the pointer value, you could just
create the fname variable as a const char*:
const char*    fname = d_fileSelection->get_filename().c_str();        //
.data (); They both work.

This takes care of the conversion, but without knowing what kind of odd
behaviour you are getting, I don't know if the two are related.

Hope this helps,
Gene

-----Original Message-----
From: Paul Barton-Davis <pbd@op.net>
To: gtk-list@redhat.com <gtk-list@redhat.com>
Date: Monday, June 28, 1999 8:31 AM
Subject: [gtk-list] Re: Gtk--: Stupid string-->char conversion question


>In message <OFAEE275E3.2F9A36F6-ONC125679E.004B688B@peoplesoft.com>you
write:
>>Hi,
>>
>>I'm using Gtk-- and seem to have some odd behaviour
>>when reading data from entry boxes. Here's what I'm doing
>>to get the resulting input value:
>>
>>     char *fname = (char *) (d_fileSelection->get_filename().data());
>
>Assuming that the rval is a string or string&, use
>
>      char *fname = d_fileSelection->get_filename().data().c_str();
>
>There is no builtin cast to (char *) for the string class.
>
>--p
>
>--
>To unsubscribe: mail -s unsubscribe gtk-list-request@redhat.com < /dev/null
BEGIN:VCARD
VERSION:2.1
N:Thompson;Eugene;S.
FN:Eugene S. Thompson
ORG:Point Information Network, Corp.;WaveLink Studio
TITLE:Senior Software Engineer
TEL;WORK;VOICE:425.823.0111
TEL;WORK;FAX:425.823.0143
ADR;WORK;ENCODING=QUOTED-PRINTABLE:;;11335 NE 122nd Way=0D=0ASuite 115;Kirkland;WA;98034;USA
LABEL;WORK;ENCODING=QUOTED-PRINTABLE:11335 NE 122nd Way=0D=0ASuite 115=0D=0AKirkland, WA 98034=0D=0AUSA
URL:
URL:http://www.wavelink.com
EMAIL;PREF;INTERNET:gthompson@pin-corp.com
REV:19990628T163639Z
END:VCARD


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]