Topic: Can i create a constructor in a data structure?


Author: fuiwong@gmail.com
Date: Wed, 26 Jul 2006 09:25:00 CST
Raw View
Can we create a constructor in a structure? does the following right?


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

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Greg Herlihy" <greghe@pacbell.net>
Date: Wed, 26 Jul 2006 10:47:39 CST
Raw View
fuiwong@gmail.com wrote:
> Can we create a constructor in a structure? does the following right?
>
>
> #include <iostream.h>

iostream.h is obsolete. Use <iostream> instead. In fact none of the C++
(only) header files end in .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';}};

The declaration is a legal declaration of a struct named USERID with a
default constructor that initializes the first element of its several
character array data members to 0.

Constructible classes is just one feature of C++, there are of course
many more. So I would suggest considering a few changes to this
declaration to take advantage of some of those additional capabilities.
But first, a stylistic note: generally all caps is reserved for macros.
So I would suggest changing USERID to "User", "UserAccount" - that is,
a mixed case (or lower-case) name that would better conform to naming
conventions.

The first suggested change is to eliminate "magic numbers" (numeric
literals that appear directly in code - seemingly out of nowhere).
Instead, favor static constants (in this case, declared within the
struct):

     struct User
     {
          static const size_t kUserNameMaxChars = 4;

          char UserName[ kUserNameMaxChars ];
           ...

By defining a static integral constant, any code that checks against
UserName character array overruns is assured of checking against
UserName's actual size. Preventing overruns in the first place is an
even better approach, so I would consider replacing the character
arrays completely. Character arrays are inflexible, potentially
wasteful and prone to overrun (undefined behavior caused whenever more
data is stored in an arrray than it can hold). Replacing the character
array with an object such as std::string address these shortcomings:

    #include <string>

    struct User
    {
        std::string UserName;
        ...

Often a program needs to "validate" a supplied value before it decides
to accept it. For instance, even were UserName stored as a std::string,
a program may still need to enforce a limit on its length. With public
access to a data member, the User struct cannot easily validate the
UserName data member whenever its value may be changed. Therefore, in
order to allow only controlled changes to UserName, the struct can
declare the UserName data member private and provide public accessor
methods to get and set its value:

     struct User
     {
     private:
          std::string UserName;
          ...
     public:
          std::string GetUserName() const;
          void SetUserName(const std::string& newName);
          ...

In this example,  the SetUserName() method is able to validate the
proposed updated name before deciding whether to assign the new value
to its UserName data member.

These suggestions are by no means exhaustive. Rather they are to meant
to illustrate that as long as a program takes any advantage of C++, it
may as well take full advantage of C++ in any way practical.

Greg

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]