Hi

I have a C typedef struct that I need to change to a C++ class.

typedefstruct structTag3DScene
{
p3DObjectGroup pObjGroupArray; /* array of grouped scene objects */
int numObjGroups; /* count of grouped scene objects */
}
f3DScene, /* 3D Scene */
* p3DScene; /* pointer to 3D scene */

So far I have

class structTag3DScene
{
private:
p3DObjectGroup pObjGroupArray; /* array of grouped scene objects */
int numObjGroups; /* count of grouped scene objects */
 
public:
 
}

Well that’s all I can do!

If I new what the last two lines of the original struct did i might be able to work it out.
Can anyone help?
Thanks!

Don't forget that you need a ; at the end of your class defn -

class class_name {
  // ...
};

In C when you created a struct and didn't define it with a typedef you'd have to say:

struct struct_name struct_instance_name;

rather than what you'd be used to. However that need is now gone in C++ so you can just do a:

struct_name struct_instance_name;

and it'll be happy out. Now. In the code you posted there the structure is typedef-ed ... In C this would be fine with either of these methods

typedef str {
 // add stuff here
} my_struct;

// ...

my_struct my_inst; // <- OK
struct str my_inst_2; // <- OK

str my_inst_3;// <- illegal

so the typedef omits the necessity for a structure identifier in C, but it's no necessary in structures in C++ nor with classes so you can ignore those last two lines.

Really, all you do to change a struct to a class is change the keyword. The only significant difference between a class and a struct is the default access level - class defaults to private, struct to public. Otherwise, they are the (99.999%) the same.

Thanks, it was just the last two lines of code that threw me off a bit and it being a typedef so I wasn’t quite certain

Aye. change struct to class and put private/protected or public access specifiers where appropriate. But I believe (and I may be wrong cause it's from memory and I never did this), that in a class before the final semi colon but after the closing brace if something is typed there it becomes an instance...

class thing{ 
 // ...
} thingy;

thingy is an instance of thing, I belive. I may be wrong.

This article has been dead for over six months. Start a new discussion instead.