delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/10/10/09:43:28

From: "Arthur" <arfa AT clara DOT net>
To: <djgpp AT delorie DOT com>
Subject: RE: structures
Date: Sat, 10 Oct 1998 14:39:59 +0100
Message-ID: <000001bdf453$79cec9a0$f24b08c3@arthur>
MIME-Version: 1.0
X-Priority: 3 (Normal)
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook 8.5, Build 4.71.2173.0
Importance: Normal
In-Reply-To: <361E4CE8.A626C0D9@cableol.co.uk>
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
Reply-To: djgpp AT delorie DOT com

> You can't put functions inside structs in c, but you can put them inside
> classes in c++.  I think the function is called class, but I don't do
> c++ so I can't be sure.  Apart from replacing struct with class, it
> looks ok.
>
> 		Peter Allen

This is the equivalent in C++:

class NICEDOGY
{
   public:
	int x, y;
	int w, h;

	int Draw_It();
}

int NICEDOGY::Draw_It()
{
	x = w;
	y = h;

	return 0;  // Your function returned an int
}

You then call the function thus:

NICEDOGY dog;

dog.w = 42; dog.h = 12;

dog.Draw_It();

cout << dog.w << dog.h << dog.x << dog.y << endl;

Just remember to include the iostream library.

You can use struct rather than class, the difference being that classes are
private by default. By convention, struct is used when there are no
functions inside the structure and class is used when there are. Note that
typedef is not used in C++. If you're interested further, I can give you a
few links to C++ tutorials.

HTH

James Arthur
jaa AT arfa DOT clara DOT net
ICQ#15054819



> Twidler wrote:
> >
> > Does DJGPP support structures.  How about procedures in structures?
> >
> > I tried this in DJGPP:
> >
> > typedef struct NICEDOGY
> >     {
> >     int x, y;
> >     int w h;
> >
> >     int Draw_It()
> >         {
> >         x=w;
> >         y=h;
> >         }
> >     }NICEDOGY;
> >
> > and it didnt work...it gave me some errors at the Draw_It() procedure.
> > what is going on here?  How do I fix this without putting the Draw_It()
> > procedure outside of the structure?
> >
> > Twidler
>

- Raw text -


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