Help With Copy Constructor.

D

Dennis Jones

JoeC said:
Thanks for the time in explaining how that works.

That helped, ithink my graphic object is much better but it still
crashes at this point:

graphic::graphic(const graphic& gr){
ud = lr = 16;
//gdata = gr.gdata; <-- This crashes
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = &gdata[0];
hbitmap = CreateBitmapIndirect(&bitmap);
}


That bity of code crashes when I use the command like this:

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

It works if I coment out that part but I get a block instead of my
graphic, I think I am getting close.

Is 'gdata' a vector?

- Dennis
 
D

Dennis Jones

Phlip said:
assert(this != & gr);


Are you sure you are not assigning to yourself?

I think you're onto something there, Phlip. I hadn't even considered that
possibility. Joe, if the 'assert' fails, then that's definitely your
problem. However, rather than asserting, it would be better to handle the
special case:

graphic::graphic(const graphic& gr)
{
if ( this == &gr ) return;
...
}

- Dennis
 
J

JoeC

It is now I made the changes that were suggested. It does work better
if I take that statment out it just displays a block and don't copy the
graphic info. If I try to copy the program crashes.
 
D

Dennis Jones

JoeC said:
It is now I made the changes that were suggested. It does work better
if I take that statment out it just displays a block and don't copy the
graphic info. If I try to copy the program crashes.

Again, you'll need to provide more info. -- just saying "the program
crashes" isn't very helpful. Have you tried stepping through the code with
a debugger to watch the values of critical variables when it crashes? Maybe
you should ask in one of the Windows newsgroups -- this group is about C++
in general, not Windows.

- Dennis
 
J

JoeC

Again thanks for the help. I thought this was more of a c++ problem.
If gdata = gr.gdata; copys the vector then it should work. I see no
reason why id dosn't. I don't need to copy each element or dop I need
to have a getter statment from gr object in the copy constructor.
 
D

Dennis Jones

JoeC said:
Again thanks for the help. I thought this was more of a c++ problem.
If gdata = gr.gdata; copys the vector then it should work. I see no
reason why id dosn't.

Exactly -- from a C++ perspective, it should work. If it doesn't, there is
something else wrong, perhaps in your use of Windows code.

I don't need to copy each element

I'm not exactly sure what you are saying here. If you are referring to the
vector elements, then no, you do not need to copy each element of the vector
because a single assignment copies the entire contents of the vector into
the new vector. However, if you are referring to the class members, then I
think it makes good sense to copy each of the members, even if you could
initialize them explicitly -- I think it makes the intent of the copy
constructor clearer (you're copying the contents of one object into another)
and it will probably lead to better C++ habits in the long run.

or dop I need
to have a getter statment from gr object in the copy constructor.

Again, I'm not sure what you are asking. You do not need a getter method to
access data members of the same class -- private members are always
accessible directly.

- Dennis
 
J

JoeC

If it should work, then I might have been OK on the C++ side. I have
learned quite a bit with project. I have read in theory about objects
and copying but it is another thing to do it in practice. I will ask
on another group.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top