 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Wed Jul 26, 2006 9:10 am Post subject: Can i create a constructor in a data structure? |
|
|
Can i create a constructor in a data structure? does the following
codes right?
sample code
#include <iostream.h>
struct USERID{
char UserName[4];
char Password[7];
char LoginName[21];
char LoginPassword[11];
USERID( ){
UserName[0]= '\0';
Password[0]= '\0';
LoginName[0]= '\0';
LoginPassword[0]= '\0';}}; |
|
| Back to top |
|
 |
Stuart Redmann Guest
|
Posted: Wed Jul 26, 2006 9:10 am Post subject: Re: Can i create a constructor in a data structure? |
|
|
fuiwong (AT) gmail (DOT) com wrote:
| Quote: | Can i create a constructor in a data structure? does the following
codes right?
sample code
#include <iostream.h
struct USERID{
char UserName[4];
char Password[7];
char LoginName[21];
char LoginPassword[11];
USERID( ){
UserName[0]= '\0';
Password[0]= '\0';
LoginName[0]= '\0';
LoginPassword[0]= '\0';}};
|
Of course this is right. Keep in mind that under C++ a struct is
basically treated as a class except that you cannot use the access
modifiers like 'public:', 'protected:', or 'private:', because all
members of the struct are public. You may add other methods, even
virtual ones.
Regards,
Stuart |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Wed Jul 26, 2006 9:10 am Post subject: Re: Can i create a constructor in a data structure? |
|
|
fuiwong (AT) gmail (DOT) com wrote:
| Quote: | Can i create a constructor in a data structure? does the following
codes right?
sample code
#include <iostream.h
struct USERID{
char UserName[4];
char Password[7];
char LoginName[21];
char LoginPassword[11];
USERID( ){
UserName[0]= '\0';
Password[0]= '\0';
LoginName[0]= '\0';
LoginPassword[0]= '\0';}};
It's legal, but why not use std::string and avoid the messy initialisations? |
--
Ian Collins. |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Wed Jul 26, 2006 9:10 am Post subject: Re: Can i create a constructor in a data structure? |
|
|
* fuiwong (AT) gmail (DOT) com:
| Quote: | Can i create a constructor in a data structure? does the following
codes right?
sample code
#include <iostream.h
struct USERID{
char UserName[4];
char Password[7];
char LoginName[21];
char LoginPassword[11];
USERID( ){
UserName[0]= '\0';
Password[0]= '\0';
LoginName[0]= '\0';
LoginPassword[0]= '\0';}};
|
Others have commented on your constructor: it's OK.
However, your code will not compile with some newer compilers unless you
change
#include <iostream.h>
to
#include <iostream>
or define an <iostream.h> header yourself -- it's not standard.
Second, it's generally a Good Idea(TM) to reserve all uppercase for macros.
Third, but this has already been remarked on, you'll save a lot of work
and anguish by using std::string instead of raw character arrays.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? |
|
| Back to top |
|
 |
Kai-Uwe Bux Guest
|
Posted: Wed Jul 26, 2006 9:11 am Post subject: Re: Can i create a constructor in a data structure? |
|
|
Stuart Redmann wrote:
| Quote: | fuiwong (AT) gmail (DOT) com wrote:
Can i create a constructor in a data structure? does the following
codes right?
sample code
#include <iostream.h
struct USERID{
char UserName[4];
char Password[7];
char LoginName[21];
char LoginPassword[11];
USERID( ){
UserName[0]= '\0';
Password[0]= '\0';
LoginName[0]= '\0';
LoginPassword[0]= '\0';}};
Of course this is right. Keep in mind that under C++ a struct is
basically treated as a class except that you cannot use the access
modifiers like 'public:', 'protected:', or 'private:', because all
members of the struct are public.
|
This is incorrect. All access specifiers are at your disposal. What differs
is the default: in struct, members are public by default.
| Quote: | You may add other methods, even virtual ones.
|
That is correct.
Best
Kai-Uwe Bux |
|
| Back to top |
|
 |
|
|
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
|
|