[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [ProgSoc] returning ptr to struct in c++ ?
That's strange because I seem to remember that the position of "const"
determines whether you are protecting the pointer variable or what's being
pointed to.
I'd probably also check that "object" is not a reserved or special token for
your compiler... It may not be a ANSI C++ standard thing but some compilers
might treat it differently.
Nigel
Matt wrote:
> On Tue, May 16, 2000 at 06:13:46PM +0200, Geoffrey Saxby wrote:
> > Hi! I writing some C++ code. I have stuff this in the header:
> >
> > ////////////////////////////////////////
> > typedef struct _point
> > { long x;
> > long y;
> > } point;
> >
> > typedef struct _obstacle
> > {
> > point tl; // top left
> > point tr; // top right
> > point bl; // bottom left
> > point br; // bottom right
> > } obstacle;
> >
> > typedef struct _object
> > {
> > obstacle * ob;
> > int ttl;
> > int type;
> > } object;
> >
> > class geoff {
> > public:
> > object * cloneOb(const object * pSrc) const;
> > };
> > ////////////////////////////////////////
> >
> > And this stuff is in the .cpp file:
> >
> > ////////////////////////////////////////
> > object * geoff::cloneOb(const object * pSrc) const
> > {
> > // alloacate space
> > pNewOb = new object;
> > pNewOb->ob = new obstacle;
> >
> > // fill it in
> > pNewOb->ob->tl.x = pSrc->ob->tl.x; pNewOb->ob->tl.y = pSrc->ob->tl.y;
> > pNewOb->ob->tr.x = pSrc->ob->tr.x; pNewOb->ob->tr.y = pSrc->ob->tr.y;
> > pNewOb->ob->bl.x = pSrc->ob->bl.x; pNewOb->ob->bl.y = pSrc->ob->bl.y;
> > pNewOb->ob->br.x = pSrc->ob->br.x; pNewOb->ob->br.y = pSrc->ob->br.y;
> > pNewOb->type = pSrc->type;
> > pNewOb->ttl = pSrc->ttl;
> >
> > return pNewOb;
> > }
> > ////////////////////////////////////////
>
> > but I get a compiler error "Declaration syntax error" on the
> > function declaration in the .cpp file.
>
> > (There are, of course, other things in the files like constructors
> > etc, but they do not cause me problems :)
>
> last i checked in C
>
> foo_t const *foo;
>
> and
>
> foo_t * const foo;
>
> are the same thing. i would be very suprised that they would be
> different in C++ (but there may be some inane reason). try changing
> your code to match, ie
>
> object const *geoff::cloneOb(const object * pSrc);
>
> matt
> --
> "Linux can be a harsh mistress"
> -- bert hubert
> -
> You are subscribed to the progsoc mailing list. To unsubscribe, send a
> message containing "unsubscribe" to progsoc-request@nospam.progsoc.uts.edu.au.
> If you are having trouble, ask owner-progsoc@nospam.progsoc.uts.edu.au for help.
-
You are subscribed to the progsoc mailing list. To unsubscribe, send a
message containing "unsubscribe" to progsoc-request@nospam.progsoc.uts.edu.au.
If you are having trouble, ask owner-progsoc@nospam.progsoc.uts.edu.au for help.