question about using functions and structures...

ok…

I have a structure…say call it “3dcube”

I have a fucntion…lets call it “makecube(parameters)”

now how can I write the function so that when the user says this:

Makecube(cube1);

the function will make a new “instance” (or whatever they are called) of the struct “3dcube” with the variable name “cube1” (from the MakeCube function)

I hope you guys understood my question…

Thanks,
Aditya

Not sure whether that way is possible but I would do this as an example:

struct 3dCube
{
various variables
};

void MakeCube(3dCube* newcube)
{
newcube->variable1 = value;
etc…
}

void CreateObjects()
{
3dCube Cube1;
CreateCube(&Cube1);
3dCube Cube2;
CreateCube(&Cube2);
etc…
}

Whether this is something along the lines of what you want I don’t know but you still have to create the cube variable before the routine. I don’t know of any way of generating a variable after passing it to a function. Perhaps a bit too complex for my knowledge limit so far…

Also, there is the object orientated approach with classes which are similarly created to structs. Whether this allows what you want instead of structs I don’t know.

I’m sure class experts can point you in an object orientated direction there.

Tina

thanks tina… I was using that workaround… but I just wanted to wait ans aks to see if a struct “3dcube” can be declared like:

3dcube Cube1;

where “Cube1” would be a name given by the user of the function rather than being declared a struct first…

This is kind of what I want:

struct 3dcube
{
int x;
int y;
int z;
};

void MakeCube(char cubename)
{
3dcube cubename;
}

And in the design mode… the author would be able to say

MakeCube(Cube1);

And this would create a struct named Cube1 with the struct format of the struct 3dcube… is it more clear now?

Again, thanks for at least trying to answer my really unclear question,

-Aditya

It almost sounds like you want to write a real time drawing program.
Are you wanting to have this as a routine like say glut or glu primitives (sphere, cube).
Or are you wanting to add objects as the user is running the program?

I am still a bit unclear as to what you wanted.

Just like in you now have to add glut.h to your programs to use it.
You would also have to define some type of structure for your routine.

I not sure maybe you could do it the way you want with a pointer… but I have to think on it.

[This message has been edited by nexusone (edited 09-29-2002).]

If you are using C++, you could use the STL map to create a list of objects, and be able to easily access them with a name. It’s usage would be something like so:

#include
#include
#include “Object.h”

struct Object
{
// whatever
};
using namespace std;

typedef map<string, Object*> ObjectMap;

ObjectMap g_oObjects;

void CreateObject(string sName)
{
Object* pObj = new Object;

g_oObjects.push_back(sName, pObj);
}

Object* GetObject(string sName)
{
return g_oObjects[sName];
}

Thanks
I am not really that advanced in c++, but I will look into deciphering that

Thanks again, and if anyone can give me a simpler solution please try to do so,

-Aditya

How are you proposing to refer to each struct? Just by name? You need to know its address in memory to access its variables etc. You could make it a bit more dynamic buy using pointers so that your createcube func returns a pointer to a struct. or maybe you should be looking at someother structures like linked lists or… where you could make a fun addcube

Hey guys, I hate to be a terrible bore, but where is the:

pObj = new THING;

#ifdef __DEBUG
assert ( pObj );
#endif

if ( !pObj )
{
return ( false );
}
else
{

}

I think the answer is no you can not define a structure per your example.

If you tell us more on what function you are trying to have your program do, we could help point out a solution.

Originally posted by adityagaddam:
[b]Thanks
I am not really that advanced in c++, but I will look into deciphering that

Thanks again, and if anyone can give me a simpler solution please try to do so,

-Aditya[/b]

Ok this is some pseudo/real code that will probably explain it better…

My files:

  1. primitives.h - contains all the functions and stuff for making all the 2d and 3d primitives.

  2. main.cpp - this is ofcourse my main file and will bring all the headers and the code together

ok this code should go into primitives.h:

struct 3dcube
{
int x;
int y;
int z;
};

void makecube(char cubename, int x, int y, int z)
{

3dcube cubename (from the function parameters above);

cubename.x = x;
cubename.y = y;
cubename.z = z;

}

