http://GameProgrammer.Com

Programming

GP Mailing List
     Thread Index
     Date Index

ATXGPSIG List
     Thread Index
     Date Index

Google
>

Home

Wise2Food



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C++ Structure



> lachat wrote:
> >
> >   BruitageInfos                  MyBruitage;
>
> I assume you meant to type:
>
>     BruitageInfos* MyBruitage;
>
> Because you mention that this:
>
> >   MyBruitage = (BruitageInfos2  *)  malloc( sizeof(BruitageInfos2) );
>
> works.

The above DOESN'T work, of course, for 'MyBruitage' is a pointer to a
BruitageInfos structure, thus making the assignment invalid.

> >   MyBruitage->DSBuffer->Stop();
> >
> >   It's as if the compiler doesn't know what "MyBruitage" refers to !!
>
> It does know, but you're skipping a level of indirection.  MyBruitage is a
> pointer, not a structure.  Before you can access your DSBuffer member of
the
> structure you've defined, you need to dereference your pointer to tell the
> compiler what exactly you are doing.
>
> What you are trying to do should look like this:
>
>      (MyBruitage *).DSBuffer->Stop();

Actually it should look like this, assuming that a valid 'BruitageInfos' -
pointer:

(*MyBruitage).DSBuffer->Stop();

However, this is no solution for the problem, as we're not able to use the
above assignment. You could use something like the following example:

#include <iostream.h>

struct Test
{
 int a;
};

struct Test2
{
 int b;
};

void main()
{
 // create generic pointer
 void *test;

 test = new (Test);
// or test = new(Test2);

 // explicit conversion to Test*
 ((Test *)test)->a = 10;
// or ((Test2 *)test)->b = 10

 cout << ((Test *)test)->a << "\n";
// or cout << ((Test2 *)test)->b << "\n";

 // free memory again
 delete (test);
}

There's no way around this, if you want to work with generic pointers.

Ciao,
Ron Nanko
chief coder, gm²

Ceterum censeo Carthaginem esse delendam.

Ron Nanko, department of computer science, university of Kiel, Germany
contact me at rna@informatik.uni-kiel.de (university), ICQ #24926976,
RonNanko@gm-squared.de (private), or http://www.RonNanko.de
Visit my game development company at http://www.gm-squared.de



=================================================================
The GameProgrammer.Com mailing list is for the open discussion
of any topic related to the art, science, and business of
programming games. This list is especially tolerant of beginners.
We were all beginners once

To SUBSCRIBE or UNSUBSCRIBE please visit:
http://gameprogrammer.com/mailinglist.html