C++ inheritance question

hi`

I didn’t say that it is not possible with structs to have virtual
functions. I am aware that the only difference is the default access
permission. What I wanted to say (I formulated it a bit strange) is that
you can do something like

class Rect
{
public:
int x1;
int y1;
int x2;
int y2;

Rect();
~Rect();

}

and then use the following struct in C code to access the member vars of
the previous class. This is only guaranteed to work if the class e.g.
has no virtual functions.

struct Rect
{
int x1;
int y1;
int x2;
int y2;
}

Sorry for the confusion, it really was badly formulated the first time.
Well, this is getting really off topic :wink: BTW, could you email me that
g++ killer code :slight_smile:

Dave wrote:>

That isn’t true either. Structs can have anything a class can. My advisor
at my university is a moderator for comp.std.c++ and he attends the
standards meeting for ANSI C++. He is an expert and assures me that
structs and classes are identical and the access permissions are the only
difference. This means structs can have virtual functions and be base
"structs" if you want. A struct can do anything a class can do but has
default public access. If you don’t believe me ask the question on
comp.std.c++ newsgroup… You will get the same answer…
In fact he showed me code today that will put g++ in an infinite loop and
shows that it doesn’t handle classes properly. That’s ok though it really
broke Visual C++. We haven’t tested borland yet. If you want I will send
you the code… Just email me…

Dave


Daniel Vogel My opinions may have changed,
666 @ http://grafzahl.de but not the fact that I am right

Daniel Vogel wrote:

hi`

I didn’t say that it is not possible with structs to have virtual
functions. I am aware that the only difference is the default access
permission. What I wanted to say (I formulated it a bit strange) is that
you can do something like

class Rect
{
public:
int x1;
int y1;
int x2;
int y2;

    Rect();
    ~Rect();

}

and then use the following struct in C code to access the member vars of
the previous class. This is only guaranteed to work if the class e.g.
has no virtual functions.

struct Rect
{
int x1;
int y1;
int x2;
int y2;
}

Sorry for the confusion, it really was badly formulated the first time.
Well, this is getting really off topic :wink: BTW, could you email me that
g++ killer code :slight_smile:

That’s the same if the first class was a struct.

That’s the exact reason C++ structs (which are the same as classes
except for access permissions) are compatible with C structs: the latter
have no virtual methods.–
Marc A. Lepage
http://www.antimeta.com/
Minion open source game, RTS game programming, etc.

“Marc A. Lepage” wrote:

That’s the same if the first class was a struct.

Sure.

That’s the exact reason C++ structs (which are the same as classes
except for access permissions) are compatible with C structs: the latter
have no virtual methods.

Yes, but if the class had virtual member functions the standard says
nothing about the memory layout, although most compilers do it the way
you intuitively think :wink: So if I had a

class Rect
{
int a;
virtual void dummy();
}

I couldn’t use

struct Rect
{
int a;
}

in my C code to access ‘a’. Well, I could of course but it is not
guaranteed to work as the compiler might arrange the variables
differently in memory.–
Daniel Vogel My opinions may have changed,
666 @ http://grafzahl.de but not the fact that I am right

This goes even further. You can use a struct to access member variables
and member functions of a class in some cases (no virtual functions and
some other limitations).

That isn’t true either. Structs can have anything a class can. My advisor
at my university is a moderator for comp.std.c++ and he attends the
standards meeting for ANSI C++. He is an expert and assures me that
structs and classes are identical and the access permissions are the only
difference. This means structs can have virtual functions and be base
"structs" if you want. A struct can do anything a class can do but has
default public access. If you don’t believe me ask the question on
comp.std.c++ newsgroup… You will get the same answer…
In fact he showed me code today that will put g++ in an infinite loop and
shows that it doesn’t handle classes properly. That’s ok though it really
broke Visual C++. We haven’t tested borland yet. If you want I will send
you the code… Just email me…

Dave

At the command-line try typing ‘ipcs’. It will list all the shared
memory and semaphore arrays that you have. If it’s a long list, and
you’re not running anything else, you can remove them with ipcrm (do a
’man ipcrm’).

Thank you for this little tidbit. Wow! 128 semaphores in use on my

system!..:slight_smile: A little Perl program takes care of that. Whenever I run the
program, another semaphore gets leaked. Sam is definitely right about
the leakage.

    -The Mighty Mike Master
    http://tmmm.simplenet.comOn Wed, 29 Mar 2000, Jeff Freedman wrote:

Interesting. Apparently you are leaking semaphores when threads are not
being cleaned up properly. This is not entirely unexpected. Perhaps SDL
should keep track of the threads and semaphores and clean them all up when
the application quits?
Thanks to the ipcs trick, I’m able to track explicitly what’s
happening. Is there a better way to write the program to avoid this
problem? I know I’m calling SDL_CloseAudio() like I should. The SDL test
programs do the same thing, leaking semaphores all over the place.

