我可以在数据结构中创建构造函数吗? [英] Can i create a constructor in a data structure?

查看:118
本文介绍了我可以在数据结构中创建构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在数据结构中创建构造函数吗?请执行以下

代码对吗?


示例代码

#include< iostream.h>

struct USERID {

char UserName [4];

char密码[7];

char LoginName [21];

char LoginPassword [11];


USERID(){

UserName [0] =''\ 0'';

密码[0] =''\''';

LoginName [0] =''\ 0'';

LoginPassword [ 0] =''\''';}};

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

推荐答案

fu ***** @ gmail.com 写道:

我可以在数据结构中创建构造函数吗?请执行以下

代码对吗?


示例代码

#include< iostream.h>

struct USERID {

char UserName [4];

char密码[7];

char LoginName [21];

char LoginPassword [11];


USERID(){

UserName [0] =''\ 0'';

密码[0] =''\''';

LoginName [0] =''\ 0'';

LoginPassword [ 0] =''\ 0'';}};
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'';}};



这是合法的,但为什么不使用std :: string并避免凌乱的初始化?


-

Ian Collins。

It''s legal, but why not use std::string and avoid the messy initialisations?

--
Ian Collins.


fu ***** @ gmail.com 写道:

我可以在数据结构中创建构造函数?请执行以下

代码对吗?


示例代码

#include< iostream.h>

struct USERID {

char UserName [4];

char密码[7];

char LoginName [21];

char LoginPassword [11];


USERID(){

UserName [0] =''\ 0'';

密码[0] =''\''';

LoginName [0] =''\ 0'';

LoginPassword [ 0] =''\ 0'';}};
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'';}};



当然这是对的。请记住,在C ++下,一个struct基本上被视为一个类,除了你不能使用访问

修饰符,如''public:'',''protected: ''或''private:'',因为结构的所有

成员都是公开的。你可以添加其他方法,甚至是虚拟的



问候,

Stuart

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

< br>

* fu*****@gmail.com

我可以在数据结构中创建构造函数吗?请执行以下

代码对吗?


示例代码

#include< iostream.h>

struct USERID {

char UserName [4];

char密码[7];

char LoginName [21];

char LoginPassword [11];


USERID(){

UserName [0] =''\ 0'';

密码[0] =''\''';

LoginName [0] =''\ 0'';

LoginPassword [ 0] =''\''';}};
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'';}};



其他人评论了你的构造函数:它没关系。


但是,你的代码不会编译一些更新的编译器除非你

改变


#include< iostream.h>


to


#include< iostream>


或自己定义一个< iostream.hheader - 它不是标准的。


其次,它通常是一个好主意(TM)来保留宏的全部大写。


第三,但这已被评论,你'通过使用std :: string而不是原始字符数组,将节省大量工作

和痛苦。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?

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?


这篇关于我可以在数据结构中创建构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