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::Emplo yee(char *firstname, char *lastname, char *address, long
salary):
itsFirstname(fi rstname),
itsLastname(las tname),
itsAddress(addr ess),
itsSalary(salar y)
{}
-------------
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","Do e","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 2 1049
On 28 Jan 2005 00:32:18 -0800, wo*********@yah oo.com
<wo*********@ya hoo.com> wrote:
[...] Employee and a class String. Employee is using data members of type String.
[...]
Employee::Emplo yee(char *firstname, char *lastname, char *address, long salary): itsFirstname(fi rstname), itsLastname(las tname), itsAddress(addr ess), itsSalary(salar y) {}
[...]
Employee Edie("Jane","Do e","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
<wo*********@ya hoo.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.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::Emplo yee(char *firstname, char *lastname, char *address, long salary): itsFirstname(fi rstname), itsLastname(las tname), itsAddress(addr ess), itsSalary(salar y) {} -------------
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","Do e","1461 Shore Parkway", 20000); ------------- Why is this correct ?
std::string (and other string classes) typically have a constructor
declared as: std::string::st ring( 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","Do e","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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Carlos Ribeiro |
last post by:
As a side track of my latest investigations, I began to rely heavily
on generators for some stuff where I would previsouly use a more
conventional approach. Whenever I need to process a list, I'm tending
towards the use of generators. One good example is if I want to print
a report, or to work over a list with complex processing for each
item. In both cases, a simple list comprehension can't be used. The
conventional approach involves...
|
by: Rafi Kfir |
last post by:
Hi,
This may look as a smiple task to most of you, but to me (a beginner
with C), it drives me crazy.
All I want is that one function passes a two dimensional array of
strings to another function.
example:
NOTE: the strings can be of different length and the array will be
|
by: Alfonso Morra |
last post by:
Hi,
I am having some probs with copying memory blocks around (part of a
messaging library) and I would like some confirmation to make sure that
I'm going about things the right way.
I have some data types defined thus:
typedef enum {
ONE ,
|
by: glenn |
last post by:
I am use to programming in php and the way session and post vars are past
from fields on one page through to the post page automatically where I can
get to their values easily to write to a database or continue to process on
to the next page.
I am now trying to learn ASP to see if we can replace some of our
applications that were written in php with an ASP alternative. However,
after doing many searches on google and reading a couple...
|
by: M O J O |
last post by:
Hi,
A simple question...
This is just an example ... Say I have a class (MyClass) that has only
few variables (let's say 10 strings) but many-many-many subs and
fuctions, so many subs and functions that they take up 10k of memory.
The subs and fuctions creates no new variables.
Here's my question....
| |
by: Simon Windsor |
last post by:
Hi
Is there a standard postgres method of replacing empty strings.
In Oracle, nvl handles nulls and empty strings, as does ifnull() in
MySQL, but with postgres coalesce only handles null strings.
If, not is the best solution to create a plpgsql function, ie
CREATE FUNCTION isEmpty (character varying, character varying) RETURNS
|
by: Steven |
last post by:
Hello,
I have a question about strcmp(). I have four words, who need to be
compared if it were two strings. I tried adding the comparison values
like '(strcmp(w1, w2) + strcmp(w3, w4))', where w1 and w2 make up the
first string and, w3 and w4 make up the second string. I do not want
to allocate memory, then put the words together to create a string
only to facilitate strcmp() comparison.
My question; Does anyone know how to get the...
|
by: denis |
last post by:
Hi there,
I got a tough interview questions lately, and I would like to hear
your opinion:
An array of N chars is given
Write an efficient algorithm to find all the repeating substring with a
minimal size
|
by: happyvalley |
last post by:
I just wonder how to pass arguments to this function with a char**
void oldmain(int argv, char**argc)
{
........
}
void main(void)
{
int argv;
|
by: copx |
last post by:
Unforuntately, I know next to nothing about ASM and compiler construction,
and while I was aware of the syntactic differences between pointers and
arrays, I was not aware of this:
http://udrepper.livejournal.com/13851.html
(Yes, it is livejournal, but it is the journal of the glibc maintainer)
I am not sure I really understand this low-level difference between pointers
and arrays, and I do not have the time to learn ASM and compiler...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |