DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   string vs. variable (http://www.daniweb.com/forums/thread156461.html)

clutchkiller Nov 10th, 2008 5:00 pm
string vs. variable
 
What would be the difference between something like

int func1 = "This is my function"

and

string func = "This is my function" (sorry if syntax is off)

The only thing ive been able to find in research, is that c++ offers a specific header to handle string i/o etc...

thanks

Narue Nov 10th, 2008 5:03 pm
Re: string vs. variable
 
int is a type built right into the language, which means you don't need to link with a library to use it. string (or std::string, to be specific) is a class in the standard library that gives you a string type. Ultimately the difference is minimal, aside from the fact that the two types are used completely differently, of course. ;) But to use std::string, you need to include <string>.

>int func1 = "This is my function"
This is a syntax error, by the way.

clutchkiller Nov 10th, 2008 5:19 pm
Re: string vs. variable
 
lol, ok. Syntax aside =P, you say that the difference is minimal, but the two are usually used in two completly different ways. That confuses me =(. Is the main significance with using the string class the i/o functionality then? And could you possibly throw me some examples different situations when a variable is used and when a string is used?

Thanks again
-the noob

Narue Nov 10th, 2008 5:30 pm
Re: string vs. variable
 
>That confuses me =(
Get used to it. Good programmers are in a constant state of confusion. What I mean is that std::string can be used as a first-class type just like int. However, because they're different types, they have different usage patterns.

>Is the main significance with using the string class the i/o functionality then?
Nope, the significance of using the string class is being able to use strings. :icon_rolleyes: The significance of int is that you can do integer math, which is somewhat more difficult using strings. I/O works with both, fortunately.

clutchkiller Nov 10th, 2008 5:38 pm
Re: string vs. variable
 
>That confuses me =(
Get used to it. Good programmers are in a constant state of confusion.

My god, i will live by these words...

Looks like i have some quesi specifics to investigate! Up Up And AWAY!


All times are GMT -4. The time now is 12:06 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC