FLTK logo

Re: [fltk.general] Why do I need to convert a string to a string for Fl_Input

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Library      Forums      Links      Apps     Login 
 All Forums  |  Back to fltk.general  ]
 
Previous Message ]New Message | Reply ]Next Message ]

Re: Why do I need to convert a string to a string for Fl_Input Mo_Al_ Oct 06, 2022  
  win2_unitprice->value(to_string(z).c_str());

should work

On Friday, October 7, 2022 at 12:29:13 AM UTC+3 rogertunnicl...@gmail.com wrote:
Thx for the replies everybody. No sure I really "get it" but at least I know how to make it work. (s2 existed so I could look at the values in GDB)

Having said that, how should I code this so iy happens in one step?

string s1 = to_string(z);
win2_unitprice->value(s1.c_str());

On Friday, October 7, 2022 at 6:33:21 AM UTC+11 pvr...@btinternet.com wrote:

From: roger tunnicliffe
Sent: 05 October 2022 23:40
To: fltk.general
Subject: [fltk.general] Why do I need to convert a string to a string for Fl_Input

 

I have the following definitions:-

 

 Fl_Input            *win2_unitprice;
  win2_unitprice        = new Fl_Input(x+50,y+200,100,20,"Unit Price..:");

and the following code:-

double z;

.

.
  string s1 = to_string(z);
  string s2 = s1.c_str();
// win2_unitprice->value(s1);
    win2_unitprice->value(s1.c_str());

If I uncomment line 3 I get the following error:-
error: no matching function for call to ‘Fl_Input::value(std::__cxx11::string&)’
   win2_unitprice->value(s1);

but if i look at the data I see that:-

s1 holds the address of the data 0x31 0x2e 0x31 0x30 ....

and 

s2 holds the address of the data 0x31 0x2e 0x31 0x30 ....

 

so why do I need to use c_str() to make this work ??


<START>

Hi Roger,

 

The short answer is the difference between strings in C and C++ languages. A C-string has type char* - that is a pointer to a series of 8-bit characters in memory, ended with the NUL (x00) character. A C++-string (type string) is a class with some overheads to manage access to it and memory allocation.

 

FLTK generally uses char* to pass string data around.

 

The class string method string::c_str() accesses the raw char* string within the class and this is used to pass the string content to FLTK widgets.

 

The statement

string s2 = s1.c_str()

uses a feature of C++ that overloads the assignment operator (=) so that you can create a new C++ string from the C string.

 

Regards Phil.

--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkgeneral/b8094d8d-65e4-4f3c-8beb-c84834a92239n%40googlegroups.com.
Direct Link to Message ]
 
     
Previous Message ]New Message | Reply ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2025 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.