Then I should be able to use this in my rendering routine in main.cpp:

makecube(cube1, 0,0,0);

that call to the function should make a 3dcube struct called “cube1”…

is that more clear… if you guys can understand what I asked now, please correct the above pseudo/real code to the correct syntax, if something is not in the correct syntax… Sorry if I am unclear but I dont know how to describe my problem in detail… hope the above will help you guys to answer my question.

Thanks,
Aditya Gaddam

You can’t do that the way you want to, and honestly, I fail to see the reason you would need to.

Your pseudocode just uses a local instance of that struct, which is going to be destroyed when the method exits anyway.

If you think that by using a dynamic name like that is somehow going to let you store multiple instances with different names, then I hate to break it to you, but it don’t work like that. You’ll need to use some container object to keep a collection, and if you want to name each, also store a name for each. My example code for using an STL map is one way you could do this.

Yes… your varaible structures would be put in as your example: primitives.h

Now as for your routines your can have like a primitives.c (your primitive code) or you can create a library file (.lib) to put them in. Note that in C code is not ok to put the function code in the .h files, but in C++ it is ok from what I understand.

primitives.h

typedef struct
{
int x;
int y;
int z;
}cube3d; ///Note You can not have a variable or structure that starts with a number.

primitives.c

void make3dcube(cube3d *cube, int x, int y, int z )
{
cube.x = x;
cube.y = y;
cube.z = z;
}

// main.c

#include “primitives.h”
static cube3d My_cube; // make a place to store the new cube

void main()
{

make3dcube( My_cube, x, y, z);

}

How here is a big question, do you want the cube to be drawn when you call make3dcube?
or you going to have a routine called draw3Ddcube(cube)?

Originally posted by adityagaddam:
[b]Ok this is some pseudo/real code that will probably explain it better…

My files:

  1. primitives.h - contains all the functions and stuff for making all the 2d and 3d primitives.
  1. main.cpp - this is ofcourse my main file and will bring all the headers and the code together

ok this code should go into primitives.h:

struct 3dcube
{
int x;
int y;
int z;
};

void makecube(char cubename, int x, int y, int z)
{

3dcube cubename (from the function parameters above);

cubename.x = x;
cubename.y = y;
cubename.z = z;

}

Then I should be able to use this in my rendering routine in main.cpp:

makecube(cube1, 0,0,0);

that call to the function should make a 3dcube struct called “cube1”…

is that more clear… if you guys can understand what I asked now, please correct the above pseudo/real code to the correct syntax, if something is not in the correct syntax… Sorry if I am unclear but I dont know how to describe my problem in detail… hope the above will help you guys to answer my question.

Thanks,
Aditya Gaddam

[/b]

I think you mis-understood what he wanted. Look at his pseudo-code again.

void makecube(char cubename, int x, int y, int z)
{

3dcube cubename (from the function parameters above);

cubename.x = x;
cubename.y = y;
cubename.z = z;

}

It seems to me that maybe he thinks that if you use the same name of an object in two different places, you are accessing the same object. This is NOT the case if each of the objects is in a different scope. (e.g. two different functions.)

You can’t do this with a function, but you can do it with a macro:

#define makecube(cubename,x,y,z)\
Cube3D cubename = {x,y,z};

Of course, the variable ‘cubename’ would be subject to the same scope rules as other variables. If you want it to be global, you have to call the macro outside of all functions.

Having said this, I don’t see the point of using it, and I think you are confusing C/C++ with an interpreted language (Basic, Matlab, etc.). The argument ‘cubename’ in the above macro is an identifier, not a character string. If you want to get the name of a variable at run time, how do you expect to know how to reference it in your code?

Also, you also can’t call the struct 3dCube because identifiers can’t begin with a number.

[This message has been edited by Aaron (edited 10-01-2002).]

I think you missed that he also asked the correct way to do it.

I tried to give an example of how you may create something close. Since he can not do it his way.

Originally posted by Deiussum:
[b]I think you mis-understood what he wanted. Look at his pseudo-code again.

void makecube(char cubename, int x, int y, int z)
{

3dcube cubename (from the function parameters above);

cubename.x = x;
cubename.y = y;
cubename.z = z;

}