I recommend that you upgrade to glibc-2.1, specifically because of those
threading problems SDL is trying to work around, though.
I’ll work on this. But do you suppose there will be a fix for the
glibc 2.0 people? I would hate for my program to quietly chew up their
system resources, even if they do have to run the application 128 times
before they notice.

    -The Mighty Mike Master
    http://tmmm.simplenet.comOn Wed, 29 Mar 2000, Sam Lantinga wrote:

Daniel Vogel wrote:

Yes, but if the class had virtual member functions the standard says
nothing about the memory layout, although most compilers do it the way
you intuitively think :wink: So if I had a

class Rect
{
int a;
virtual void dummy();
}

I couldn’t use

struct Rect
{
int a;
}

in my C code to access ‘a’. Well, I could of course but it is not
guaranteed to work as the compiler might arrange the variables
differently in memory.

You can replace class/struct in either of those. All four combinations
work. As long as the access is correct, and even then you can #define it
away.–
Marc A. Lepage
http://www.antimeta.com/
Minion open source game, RTS game programming, etc.

This was a really interesting problem.
SDL 1.1 has a mutex that it uses to protect the per-thread errno, but
because it’s not ever safe to assume that child threads are no longer
around when the main thread quits, the mutex is never freed.

With pthreads, this isn’t a problem because there is a manager thread
that watches the main thread and cleans everything up when it exits,
however under glibc-2.0 (and libc5), there is no failsafe to clean up,
and the thread lock mutex was leaked - once per multi-threaded SDL program.

I added a fix - create the thread locking mutex with a unique key, which
is shared between all instances of SDL applications, and is reused in
subsequent runs of the application. I increment the key and try again,
if I can’t create the mutex because it exists and is owned by another user.

This is in CVS, and will be in the soon to come 1.1.2 release.

Thanks for the brain teaser! :slight_smile:

See ya,
-Sam Lantinga (slouken at devolution.com)

Lead Programmer, Loki Entertainment Software–
“Any sufficiently advanced bug is indistinguishable from a feature”
– Rich Kulawiec

hello,

I am using sdl 1.1.1 with opengl …
I am having great difficulty (and am slowly being driven entirely INSANE !!!)
moving an object fowards by 1 unit when I know its x z and y rotations… I can
do it in 2d by

x = sin(rotationy);
y = cos(rotationy);

it is obvious that in 3d it is a LOT more complex but I have been drawing
diagrams and trying to figure it out for more than 4 hours now and I cant do it
!!!

Is it possible or what ??
Someone said opengl will do this allready but how ?
I can translate points when they are in glvertex3f etc…
but how to get it to give you the values rather than put it on screen ?

thanks a heap,
Oliver Batchelor

hello,

I am using sdl 1.1.1 with opengl …
I am having great difficulty (and am slowly being driven entirely INSANE
!!!)
moving an object fowards by 1 unit when I know its x z and y rotations… I
can
do it in 2d by

x = sin(rotationy);
y = cos(rotationy);

it is obvious that in 3d it is a LOT more complex but I have been drawing
diagrams and trying to figure it out for more than 4 hours now and I cant
do it
!!!

What, do you mean the 3D rotation equations? The basic principle is that you
rotate in two dimensions, but around three axes. (X, Y, and Z) So if 2D
rotation looks like:

NX = Cos(Angle) * X - Sin(Angle) * Y
NY = Sin(Angle) * X + Cos(Angle) * Y

You basically extend this to three dimensions, giving you a series of
equations something like this (if memory serves):

For rotation around the X Axis:

NY = Cos(AngleX) * Y - Sin(AngleX) * Z
NZ = Sin(AngleX) * Y + Cos(AngleX) * Z

For rotation around the Y Axis:

NX = Cos(AngleY) * X - Sin(AngleY) * Z
NZ = Sin(AngleY) * X + Cos(AngleY) * Z

and so forth for the Z axis. Just do these in order of Z, X, Y, and you
should be fine.

There is a more efficient subset of routines which reduce the number of
multiplications necessary from 12 to 9, but they use matrix mathematics. I
can explain how to do them if you want.

You can also easily optimize one multiplication out of there if you’re
clever. If you have trouble, hunt up a Grade 12 or first year uni. math text
and jump through the trig section. It’s really easy once you figure it out.

Oliver Batchelor

Cheers,

Nicholas

Nicholas Vining “While you’re out there struggling
vining at pacificcoast.net with your computer, I’m naked,
icq: 20872003 clueless, and feeling good!”
- Ratbert

