构造函数 [英] Constructor function

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

问题描述

Hello All,


我对构造函数感到困惑。说我有一个类

喜欢以下


班级考试{

int a;

public:


test(){}

test(int a){

this-> a = a; < br $> b $ b}

test(test& b){

this-> a = ba

}

void printVal(){

cout<< a<< endl;

}


现在我的主要功能
现在我有以下


(test(2))。printVal();


这个有效。它创建一个未命名的对象。 (我可以称这个未命名的

对象吗?)


以下情况也是如此


test b;

b = test(2);


这里,正在创建两个对象。 b表示b。是由构造函数test(2)创建的未命名的

对象的副本。


这是否意味着构造函数返回的对象是测试?

以下情况会发生什么?假设我在

文件范围内有这样的功能


void myFunc(测试a){


a .printVal();


}


当我从主叫这样的话时会发生什么:


myFunc(test(2));


这里,构造函数创建一个未命名的对象,然后myFunc获取

对象。但是怎么样?我测试了这个,我发现在这个

的情况下,没有复制

构造函数和赋值运算符(=)!

帮助我完成我的论文概念!


非常感谢

Hello All,

I am little confused about the constructor function. Say I have a class
like the following

class test{
int a;
public:

test(){}
test(int a){
this->a = a;
}
test(test &b){
this->a = b.a
}
void printVal(){
cout<<a<<endl;
}

}

now in my main function I have the following

(test(2)).printVal();

this works. It creates an unnamed object. (Can I call this unnamed
object?)

The following is true also

test b;
b = test(2);

here, two objects are being created. The "b" is a copy of unnamed
object created by the constructor "test(2)".

Does it mean that the constructor is returning an object of test?
Also what happens in the following case? Say I have function in the
file scope like this

void myFunc(test a){

a.printVal();

}

what happens when I call this from main like this:

myFunc(test(2));

here, the constructor creates an unnamed object, then myFunc gets that
object. But how? I tested this, and I found out that neither the copy
constructor nor the assignment operator(=) is being called in this
case!

Help me in my concept of theses!

thanks a lot

推荐答案

在新闻中写道:11 ********************** @ z14g2000cwz.googlegr psps.com在

comp.lang中。 c ++:
wrote in news:11**********************@z14g2000cwz.googlegr oups.com in
comp.lang.c++:
Hello All,

我对构造函数感到困惑。说我有一个班级
喜欢以下

班级考试{
int a;
公开:

考试(){} <测试(int a){
this-> a = a;
}
test(test& b){
this-> a = ba
}
void printVal(){
cout<< a<< endl;
}

}
现在在我的主要功能中我有以下

(test(2))。printVal();

这个有效。它创建一个未命名的对象。 (我可以打电话给这个未命名的
对象吗?)


(是的,它的名字和其他任何名字一样好,但是暂时的可能是

更好,我们不需要说出临时的无名临时

总是未命名的。

以下情况也是如此

测试b;
b = test(2);

这里,正在创建两个对象。 b表示b。是由构造函数test(2)创建的未命名对象的副本。

是否意味着构造函数返回测试对象?


不,不是真的;-(,编译器为临时

创建存储并将该存储传递给构造函数(''this''变量点

到它)。

在下面的例子中会发生什么?说我在
文件范围内有这样的功能

void myFunc(test a){

a.printVal();

}
当我从主叫这样的话时会发生什么:

myFunc(test(2));

这里,构造函数创建一个未命名的对象,然后myFunc获取该对象。但是如何?我测试了这个,我发现了在这个
的情况下,没有复制
构造函数和赋值运算符(=)!
Hello All,

I am little confused about the constructor function. Say I have a class
like the following

class test{
int a;
public:

test(){}
test(int a){
this->a = a;
}
test(test &b){
this->a = b.a
}
void printVal(){
cout<<a<<endl;
}

}

now in my main function I have the following

(test(2)).printVal();

this works. It creates an unnamed object. (Can I call this unnamed
object?)

(Yes, its as good a name as any other, but "a temporary" might
be better, we don''t need to say unnamed-temporary as temporaries
are always unnamed).
The following is true also

test b;
b = test(2);

here, two objects are being created. The "b" is a copy of unnamed
object created by the constructor "test(2)".

Does it mean that the constructor is returning an object of test?

No, well not really ;-(, the compiler creates storage for an temporary
and passes that storage to the constructor (the ''this'' variable points
to it).

Also what happens in the following case? Say I have function in the
file scope like this

void myFunc(test a){

a.printVal();

}

what happens when I call this from main like this:

myFunc(test(2));

here, the constructor creates an unnamed object, then myFunc gets that
object. But how? I tested this, and I found out that neither the copy
constructor nor the assignment operator(=) is being called in this
case!




使用了复制构造函数(但在那里正如你所发现的那样是一种扭曲。

编译器可以省略(通过)复制结构,在这种情况下,
,直接将对象构造到目标变量。


即使复制构造函数有副作用,也允许编译器执行此操作(例如:std :: cout<< 在复制构造函数中\ n;;)。

所以在上面的例子中,编译器将为paramiter创建存储空间

''a'' of myFunc,然后调用构造函数(test :: test(int))来

就地构造它,删除(通过)复制构造函数。


HTH。


Rob。

-
http://www.victim-prime.dsl.pipex.com/


bipod.rafique写道:
bipod.rafique wrote:
test(int a){
this-> a = a;


问候,并祝贺逃避Java。你会发现编写良好的C ++

代码更安全,更优雅,更高效...


在上述情况下,要么命名为不同的或替换这个 - > a与m_a。

这是一个常见的疣使成员看起来与当地人不同。

测试b;
b =测试(2);

这里,正在创建两个对象。 b表示b。是由构造函数test(2)创建的未命名对象的副本。

是否意味着构造函数返回测试对象?


构造函数不是函数 - 它是一个特殊的代码块,当你说出它的类的名字时,你会调用它。上面的工作好像''b''完全构建了b $ b,然后是一个临时的完全构造,然后分配

operator =将临时复制到''b''。


某些小的优化会发生,其中一些可以省略额外的构造和副本,但是你不应该编写代码太多

很多构造函数所以你不必担心这些。

void myFunc(测试a){

a.printVal();

myFunc(test(2));


这是多余的。考虑测试a = 2.你得到与

myFunc(2)相同的效果。

这里,构造函数创建一个未命名的对象,然后myFunc得到它
宾语。但是怎么样?我测试了这个,我发现在这个
的情况下,没有复制
构造函数和赋值运算符(=)!
test(int a){
this->a = a;
Greetings, and congrats from escaping Java. You will find well-written C++
code to be safer, more elegant, and much more productive...

In the above case, either name a different or replace this->a with m_a.
That''s a common "wart" to make members look different from locals.
test b;
b = test(2);

here, two objects are being created. The "b" is a copy of unnamed
object created by the constructor "test(2)".

Does it mean that the constructor is returning an object of test?
A constructor is not a function - it''s a special block of code invoked when
you speak the name of its class. The above works as if ''b'' fully
constructed, then a temporary fully constructed, then the assignment
operator = copied the temporary into ''b''.

Certain minor optimizations happen, and some of those are permitted to elide
extra constructions and copies, but you should not write code that does too
much in constructors so you shouldn''t worry about those.
void myFunc(test a){

a.printVal();

}

what happens when I call this from main like this:

myFunc(test(2));
That''s redundant. Consider test a = 2. You get the same effect with
myFunc(2).
here, the constructor creates an unnamed object, then myFunc gets that
object. But how? I tested this, and I found out that neither the copy
constructor nor the assignment operator(=) is being called in this
case!




由于我警告的次要优化 - 特别是返回

值构造优化。这里有人会记住它的正确名称。


-

Phlip
http://www.c2.com/cgi/wiki?ZeekLand


Phlip写道:
Phlip wrote:
构造函数不是函数 - 它是当你说出它的类名时调用的特殊代码块。
A constructor is not a function - it''s a special block of code invoked when
you speak the name of its class.




这是一个功能。它属于称为特殊

成员函数的组合。但是,它不参与名称解析

因此无法调用。除此之外,它遵循与其他非静态成员函数相同的

规则。



It is a function. It comes under the group of things called special
member functions. However, it does not participate in name resolution
and hence can''t be called. Other than that, it follows the same
rules that other non-static member functions do.


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

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