delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/03/05/15:00:41

From: Dan <dyoon AT home DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: constructors + djgpp/allegro
Date: Thu, 05 Mar 1998 11:40:37 -0800
Organization: @Home Network
Lines: 102
Message-ID: <34FEFFB5.504E@home.com>
References: <D1FB30BBA491D1118E6D006097BCAE3911A919 AT Probe-nt-2a DOT Probe DOT co DOT uk>
Reply-To: dyoon AT home DOT com
NNTP-Posting-Host: cc104221-a.bnapk1.occa.home.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Shawn Hargreaves wrote:
> 
> Dan <dyoon AT home DOT com> writes:
> >    myship(int newX, int newY)
> >    {
> >       x = newX;
> >       y = newY;
> >       image = (BITMAP*)data[myship].dat;
> >    }
> > }
> 
> When you refer to data[myship].dat, I presume the <myship> is one of
> the object identifiers defined in your grabber header file? If so,
> you can't also have a class called myship, because this has been
> #defined to something else.

Sorry about that, in my program the variable and class names I used are 
different. I was just trying to show a simplified version of my
constructor.
Also, itt works with method "2" but
NOT with method "1".  The message i posted earlier was wrong.

> If this doesn't explain your problem, try to isolate the smallest
> possible complete code example that demonstrates the problem
> (something that we can actually compile and run).

Ok, I will include the source here.  I think it's about as small as
I could get it.  ALSO, please note the following which I just found
out...

For METHOD 1 (constructing WITHOUT the ":")
1)  As long as "image" is the first data member to be initialized, it
    works fine.
3)  If "image" is first member data declared, and it is the last to be
    initialized, then the program crashes everytime.

Here is an example of what I mean by the previous statements

//////////////////1)
class Ship
{
private:
   float pos_x, pos_y;  //current x, y position
   BITMAP *image;	
public:
   Ship(int newCx, int newCy)
   {
      image = (BITMAP*)data[bmp_ship].dat; //IMAGE HERE WORKS FINE
      pos_x = newCx - image->w/2;
      pos_y = newCy - image->h/2;
   }

///////////////2)
class Ship
{
private:
   BITMAP *image;
   float pos_x, pos_y;  //current x, y position
public:
   Ship(int newCx, int newCy)
   {
      pos_x = newCx - image->w/2;
      pos_y = newCy - image->h/2;
      image = (BITMAP*)data[bmp_ship].dat; //image here works fine.
      //IMAGE HERE MAKES MY PROGRAM CRASH
   }
////////////////////////////////////////////

For method 2 (constructing using the ":")
1) It doesn't matter when "image" is declared or initialized, as
   long as this construction method is used, the program does not
   crash.

Here is an example of what I am talking about...

////
class Ship
{
private:
   BITMAP *image;
   float pos_x, pos_y;  //current x, y position

public:
   Ship(int newCx, int newCy)
     :pos_x(newCx), image((BITMAP*)data[bmp_ship].dat), pos_y(newCy)
   {
      pos_x = newCx - image->w/2;
      pos_y = newCy - image->h/2;
   }
/////// in this example, image is declared first but initialized in the
        middle.  it makes no difference however and the program runs
fine.

OH MY!!!!!  I think I just figured out what the problem is.  I think
the problem occurs when I attempt to access "image->w/2" BEFORE 
"image" is initialzied.  I could be getting like a NULL pointer or 
something eh?  And the reason that the constructor with the ":" works
is because in that statement, "image" gets constructed before any
attempts to access "image->w".  Wow, that was such a headache for me.
Please let me know if this is what you think the problem is.

Daniel Yoon

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019