初始化而不分配 [英] Initializing without assigning

查看:71
本文介绍了初始化而不分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自写作主要是用Java编写的,我很难理解如何在没有初始化的情况下声明一个成员,并且稍后再这样做......在Java中,

我会写类似

public static void main(String [] args){

MyType aMember;

...

aMember = new MyType(...)

...

}

然而,在C ++中,这似乎不起作用。我在课堂上宣布(这是串口通信的
),如下所示:


#include< fstream>

#include< boost / thread / thread.hpp>

class COMConn {

public:

COMConn(int port );

~COMConn();


char * send(char);

char * send(char,char [ ]);

私人:

void connect(int);


std :: ifstream输入;

std :: ofstream输出;


boost :: thread inputthread;

boost :: thread outputthread;

} ;


但是当我想初始化线程和流时,我想做

类似


void COMConn :: connect(int port){

this-> input = std :: ifstream(" COM1:");

this-> inputthread = boost :: thread(& input.read); //不正确,需要

参数,但暂时跳过这个

//等等其他人

}


如何在C ++中执行此操作?我发现我可以做

this-> input.open(" COM1:");

但这不是一般的工作原理。我发现这个

非常混乱我必须说...谢谢你的任何建议

Coming from writing mostly in Java, I have trouble understanding how to
declare a member without initializing it, and do that later... In Java,
I would write something like
public static void main(String[] args) {
MyType aMember;
...
aMember = new MyType(...)
...
}
However, in C++ this does not seem to work. I declare in class (it''s
for serial port communication) like this:

#include <fstream>
#include <boost/thread/thread.hpp>

class COMConn {
public:
COMConn(int port);
~COMConn();

char *send(char);
char *send(char, char[]);
private:
void connect(int);

std::ifstream input;
std::ofstream output;

boost::thread inputthread;
boost::thread outputthread;
};

But when I want to initialize the threads and streams, I want to do
something like

void COMConn::connect(int port) {
this->input = std::ifstream("COM1:");
this->inputthread = boost::thread(&input.read); // Not correct, needs
arguments, but skip that for now
//etc for the others
}

How do you go about doing this in C++? I found that I could do
this->input.open("COM1:");
But that is hardly something that will work in general. I find this
very confusing I must say... Thanks you for any advice

推荐答案

<
Calle Pettersson写道:

Calle Pettersson wrote:

来自写作主要是用Java编写,我无法理解如何使用
声明一个成员没有初始化,并在以后这样做...
Coming from writing mostly in Java, I have trouble understanding how to
declare a member without initializing it, and do that later...



一般规则:不要。只需将声明移动到你初始化它的位置,因为任何对象在初始化之前都是不可用的。


你不必先声明变量;这是一个古老的C习惯

不是

甚至在今天的C语言中都需要,更不用说新语言了。


HTH

Michiel Salters

General rule: don''t. Just move the declaration to the point where you
initialize it, as any object is unusable until initialized.

You don''t have to declare variables first; that''s a old C habit which
is not
even needed in today''s C, let alone in newer languages.

HTH
Michiel Salters



Mi ************* @ tomtom.com 写道:

Calle Pettersson写道:
Calle Pettersson wrote:

来自写作主要是用Java编写,我无法理解如何在没有初始化的情况下声明成员
它,以后再做...
Coming from writing mostly in Java, I have trouble understanding how to
declare a member without initializing it, and do that later...



一般规则:不要。只需将声明移动到你初始化它的位置,因为任何对象在初始化之前都是不可用的。


你不必先声明变量;这是一个古老的C习惯

不是

甚至在今天的C语言中都需要,更不用说新语言了。


HTH

Michiel Salters


General rule: don''t. Just move the declaration to the point where you
initialize it, as any object is unusable until initialized.

You don''t have to declare variables first; that''s a old C habit which
is not
even needed in today''s C, let alone in newer languages.

HTH
Michiel Salters



真的吗?但是如果我需要这个可以访问类,但是不能
在类定义中调用构造函数? (在这种情况下需要知道哪个com

端口)

另外,我不知道如果变量可以添加到类中,如果它们是
$标题中没有指定b $ b吗?

或者我误解了你?

Really? But if I need this to be accessible to the class, but can''t
call the constructor in the class definition? (Need to know which com
port in this case)
Also, I didn''t know that variables could be added to a class if they
aren''t specified in the header?
Or am I misunderstanding you?


Calle Pettersson写道:
Calle Pettersson wrote:
Mi *********** **@tomtom.com 写道:

> Calle Pettersson写道:
>Calle Pettersson wrote:

>>来自写作主要是用Java编写的,我无法理解
如何在不初始化的情况下声明成员,并且稍后执行此操作
稍​​后...
>>Coming from writing mostly in Java, I have trouble understanding
how to declare a member without initializing it, and do that
later...


一般规则:不要。只需将声明移动到初始化它的位置,因为任何对象在初始化之前都是不可用的。

您不必先声明变量;这是一种古老的C习惯,在今天的C语言中甚至不需要,更不用说新语言了。

HTH
Michiel Salters


General rule: don''t. Just move the declaration to the point where you
initialize it, as any object is unusable until initialized.

You don''t have to declare variables first; that''s a old C habit which
is not
even needed in today''s C, let alone in newer languages.

HTH
Michiel Salters



真的吗?但是如果我需要这个可以访问类,但是不能
在类定义中调用构造函数? (在这种情况下需要知道哪个com

端口)

另外,我不知道如果变量可以添加到类中,如果它们是
$标题中没有指定b $ b吗?

或者我误解了你?

Really? But if I need this to be accessible to the class, but can''t
call the constructor in the class definition? (Need to know which com
port in this case)
Also, I didn''t know that variables could be added to a class if they
aren''t specified in the header?
Or am I misunderstanding you?



看来你们彼此误解了。


*会员*在类定义中首先被声明。然后,在构造函数的

初始化列表中,初始化它们。 *变量*

(非会员,独立)不需要申报。声明/定义/

在需要时初始化它们。


class Class {

void * member; //成员:声明

public:

Class():member(0){} //初始化

};


int main(){

void * nonmember(0); //变量:declaration / definition / init''n

}

V

-

请在通过电子邮件回复时删除资本''A'

我没有回复最热门的回复,请不要问

It seems you''re misunderstanding each other.

*Members* are declared first, in the class definition. Then, in the
initialiser list of a constructor, they are initialised. *Variables*
(non-member, stand-alone) don''t need to be declared. Declare/define/
initialise them when needed.

class Class {
void *member; // a member: declaration
public:
Class() : member(0) {} // initialisation
};

int main() {
void *nonmember(0); // a variable: declaration/definition/init''n
}
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于初始化而不分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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