----- Original Message -----
From: shadows@ihug.co.nz (Oliver Batchelor)
To: sdl at lokigames.com
Date: Wednesday, March 29, 2000 11:37 PM
Subject: [SDL] moving foward in 3d with x y and z rotation

I suspect that a Quaternoin library might be what your after here, assuming
I’ve got the right end of the stick. Gamasutra have an article on it,
somewhere

Ben> ----- Original Message -----

From: Nicholas Vining [mailto:vining@pacificcoast.net]
Sent: 30 March 2000 08:55
To: sdl at lokigames.com
Subject: Re: [SDL] moving foward in 3d with x y and z rotation

-----Original Message-----
From: shadows@ihug.co.nz (Oliver Batchelor)
To: sdl at lokigames.com
Date: Wednesday, March 29, 2000 11:37 PM
Subject: [SDL] moving foward in 3d with x y and z rotation

hello,

I am using sdl 1.1.1 with opengl …
I am having great difficulty (and am slowly being driven entirely INSANE
!!!)
moving an object fowards by 1 unit when I know its x z and y rotations… I
can
do it in 2d by

x = sin(rotationy);
y = cos(rotationy);

it is obvious that in 3d it is a LOT more complex but I have been drawing
diagrams and trying to figure it out for more than 4 hours now and I cant
do it
!!!

What, do you mean the 3D rotation equations? The basic principle is that you
rotate in two dimensions, but around three axes. (X, Y, and Z) So if 2D
rotation looks like:

NX = Cos(Angle) * X - Sin(Angle) * Y
NY = Sin(Angle) * X + Cos(Angle) * Y

You basically extend this to three dimensions, giving you a series of
equations something like this (if memory serves):

For rotation around the X Axis:

NY = Cos(AngleX) * Y - Sin(AngleX) * Z
NZ = Sin(AngleX) * Y + Cos(AngleX) * Z

For rotation around the Y Axis:

NX = Cos(AngleY) * X - Sin(AngleY) * Z
NZ = Sin(AngleY) * X + Cos(AngleY) * Z

and so forth for the Z axis. Just do these in order of Z, X, Y, and you
should be fine.

There is a more efficient subset of routines which reduce the number of
multiplications necessary from 12 to 9, but they use matrix mathematics. I
can explain how to do them if you want.

You can also easily optimize one multiplication out of there if you’re
clever. If you have trouble, hunt up a Grade 12 or first year uni. math text
and jump through the trig section. It’s really easy once you figure it out.

Oliver Batchelor

Cheers,

Nicholas

Nicholas Vining “While you’re out there struggling
vining at pacificcoast.net with your computer, I’m naked,
icq: 20872003 clueless, and feeling good!”
- Ratbert

Assuming you are only displaying your object in 3d, but it is moving
around in a 2d fashion (like on a flat floor)…

First of all keep a copy of your 2D movement coordinates. Draw your
object around origin, then rotate it to be facing in the direction it
going to move ( glRotatef(angle, 0.0f, 1.0f, 0.0f) ), then use
glTranslatef to put the object in it’s final position, the big
difference is it will be on the XZ plane instead of the XY plain, so
just translate your 2d y on the z axis instead (and keep in mind that
z is negative going away from the camera and positive going towards
it).

Hope that helps!

Phoenix Kokido
members.xoom.com/kokido
@Wes_Poole

Oliver Batchelor wrote:

hello,

I am using sdl 1.1.1 with opengl …
I am having great difficulty (and am slowly being driven entirely INSANE
!!!)
moving an object fowards by 1 unit when I know its x z and y rotations… I
can
do it in 2d by

x = sin(rotationy);
y = cos(rotationy);

it is obvious that in 3d it is a LOT more complex but I have been drawing
diagrams and trying to figure it out for more than 4 hours now and I cant
do it> !!!

Is it possible or what ??
Someone said opengl will do this allready but how ?
I can translate points when they are in glvertex3f etc…
but how to get it to give you the values rather than put it on screen ?

thanks a heap,
Oliver Batchelor

Daniel Vogel wrote:

No. The only difference between a struct and a class is the default
access specifier.

This goes even further. You can use a struct to access member variables
and member functions of a class in some cases (no virtual functions and
some other limitations).

Huh? I don’t exactly understand that one… struct are the same as class
(except for the default access specifier), period. They can have
virtual, pure virtual, destructors, constructors, everything the same as
class, but they are “public” instead of “private”, that’s all.–
Pierre Phaneuf
Systems Exorcist

“Marc A. Lepage” wrote:

class Rect
{
int a;
virtual void dummy();
}

I couldn’t use

struct Rect
{
int a;
}

in my C code to access ‘a’. Well, I could of course but it is not
guaranteed to work as the compiler might arrange the variables
differently in memory.

You can replace class/struct in either of those. All four combinations
work. As long as the access is correct, and even then you can #define it
away.