It seems to me that maybe he thinks that if you use the same name of an object in two different places, you are accessing the same object. This is NOT the case if each of the objects is in a different scope. (e.g. two different functions.)[/b]

Would the following answer your query?

struct cube
{
int x;
int y;
int z;
};

cube* MakeCube ( int X, int Y, int Z )
{
cube *newcube;
newcube.x = X;
newcube.y = Y;
newcude.z = Z;
return newcube;
}

void DrawCube(cube *c)
{
glBegin(GL_QUADS); // right face
glVertex3f(c.x/2,-c.y/2,-c.z/2);
glVertex3f(c.x/2,-c.y/2,c.z/2);
glVertex3f(c.x/2,c.y/2,c.z/2);
glVertex3f(c.x/2,c.y/2,-c.z/2);
glEnd;
glBegin(GL_QUADS); // back face

}
// Then your main would look like this

int main ( void)
{
cube *my_cube;
my_cube = MakeCube(1,1,1);
// Insert loop function
DrawCube(my_cube);
// On exiting
free(my_cube)
return 0;
}

wow! thanks for all your replies guys… I think some of you guys finally understood my stupid and unclear question… but I haven’t gotten what I really need… at least I dont think so. but I will look at all your solutions. I am currently using the workaround of first declaring a new “cube3d” struct and then passing it as a parameter to the function Makecube(cube3d cube);

Thanks again and if someone does come up with something please post it here… otherwise, dont waste your time guys… it is not that important… i just wanted my engine to be as code-free as possible(for the end user… not me of course )

-Aditya

Now you need to be careful, just making it simple may limit is usefulness also.

Let’s take your idea, GLUT does already does make programming a openGL program easy. But there are limits to what you can do with glut objects.

I think if you are looking to make it simple for the programmer, make commands like this:

add_cube(name, size, x, y, z); adds a cube scene
add_triange(name, size, x, y, z); adds a triangle scene
add_textured_cube(name, char texture_name, x, y, z)

example code:
int add_cube( char *name, int s, int x, int y, int z)
{
int id_tag;

id_tag = add_object(); // our rendering engine would assign a id number.
// We create an object with the id_tag number, tell it to make a cube.
// Now the object cube is added to our que of objects to be rendered.
create_object( id_tag, CUBE, s, x, y, z);

return( id_tag )// return the address of the new object
}

// then you can have calls like this
Object_move( id_tag, x, y, z);
Object_scale( id_tag, s);
// You could also use a name, instead of id tag
// Look something like this:
Object_move( “name”, x, y, z) or

char name[30]; // use variables to store names
Object_move( name, x, y, z)

// How your engine would have a processing routine
Object_engine()
{

if (id_tag_index != 0) Check for objects to process
{
for(i = 0; i < id_tag_index; i++)
{
draw_objects( i ); // draw each object that has been added.
}
Update_display();
}

Hope this gives you an idea…

Originally posted by adityagaddam:
[b]wow! thanks for all your replies guys… I think some of you guys finally understood my stupid and unclear question… but I haven’t gotten what I really need… at least I dont think so. but I will look at all your solutions. I am currently using the workaround of first declaring a new “cube3d” struct and then passing it as a parameter to the function Makecube(cube3d cube);

Thanks again and if someone does come up with something please post it here… otherwise, dont waste your time guys… it is not that important… i just wanted my engine to be as code-free as possible(for the end user… not me of course )

-Aditya[/b]

[This message has been edited by nexusone (edited 10-02-2002).]

[This message has been edited by nexusone (edited 10-02-2002).]

keermalec

cube* MakeCube ( int X, int Y, int Z )
{
cube *newcube;
newcube.x = X;
newcube.y = Y;
newcude.z = Z;
return newcube;
}

that code hurts!
either do
cube newcube;
or
cube newcube;
newcube = (cube
) malloc(sizeof(cube));

and also

newcube->x = 123;

He wouldn’t want to do “cube newcube;”, unless the struct was returned, rather than the pointer to the struct. If you pass back a pointer to a local variable that is not dynamically allocated, you’re asking for trouble.