复制数组元素的构造函数??? [英] Copy Constructor for an Array Element ???

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

问题描述

//

//我的语法对于

//数组元素的复制构造函数是否有问题,或者是否为
// C ++语言不支持这个吗?

//


#include< stdio.h>

#include < stdlib.h>


class X {

int data;

public:

X(){Data = 56; };

X(const X& Y){Data = Y.Data; }; //复制构造函数

void构造(const X& Y){Data = Y.Data; } b / b};

int main()
/>
X * Temp =(X *)malloc(sizeof(X)* 10);

for(int N = 0; N< 10; N ++){

// Temp [N]。(ABC); //不编译

// Temp [N](ABC); //不编译

Temp [N] .Construct(ABC); //正确编译并执行

}

免费(临时);

返回0;

}

//
// Is there something wrong with my syntax for the
// Copy Constructor of an Array Element, or does
// the C++ language not support this?
//

#include <stdio.h>
#include <stdlib.h>

class X {
int Data;
public:
X() { Data = 56; };
X(const X& Y) { Data = Y.Data; }; // Copy Constructor
void Construct(const X& Y) { Data = Y.Data; };
};

int main()
{
X ABC;
X* Temp = (X*) malloc(sizeof(X) * 10);
for (int N = 0; N < 10; N++) {
// Temp[N].(ABC); // Does Not Compile
// Temp[N](ABC); // Does Not Compile
Temp[N].Construct(ABC); // Compiles and Executes Correctly
}
free(Temp);
return 0;
}

推荐答案

" Peter Olcott" <醇**** @ worldnet.att.net>写了...
"Peter Olcott" <ol****@worldnet.att.net> wrote...
//
//我的语法对于数组元素的复制构造函数是否有问题,或者
// C ++语言不支持这个?


后者。构造函数没有名称,因此在名称查找期间无法找到
,因此无法_called_。在构造对象时,他们只能_invoked_



鉴于这种情况,你最好使用_placement_new_

(在一本好的C ++书中读到它)。

//

#include< stdio.h>
#include< stdlib.h> ;类X {
int data;
public:
X(){Data = 56; X(const X& Y){Data = Y.Data; }; //复制构造函数
void构造(const X& Y){Data = Y.Data; };
};

int main()
{/> X ABC;
X * Temp =(X *)malloc(sizeof(X) * 10);
for(int N = 0; N< 10; N ++){
// Temp [N]。(ABC); //不编译
// Temp [N](ABC); //不编译
Temp [N] .Construct(ABC); //正确编译和执行
}
免费(临时);
返回0;
}
//
// Is there something wrong with my syntax for the
// Copy Constructor of an Array Element, or does
// the C++ language not support this?
The latter. Constructors do not have names, therefore they cannot
be found during name lookup and hence cannot be _called_. They can
only be _invoked_ during construction of the object.

Given this situation, you''d be better off using _placement_new_
(read about it in a good C++ book).
//

#include <stdio.h>
#include <stdlib.h>

class X {
int Data;
public:
X() { Data = 56; };
X(const X& Y) { Data = Y.Data; }; // Copy Constructor
void Construct(const X& Y) { Data = Y.Data; };
};

int main()
{
X ABC;
X* Temp = (X*) malloc(sizeof(X) * 10);
for (int N = 0; N < 10; N++) {
// Temp[N].(ABC); // Does Not Compile
// Temp[N](ABC); // Does Not Compile
Temp[N].Construct(ABC); // Compiles and Executes Correctly
}
free(Temp);
return 0;
}




V



V




" Victor Bazarov" <五******** @ comAcast.net>在消息中写道:

"Victor Bazarov" <v.********@comAcast.net> wrote in message:
" Peter Olcott" <醇**** @ worldnet.att.net>写了...
"Peter Olcott" <ol****@worldnet.att.net> wrote...
//
//我的语法对于数组元素的复制构造函数是否有问题,或者
// C ++语言不支持这个?
//
// Is there something wrong with my syntax for the
// Copy Constructor of an Array Element, or does
// the C++ language not support this?



后者。构造函数没有名称,因此在名称查找期间无法找到它们,因此无法_called_。在构造对象时,它们只能被_invoked_。

鉴于这种情况,你最好使用_placement_new_
(在一本好的C ++书中读到它) 。



The latter. Constructors do not have names, therefore they cannot
be found during name lookup and hence cannot be _called_. They can
only be _invoked_ during construction of the object.

Given this situation, you''d be better off using _placement_new_
(read about it in a good C++ book).




我建议使用赋值运算符或成员函数赋值

(如std库容器)而不是放置new,除非你是

绝对确定你需要它。


如果你确实使用了new来构建一个
$ b之上的对象$ b现有对象,请务必首先调用对象析构函数

(除非你绝对确定它没有必要。)


Jonathan



I''d recommend using an assignment operator or a member function assign
(like std library containers) instead of placement new, unless you are
absolutely sure you need it.

And if you do use placement new to constuct an object on top of an
existing object, make sure to call the objects destructor first
(unless you are absolutely sure it''s not necessary.)

Jonathan


Peter Olcott写道:
Peter Olcott wrote:
//
//我的语法是否有问题
//复制数组元素的构造函数,或者不支持C ++语言吗?
//

#include< stdio.h>
#include&l t; stdlib.h>

类X {
private:

//表示int数据;
public:
X(void ):数据(56){} //默认
X(const X& Y):Data(Y.Data){} // Copy Constructor
// void Construct(const X& Y){Data = Y.Data; };
//太晚了。 *这已经构建了

X&修改(const X& Y){Data = Y.Data;返回*这个; int main(int argc,char * argv []){
X ABC;
X * Temp =(X *)malloc(sizeof(X)*}};

10); //你不应该这样做(int N = 0; N< 10; ++ N){
Temp [N] = ABC; //使用默认作业

Temp [N] .modify(ABC); }
免费(临时);
返回0;
}
//
// Is there something wrong with my syntax for the
// Copy Constructor of an Array Element, or does
// the C++ language not support this?
//

#include <stdio.h>
#include <stdlib.h>

class X { private:
// representation int Data;
public:
X(void): Data(56) { } // default
X(const X& Y): Data(Y.Data) { } // Copy Constructor
// void Construct(const X& Y) { Data = Y.Data; }; // too late. *this already constructed
X& modify(const X& Y) { Data = Y.Data; return *this; } };

int main(int argc, char* argv[]) {
X ABC;
X* Temp = (X*)malloc(sizeof(X)*10); // you shouldn''t do this
for (int N = 0; N < 10; ++N) { Temp[N] = ABC; // uses default assignment
Temp[N].modify(ABC); }
free(Temp);
return 0;
}






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

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