Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Koos

Variables in a class?

This topic is 6069 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just recently started to use classes in C++, and now I have a question regarding how to use a variable inside the class. Here is some example code.
  
typedef struct tagVERTEXBD
{
	GLfloat		x,y,z;	// Vertex Coordinates

	GLfloat		u,v;	// Vertex Texture Coordinates

}
VERTEXBD;

typedef struct tagSURFACEBD
{
	int             nv;    	        // Number of Vertices

	VERTEXBD        vertex[nv];     // Vertex Info(Not right)

}

  
Now, what I want is for the user to be able to read from a file the surface info. How can I get it to where for each new SURFACEBD they make, they can specify the exact amount of dimensions of vertex[nv]?

Share this post


Link to post
Share on other sites
Advertisement
First of all, they aren''t classes they are structures.


  
class CSurfaceBD
{
private:
CSurfaceBD(); // don''t allow default instantiation


public:
CSurfaceBD(int nElements) ( m_nElements = nElements; vertex = new VERTEXBD[m_nElements]; }
~CSurfaceBD() { delete [] vertex; }

protected :
int m_nElements;
VERTEXBD * vertex;
};



D.V.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
In C++ a struct is a class where all members default to public instead of private.

Share this post


Link to post
Share on other sites
Sorry, you are right of course.

Just a personal preference. Methodless data structures are structures, add a method and it becomes a class.

D.V.

Share this post


Link to post
Share on other sites
Being reading too many generic c++ books I see


all structures are classes, but not all classes are structures.


make VERTEXBD vertex[nv];
to this: VERTEXBD* vertex;
this make''s a pointer to it
then when they make a surface by calling a function called, say, MakeSurface(int dimx, int dimy)

in this function you would do something like this:

{
vertex = new VERTEXBD[dimx*dimy];
//then you fill in the info
for (int i = 0; i < dimx*dimy; i++)
vertex->x = whatever;
vertex[i]->y = whatever;
etc....
}

you basically will have to make a pointer and dynamically create it with this function.


Hope that helps.

Pactuul

Share this post


Link to post
Share on other sites
Ok, so say I write it like this.

typedef struct tagVERTEXBD
{
GLfloat x,y,z;
GLfloat u,v;
} VERTEXBD;

typedef struct tagSURFACEBD
{
int nv;
VERTEXBD* vertex;
} SURFACEBD;

SURFACEBD sf;

Would I define the number of vertices in sf by coding in:
sf->vertex = new VERTEXBD[sf->nv]:

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
YES! and to free the memory at the end use...

delete [] sf->vertex;

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!