You can also play a trick and slip a virtual method in your struct, like
this:

struct Rect
{
int a;
virtual void dummyVirtual() {};
}

Note that I think that struct and class share the same name space, so
you cannot have a “class Rect” and a “struct Rect”. I’m not 100% sure,
but around 99%…–
Pierre Phaneuf
Systems Exorcist

Pierre Phaneuf wrote:

Huh? I don’t exactly understand that one… struct are the same as class
(except for the default access specifier), period. They can have
virtual, pure virtual, destructors, constructors, everything the same as
class, but they are “public” instead of “private”, that’s all.

To finally end this discussion/ confusion:

you can assume that the memory layout of a

class Rect
{
int x1;
int x2;
Rect();
~Rect();
}

is the same as

struct Rect
{
int x1;
int x2;
}

in C. If you have virtual functions in your class you can’t make any
assumptions about the memory layout and therefore it isn’t easy to
access the data by passing a pointer to an object from within a C
program.

Sorry for all this - I just wanted to mention that you can assume
something about the memory layout when accessing objects from C code…
I shouldn’t have mentioned it. My bad formulation caused quite some
confusion…–
Daniel Vogel My opinions may have changed,
666 @ http://grafzahl.de but not the fact that I am right

Nope I can do this already - Im trying to let it move in 3d eg. can have x and
z rotations too.On Fri, 31 Mar 2000, you wrote:

Assuming you are only displaying your object in 3d, but it is moving
around in a 2d fashion (like on a flat floor)…

First of all keep a copy of your 2D movement coordinates. Draw your
object around origin, then rotate it to be facing in the direction it
going to move ( glRotatef(angle, 0.0f, 1.0f, 0.0f) ), then use
glTranslatef to put the object in it’s final position, the big
difference is it will be on the XZ plane instead of the XY plain, so
just translate your 2d y on the z axis instead (and keep in mind that
z is negative going away from the camera and positive going towards
it).

Hope that helps!

Phoenix Kokido
members.xoom.com/kokido
kokido at postmark.net

Oliver Batchelor wrote:

hello,

I am using sdl 1.1.1 with opengl …
I am having great difficulty (and am slowly being driven entirely INSANE
!!!)
moving an object fowards by 1 unit when I know its x z and y rotations… I
can
do it in 2d by

x = sin(rotationy);
y = cos(rotationy);

it is obvious that in 3d it is a LOT more complex but I have been drawing
diagrams and trying to figure it out for more than 4 hours now and I cant
do it
!!!

Is it possible or what ??
Someone said opengl will do this allready but how ?
I can translate points when they are in glvertex3f etc…
but how to get it to give you the values rather than put it on screen ?

thanks a heap,
Oliver Batchelor

Pierre Phaneuf wrote:

You can also play a trick and slip a virtual method in your struct, like
this:

struct Rect
{
int a;
virtual void dummyVirtual() {};
}

Note that I think that struct and class share the same name space, so
you cannot have a “class Rect” and a “struct Rect”. I’m not 100% sure,
but around 99%…

Looking up “struct, and class” in the index of Stroustrup’s The C++
Programming Language (Third Edition) reveals this [section 10.2.8]:

"By definition, a struct is a class in which members are by default
public; that is,

struct s { ...

is simply shorthand for

class s { public: ..."

If you look up “struct, hack” you will find out how the namespace rules
bend for C compatibility. You can declare class foo and function foo in
the same namespace (in which case you must qualify the class to specify
it). But you can’t have struct foo and class foo in the same namespace;
they are the same.

[Nobody should be writing C++ without access to Stroustrup’s book.]–
Marc A. Lepage
Software Developer
Molecular Mining Corporation
http://www.molecularmining.com/

Looking up “struct, and class” in the index of Stroustrup’s The C++
Programming Language (Third Edition) reveals this [section 10.2.8]:

Can you folks take this to the C language mailing list?

Thanks. :slight_smile:

-Sam Lantinga				(slouken at devolution.com)

Lead Programmer, Loki Entertainment Software–
“Any sufficiently advanced bug is indistinguishable from a feature”
– Rich Kulawiec

“Marc A. Lepage” wrote:

Note that I think that struct and class share the same name space, so
you cannot have a “class Rect” and a “struct Rect”. I’m not 100% sure,
but around 99%…

If you look up “struct, hack” you will find out how the namespace rules
bend for C compatibility. You can declare class foo and function foo in
the same namespace (in which case you must qualify the class to specify
it). But you can’t have struct foo and class foo in the same namespace;
they are the same.

Ok, thanks for the confirmation!

[Nobody should be writing C++ without access to Stroustrup’s book.]

Well, I do, so THERE! :-)–
Pierre Phaneuf
Systems Exorcist