473,396 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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';}};

Jul 26 '06 #1
16 2003
fu*****@gmail.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';}};
It's legal, but why not use std::string and avoid the messy initialisations?

--
Ian Collins.
Jul 26 '06 #2
fu*****@gmail.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. You may add other methods, even
virtual ones.

Regards,
Stuart
Jul 26 '06 #3
* fu*****@gmail.com:
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.hheader 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?
Jul 26 '06 #4
Stuart Redmann wrote:
fu*****@gmail.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.

You may add other methods, even virtual ones.
That is correct.
Best

Kai-Uwe Bux

Jul 26 '06 #5

Ian Collins wrote:
fu*****@gmail.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';}};
It's legal, but why not use std::string and avoid the messy initialisations?
Because that would make it no longer a POD? Of course, in that case you
want to derive from struct USERID and put the ctor in the derived
class.
You don't have to change the original struct for that, which is another
advantage. (Of course, for new software this is irrelevant)

HTH,
Michiel Salters

Jul 26 '06 #6
In article <11**********************@i42g2000cwa.googlegroups .com>,
Mi*************@tomtom.com says...

[ ... ]
struct USERID{
[ ... ]
USERID( ){
[ ... ]
It's legal, but why not use std::string and avoid the messy initialisations?

Because that would make it no longer a POD?
"A POD-struct is an aggregate class..." (9/4), and "An aggregate is
an array or class with no user-defined constructors..." (8.5.1). As
it stands right now, it's not a POD (though from your comments below,
perhaps you already knew that...)
Of course, in that case you
want to derive from struct USERID and put the ctor in the derived
class.
That would be better avoided, IMO. One of the strengths of C++ is the
ability to ensure against creating uninitialized objects, but this
removes any such assurance. At least in my mind, the first question
would be whether there's really any reason for this to be a POD to
start with. If there is, the second question would be how much work
it'll take to fix that.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 26 '06 #7
Geo
Second, it's generally a Good Idea(TM) to reserve all uppercase for macros.
That implies that it is a Good Idea(TM) to have macros in the first
place, why reserve an entire class of identifiers for something you
should never need ?

Jul 26 '06 #8
Geo wrote:
>
>Second, it's generally a Good Idea(TM) to reserve all uppercase for
macros.

That implies that it is a Good Idea(TM) to have macros in the first
place, why reserve an entire class of identifiers for something you
should never need ?
Because you do need macros (whether you should or not).
Best

Kai-Uwe Bux
Jul 26 '06 #9
Geo

Kai-Uwe Bux wrote:
Geo wrote:
Second, it's generally a Good Idea(TM) to reserve all uppercase for
macros.
That implies that it is a Good Idea(TM) to have macros in the first
place, why reserve an entire class of identifiers for something you
should never need ?

Because you do need macros (whether you should or not).
Best

Kai-Uwe Bux
Do I ?

The only explicit #defines I have in my code are include guards and I
don't consider them macros, what do I NEED macros for ?

Jul 27 '06 #10
Geo wrote:
>
Kai-Uwe Bux wrote:
>Geo wrote:
>
Second, it's generally a Good Idea(TM) to reserve all uppercase for
macros.
That implies that it is a Good Idea(TM) to have macros in the first
place, why reserve an entire class of identifiers for something you
should never need ?

Because you do need macros (whether you should or not).
Best

Kai-Uwe Bux

Do I ?
Clearly. See your next statement:
The only explicit #defines I have in my code are include guards
There you are.

and I don't consider them macros,
Whether you consider them macros or not is utterly irrelevant. They *are*
macros.

what do I NEED macros for ?
Try to write a simple logging facility that only is active in debug mode and
registers file and line number with each log entry automatically, so that
you could do in your code

RLOG << some_data; // this is in the file CTime.cpp on line 242

and in the log-file, it would read, say:

[CTime.cpp: 242] 120978

In this case, I would be very surprised if you could do that without macros.
Best

Kai-Uwe Bux
Jul 27 '06 #11
Kai-Uwe Bux wrote:
>
Try to write a simple logging facility that only is active in debug mode and
registers file and line number with each log entry automatically, so that
you could do in your code

RLOG << some_data; // this is in the file CTime.cpp on line 242
I do that in my code without using macros, except that it looks like:

log() << "some" << data;

Jul 28 '06 #12
Old Wolf wrote:
Kai-Uwe Bux wrote:
>>
Try to write a simple logging facility that only is active in debug mode
and registers file and line number with each log entry automatically, so
that you could do in your code

RLOG << some_data; // this is in the file CTime.cpp on line 242

I do that in my code without using macros, except that it looks like:

log() << "some" << data;
I take it that log() creates a temporary. Now, how do you get the file and
line number in there? If you do

class log {

std::string file;
std::string line;

public:

log ( void )
: file ( __FILE__ )
, line ( __LINE__ )
{}

// more

};

then you will end up doing the file and line number of the definition of the
log constructor instead of the file and line number of the logging line

log() << "some" << data; // this file and line number shall be recorded
I would love to see how your log class does that.
Best

Kai-Uwe Bux

Jul 28 '06 #13
Kai-Uwe Bux wrote:
Old Wolf wrote:
>Kai-Uwe Bux wrote:
>>>
Try to write a simple logging facility that only is active in debug
mode and registers file and line number with each log entry
automatically, so that you could do in your code

RLOG << some_data; // this is in the file CTime.cpp on line 242

I do that in my code without using macros, except that it looks like:

log() << "some" << data;

I take it that log() creates a temporary. Now, how do you get the
file and line number in there? If you do

class log {

std::string file;
std::string line;

public:

log ( void )
: file ( __FILE__ )
, line ( __LINE__ )
{}

// more

};

then you will end up doing the file and line number of the definition
of the log constructor instead of the file and line number of the
logging line

log() << "some" << data; // this file and line number shall be
recorded
I would love to see how your log class does that.
It probably doesn't. It doesn't have to. Not everything has to be
logged with __FILE__ or __LINE__ involved. And 'log()' could be a simple
function that returns a reference [to a static object inside it].

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 28 '06 #14
Victor Bazarov wrote:
Kai-Uwe Bux wrote:
>Old Wolf wrote:
>>Kai-Uwe Bux wrote:

Try to write a simple logging facility that only is active in debug
mode and registers file and line number with each log entry
automatically, so that you could do in your code

RLOG << some_data; // this is in the file CTime.cpp on line 242
Note the following claim:
>>I do that in my code without using macros, except that it looks like:

log() << "some" << data;
Old Wolf explicitly claims to do *that* (refering to the logging
requirements laid out in the paragraph before, which include recording of
file and line number information).
>I take it that log() creates a temporary. Now, how do you get the
file and line number in there? If you do

class log {

std::string file;
std::string line;

public:

log ( void )
: file ( __FILE__ )
, line ( __LINE__ )
{}

// more

};

then you will end up doing the file and line number of the definition
of the log constructor instead of the file and line number of the
logging line

log() << "some" << data; // this file and line number shall be
recorded
I would love to see how your log class does that.

It probably doesn't.
Then, Old Wolfs claim that his class does *that* is false.

It doesn't have to. Not everything has to be logged with __FILE__ or
__LINE__ involved.
True, but irrelevant for the point under discussion (unless you want to
change the topic from "what can you do with macros that is hard or
impossible to do without" to "how should a logging facility be designed").

And 'log()' could be a simple function that returns a reference [to a
static object inside it].
True. And how would that allow for file and line logging without macros?
Best

Kai-Uwe Bux
Jul 28 '06 #15
Kai-Uwe Bux wrote:
Old Wolf wrote:
Kai-Uwe Bux wrote:
>
Try to write a simple logging facility that only is active in debug mode
and registers file and line number with each log entry automatically, so
that you could do in your code

RLOG << some_data; // this is in the file CTime.cpp on line 242
I do that in my code without using macros, except that it looks like:

log() << "some" << data;

I take it that log() creates a temporary. Now, how do you get the file and
line number in there? If you do
I don't (file/line logging is yucky IMHO). Anyway, this can't be done
easily without using __FILE__ and __LINE__ , which are macros.
So it is a bit strange to distinguish between solutions that use
macros and solutions that don't.

Jul 28 '06 #16
Old Wolf wrote:
Kai-Uwe Bux wrote:
>Old Wolf wrote:
Kai-Uwe Bux wrote:

Try to write a simple logging facility that only is active in debug
mode and registers file and line number with each log entry
automatically, so that you could do in your code

RLOG << some_data; // this is in the file CTime.cpp on line 242

I do that in my code without using macros, except that it looks like:

log() << "some" << data;

I take it that log() creates a temporary. Now, how do you get the file
and line number in there? If you do

I don't (file/line logging is yucky IMHO). Anyway, this can't be done
easily without using __FILE__ and __LINE__ , which are macros.
Actually, my point is a little less trivial. It is not just that you cannot
avoid __FILE__ and __LINE__, but that I do not see a way to avoid defining
your *own* macros to do this. E.g., something like

#define RLOG ( log() << __FILE__ << " [" << __LINE__ << "]: " )

would do it. If you see a way of doing away with the #define, I would be
surprised.

So it is a bit strange to distinguish between solutions that use
macros and solutions that don't.
The distinction is more about solutions where you use #define and solutions
where you don't. Please reread the discussion up-thread. The dispute
started from the claim "I only use #define for include guards" or some such
thing.
Best

Kai-Uwe Bux
Jul 28 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jim Langston | last post by:
What I want to do: class MyClass { public: struct { bool Enable; bool Velvety; float Density;
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
5
by: Lance | last post by:
I need to create a Drawing.Bitmap from an array of integer values. My current technique creates a bitmap that eventually becomes corrupt (i.e., the bitmap's pixels change to a different color...
3
by: Steve | last post by:
Hello, I created a public Structure in a Standard Module and also an array of Structures. Then I load data into the array of structures in a public sub that I call on the Form load event. ...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
5
by: sarathy | last post by:
Hi, I need to see the output of the program when no copy constructor is used. Since by default, there is an implicit one, how should i perform the override, so as to totally remove the copy...
27
by: max | last post by:
Hello, I am a newbye, and I'm trying to write a simple application. I have five tables with three columns; all tables are identical; I need to change some data in the first table and let VB...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
5
by: Sergio Montero | last post by:
I have a MustInherits Base class that implements a custom IDataLayer interfase. IDataLayer expose CRUD methods. Base class constructor requires two parameters: ConnectionString TableName ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.