C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

question about strings

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
wongjoekmeu@yahoo.com
Guest





PostPosted: Fri Jan 28, 2005 8:32 am    Post subject: question about strings Reply with quote



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

Back to top
ulrich
Guest





PostPosted: Fri Jan 28, 2005 9:00 am    Post subject: Re: question about strings Reply with quote



On 28 Jan 2005 00:32:18 -0800, [email]wongjoekmeu (AT) yahoo (DOT) com[/email]
<wongjoekmeu (AT) yahoo (DOT) com> wrote:

[...]

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

[...]

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

[...]

Quote:
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

Back to top
Ivan Vecerina
Guest





PostPosted: Fri Jan 28, 2005 9:19 am    Post subject: Re: question about strings Reply with quote



<wongjoekmeu (AT) yahoo (DOT) com> wrote

Quote:
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 ?


Quote:
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.

Quote:
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



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.