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 

Can i create a constructor in a data structure?

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






PostPosted: Wed Jul 26, 2006 9:10 am    Post subject: Can i create a constructor in a data structure? Reply with 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';}};
Back to top
Stuart Redmann
Guest





PostPosted: Wed Jul 26, 2006 9:10 am    Post subject: Re: Can i create a constructor in a data structure? Reply with quote



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





PostPosted: Wed Jul 26, 2006 9:10 am    Post subject: Re: Can i create a constructor in a data structure? Reply with quote



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





PostPosted: Wed Jul 26, 2006 9:10 am    Post subject: Re: Can i create a constructor in a data structure? Reply with quote

* 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





PostPosted: Wed Jul 26, 2006 9:11 am    Post subject: Re: Can i create a constructor in a data structure? Reply with quote

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