关于构造函数的奇怪问题 [英] Strange problem about constructor

查看:73
本文介绍了关于构造函数的奇怪问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:


在我的代码中,我使用内联构造函数定义了一个类。但它没有工作。我将课程描述如下:


myclass {

public:

myclass(int a,int b){r1 =一个; r2 = b;}


受保护:

int r1;

int r2;

double z;

}


但构造函数不起作用。当我声明

类的对象时,构造函数无法正确初始化变量。


然后我修改代码如下:


myclass {

public:

myclass(int a,int b);


protected :

int r1;

int r2;

double z;

}

myclass :: myclass(int a,int b){

r1 = a;

r2 = b;

}


它有效。当我声明类的对象时,构造函数可以正确初始化变量。


有什么问题?


非常感谢。


John

Hi all:

In my code I define a class with inline constructor. But it does not
work. I describe the class as below:

myclass{
public:
myclass(int a, int b) { r1 = a; r2 = b;}

protected:
int r1;
int r2;
double z;
}

But the constructor does not work. When I declare an object of the
class, the constructor can not correctly initialize the variables.

Then I modify the code as below:

myclass{
public:
myclass(int a, int b);

protected:
int r1;
int r2;
double z;
}

myclass::myclass(int a, int b) {
r1 = a;
r2 = b;
}

It works. When I declare an object of the class, the constructor can
correctly initialize variables.

What is the problem?

Thanks a lot.

John

推荐答案

" John" <乔********* @ yahoo.com>在留言中写道

news:c3 ************************** @ posting.google.c om ...
"John" <jo*********@yahoo.com> wrote in message
news:c3**************************@posting.google.c om...
在我的代码中,我使用内联构造函数定义了一个类。但它没有工作。我将课程描述如下:

myclass {
public:
myclass(int a,int b){r1 = a; r2 = b;}

受保护:
int r1;
int r2;
双z;
}

但是构造函数不起作用。当我声明
类的对象时,构造函数无法正确初始化变量。
In my code I define a class with inline constructor. But it does not
work. I describe the class as below:

myclass{
public:
myclass(int a, int b) { r1 = a; r2 = b;}

protected:
int r1;
int r2;
double z;
}

But the constructor does not work. When I declare an object of the
class, the constructor can not correctly initialize the variables.




你怎么知道程序不起作用?你上面显示的代码是

显然不是实际的代码,因为它不会编译。那么你实际上做了什么,你怎么知道它的表现不正确?



How do you know the program isn''t working? The code you show above is
obviously not the actual code, because it won''t compile. So what did you
actually do, and how do you know that it''s not behaving correctly?


" Andrew Koenig" < ar*@acm.org>写道......
"Andrew Koenig" <ar*@acm.org> wrote...
" John" <乔********* @ yahoo.com>在消息中写道
新闻:c3 ************************** @ posting.google.c om ...
"John" <jo*********@yahoo.com> wrote in message
news:c3**************************@posting.google.c om...
在我的代码中,我使用内联构造函数定义了一个类。但它没有工作。我将课程描述如下:

myclass {
public:
myclass(int a,int b){r1 = a; r2 = b;}

受保护:
int r1;
int r2;
双z;
}

但是构造函数不起作用。当我声明一个
类的对象时,构造函数无法正确初始化变量。
In my code I define a class with inline constructor. But it does not
work. I describe the class as below:

myclass{
public:
myclass(int a, int b) { r1 = a; r2 = b;}

protected:
int r1;
int r2;
double z;
}

But the constructor does not work. When I declare an object of the
class, the constructor can not correctly initialize the variables.



你怎么知道程序不起作用?你上面显示的代码显然不是实际的代码,因为它不会编译。那么你实际上做了什么,你怎么知道它的行为不正确?



How do you know the program isn''t working? The code you show above is
obviously not the actual code, because it won''t compile. So what did you
actually do, and how do you know that it''s not behaving correctly?




绝对同意安德鲁在这里。只是旁注,我看过很多新手会写的b $ b $

myclass(int a,int b){int r1 = a ; int r2 = b;
甚至没有意识到它与预期的方式不同。它甚至可能发生这样的事情,即在将问题输入新闻组时,他们无意中纠正了错误。当然,如果编译器警告类似这样的情况,那将是很好的...


到OP:不要使用赋值,使用初始化列表。


Victor



Absolutely agree with Andrew here. Just a side note, I''ve seen many
times that a newbie would write

myclass(int a, int b) { int r1 = a; int r2 = b; }

without even realising that it''s different from the intended way. It
can even happen that while typing in their problem into a newsgroup
posting they inadvertently correct the error. Of course, it would be
nice if compilers warned about situations like that...

To the OP: do NOT use assignment, use the initialiser list.

Victor




" John" <乔********* @ yahoo.com>在留言中写道

news:c3 ************************** @ posting.google.c om ...

"John" <jo*********@yahoo.com> wrote in message
news:c3**************************@posting.google.c om...
大家好:

在我的代码中,我使用内联构造函数定义了一个类。但它没有工作。我将课程描述如下:

myclass {
public:
myclass(int a,int b){r1 = a; r2 = b;}

受保护:
int r1;
int r2;
双z;
}

但是构造函数不起作用。当我声明一个
类的对象时,构造函数无法正确初始化变量。

然后我修改代码如下:

myclass {
public:
myclass(int a,int b);

protected:
int r1;
int r2;
double z;
}

myclass :: myclass(int a,int b){
r1 = a;
r2 = b;
}

有什么问题?
Hi all:

In my code I define a class with inline constructor. But it does not
work. I describe the class as below:

myclass{
public:
myclass(int a, int b) { r1 = a; r2 = b;}

protected:
int r1;
int r2;
double z;
}

But the constructor does not work. When I declare an object of the
class, the constructor can not correctly initialize the variables.

Then I modify the code as below:

myclass{
public:
myclass(int a, int b);

protected:
int r1;
int r2;
double z;
}

myclass::myclass(int a, int b) {
r1 = a;
r2 = b;
}

It works. When I declare an object of the class, the constructor can
correctly initialize variables.

What is the problem?




看到整个节目是不可能的。您发布的两个代码示例

同样不正确(缺少半冒号,缺少关键字等)

但是就编写内联构造函数而言,您正确地执行了此操作。所以

无论你的问题是什么,它都在你没有发布的代码中。这个

是一个非常常见的情况,新手发布到这个群体,他们不会理解他们出错的地方并经常发布该计划的一部分

这是正确的,而不是错误的部分。如果您需要帮助,请发布

实际代码,并发布完整的程序。


john



Without seeing an entire program it is impossible to say. Both code examples
you posted are equally incorrect (missing semi colons, missing keywords etc)
but as far as writing an inline constructor goes you did that correctly. So
whatever your problem was it was somewhere in the code you didn''t post. This
is a very common situation with newbies posting to this group, they don''t
understand where they are going wrong and often post the part of the program
that is correct, not the part that is wrong. If you want help then post the
real code, and post a complete program.

john


这篇关于关于构造函数的奇怪问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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