帮助复制构造函数。 [英] Help With Copy Constructor.

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

问题描述

我仍然在开发我的游戏,我的程序越来越好了。我希望工作的最多

。我想我在复制

构造函数时遇到了麻烦。基本上我希望它复制gdata数组。如果我这样做,我的gdata

将起作用:gdata [32] = this-> get(lp);但这不是我想要做的事情。当我编写代码时,我会得到一些神秘的错误。

注释掉了。


49 C:\Documents and Settings \Work\My Documents\C ++ \Dungeon

Adventure2 \gaphic.cpp将`const graphic''作为'this''参数传递

`BYTE graphic :: get(int)''丢弃限定词


C:\Documents and Settings \Work\My Documents\ C ++ \ Dungeon

Adventure2 \ Makefile.win [构建错误] [gaphic.o]错误1


我对指针和对象非常好但是我的编译器很令人沮丧

我。我可以得到一些建议,让这个复制建设者做它所需要的



class graphic {

int btmap;

int lr,ud; //图形的Diminsion(大小)

BYTE gdata [32]; //位图数组

HBITMAP hbitmap;

BITMAP位图;

HDC hdc,hdcmem;

void copy( BYTE in []);

BYTE get(int n){return gdata [n];}


public:

graphic();

graphic(BYTE c []);

graphic(const graphic&);

graphic& operator =(graphic&);

void SetGr(BYTE c []);

void set(BYTE c []);

void显示(HWND,int,int);

};


graphic :: graphic(const graphic& gr){

for(int lp = 0; lp!= 32; lp ++){

// gdata [32] = gr.get(lp); < - 我收到错误。

}

ud = lr = 16;

BITMAP位图= {0,ud,lr,2, 1,1};

bitmap.bmBits = gdata;

hbitmap = CreateBitmapIndirect(& bitmap);

}

解决方案

我正在调用这样的复制构造:


if(play){

cgr = new graphic(play-> gOut());

return * cgr;

}


如果这有帮助


JoeC写道:

BYTE get(int n){return gdata [n ];}




BYTE get(int n)const {return gdata [n];}


现在Google for ; const correct。你应该不断改变任何方法

不会改变*这个。


-

Phlip
http://c2.com/cgi/wiki?ZeekLand < - - 不是博客!!!




" JoeC" <恩***** @ yahoo.com>在消息中写道

news:11 ********************** @ g10g2000cwb.googlegr oups.com ...

我还在继续我的游戏,我的计划越来越好了。大多数我想要的作品。我想我在复制
构造函数时遇到了麻烦。基本上我希望它复制gdata数组。如果我这样做,我的gdata将会起作用:gdata [32] = this-> get(lp);但这并不是我想要做的事情当我编写代码时,我会得到一些神秘的错误。



它在我看来,最简单,最直接的方式来复制gdata的

内容就是这样做:


graphic :: graphic(const graphic& gr)

{

memcpy(gdata,gr.gdata,sizeof(BYTE)* sizeof(gdata));

}


- 丹尼斯


I am still working on my game and my program is getting better. Most
of what I want to works. I think I am having trouble with copy
constructor. Basically I want it to copy the gdata array. My gdata
will work if I do: gdata[32] = this->get(lp); But that is not what I
want to do I get some cryptic errors when I write the code like that is
commented out.

49 C:\Documents and Settings\Work\My Documents\C++\Dungeon
Adventure2\gaphic.cpp passing `const graphic'' as `this'' argument of
`BYTE graphic::get(int)'' discards qualifiers

C:\Documents and Settings\Work\My Documents\C++\Dungeon
Adventure2\Makefile.win [Build Error] [gaphic.o] Error 1

I am pretty good with pointer and objects but my compiler is fustrating
me. Can I get some advice to make this copy constructer do what it is
suppoded to do.

class graphic{
int btmap;
int lr,ud; //Diminsion (size) of the graphic
BYTE gdata[32]; //bitmap array
HBITMAP hbitmap;
BITMAP bitmap;
HDC hdc, hdcmem;
void copy(BYTE in[]);
BYTE get(int n){return gdata[n];}

public:
graphic();
graphic(BYTE c[]);
graphic(const graphic&);
graphic& operator = (graphic&);
void SetGr(BYTE c[]);
void set(BYTE c[]);
void display(HWND, int, int);
};

graphic::graphic(const graphic& gr){
for(int lp = 0; lp != 32; lp++){
//gdata[32] = gr.get(lp); <- I get errors.
}
ud = lr = 16;
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = gdata;
hbitmap = CreateBitmapIndirect(&bitmap);
}

解决方案

I am calling the copy constructr like this:

if(play){
cgr = new graphic(play->gOut());
return *cgr;
}

In case this helps


JoeC wrote:

BYTE get(int n){return gdata[n];}



BYTE get(int n) const {return gdata[n];}

Now Google for "const correct". You should make constant any method that
doesn''t change *this.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!



"JoeC" <en*****@yahoo.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...

I am still working on my game and my program is getting better. Most
of what I want to works. I think I am having trouble with copy
constructor. Basically I want it to copy the gdata array. My gdata
will work if I do: gdata[32] = this->get(lp); But that is not what I
want to do I get some cryptic errors when I write the code like that is
commented out.



It seems to me that the easiest and most straight-forward way to copy the
contents of gdata is to simply do this:

graphic::graphic(const graphic& gr)
{
memcpy( gdata, gr.gdata, sizeof(BYTE) * sizeof(gdata) );
}

- Dennis


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

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