Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question about strings

Reply
Thread Tools

question about strings

 
 
wongjoekmeu@yahoo.com
Guest
Posts: n/a
 
      01-28-2005
Hello All,
I have the following C++ code which I do not understand. I have a class
Employee and a class String. Employee is using data members of type
String.

The definition of the overloaded constructor of Employee is as follow:

-------------
Employee::Employee(char *firstname, char *lastname, char *address, long
salary):
itsFirstname(firstname),
itsLastname(lastname),
itsAddress(address),
itsSalary(salary)
{}
-------------

This constructor requires three pointers of type char as input
parameter and one long int.
So I would suppose that when an object of type Employee is being
created that in the input parameters (the first three) should be
adresses of objects of type String. But now, to my surprise in the
main() function it is written:
-------------
Employee Edie("Jane","Doe","1461 Shore Parkway", 20000);
-------------
Why is this correct ? The first three input parameters are character
strings. How is that possible that this program compiles ?? Somewhere
in the comments of the listing it says that the class String know how
to convert a character string to a String. But if for instance this is
valid and "Jane" is being converted to a String object, but then it is
still not a pointer. Can anyone explain this to me.
Thank you in advance.

Robert

 
Reply With Quote
 
 
 
 
ulrich
Guest
Posts: n/a
 
      01-28-2005
On 28 Jan 2005 00:32:18 -0800,
<> wrote:

[...]

> Employee and a class String. Employee is using data members of type
> String.


[...]

> Employee::Employee(char *firstname, char *lastname, char *address, long
> salary):
> itsFirstname(firstname),
> itsLastname(lastname),
> itsAddress(address),
> itsSalary(salary)
> {}


[...]

> Employee Edie("Jane","Doe","1461 Shore Parkway", 20000);
> -------------
> Why is this correct ? The first three input parameters are character
> strings. How is that possible that this program compiles ?? Somewhere
> in the comments of the listing it says that the class String know how
> to convert a character string to a String. But if for instance this is
> valid and "Jane" is being converted to a String object, but then it is
> still not a pointer. Can anyone explain this to me.


the string object takes a _copy_ of what your char* is pointing at. that's
the whole magic.

--
have a nice day
ulrich
 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      01-28-2005
<> wrote in message
news: ups.com...
> Hello All,

Hi,
> I have the following C++ code which I do not understand. I have a class
> Employee and a class String. Employee is using data members of type
> String.

I'll assume you mean std::string ?

> The definition of the overloaded constructor of Employee is as follow:
>
> -------------
> Employee::Employee(char *firstname, char *lastname, char *address, long
> salary):
> itsFirstname(firstname),
> itsLastname(lastname),
> itsAddress(address),
> itsSalary(salary)
> {}
> -------------
>
> This constructor requires three pointers of type char as input
> parameter and one long int.
> So I would suppose that when an object of type Employee is being
> created that in the input parameters (the first three) should be
> adresses of objects of type String. But now, to my surprise in the
> main() function it is written:
> -------------
> Employee Edie("Jane","Doe","1461 Shore Parkway", 20000);
> -------------
> Why is this correct ?

std::string (and other string classes) typically have a constructor
declared as: std::string::string( char const* p );
This constructor copies the null-terminates string pointed to by p
to initialize its contents.

> The first three input parameters are character
> strings. How is that possible that this program compiles ?? Somewhere
> in the comments of the listing it says that the class String know how
> to convert a character string to a String. But if for instance this is
> valid and "Jane" is being converted to a String object, but then it is
> still not a pointer. Can anyone explain this to me.

The data is *copied* by the string constructor into its own storage.

Actually, there a different problem with the Employee constructor
it describes: its first 3 parameters should not be of type char*,
but of either char const* or std::string const&.
The declaration:
Employee Edie("Jane","Doe","1461 Shore Parkway", 20000);
passes string literals ( the "..." stuff ) which normally has type
char const* ( actually char const[] ). A deprecated conversion
to char* is however available, only for backwards-compatibility
with C.


I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
How to generate k+1 length strings from a list of k length strings? Girish Sahani Python 17 06-09-2006 11:01 AM
Catching std::strings and c-style strings at once Kurt Krueckeberg C++ 2 11-17-2004 03:53 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57