C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-14-2009, 09:27 AM   #1
Registered User
 
Join Date: Aug 2006
Posts: 61
allocatable arrays in CLASSes and STRUCTS

Hi there,

I am using a struct and a Class whose elements may be arrays. I would like though, for these arrays to be dynamically allocatable when I declare them in the Class and main code; how is it supposed to be done in C++?

I was firstly trying to do it in C, but I prefer to try in C++ because I have files to read and find it easier for them to be read in C++.

I wrap my code here; it's very simple, however, please let me know if there are more information that you need

Thank you in advance,
Best
S.M

Code:
typedef struct VertexTag{

	float x; //x-coord of Vertex
	float y; //y-coord of Vertex
	float z; //z-coord of Vertex

} typeVertex;

//*******************************************************************//
//	Domain class:
//*******************************************************************//
class Mesh{

	public:
		typeVertex	coords[ SIZE_TO_BE_ALLOCATED_DYNAMICALLY ];

		Mesh(void);			// constructor		

};
simone.marras is offline   Reply With Quote
Old 03-14-2009, 10:29 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,366
Quote:
Originally Posted by simone.marras
I would like though, for these arrays to be dynamically allocatable when I declare them in the Class and main code; how is it supposed to be done in C++?
Generally, we would use a std::vector.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 03-14-2009, 10:37 AM   #3
Registered User
 
Join Date: Aug 2006
Posts: 61
Quote:
Originally Posted by laserlight View Post
Generally, we would use a std::vector.
thank you, but if I use a std::vector to declare COORDS, how would I access the elements of the struct?

thanks again
simone.marras is offline   Reply With Quote
Old 03-14-2009, 10:38 AM   #4
The larch
 
Join Date: May 2006
Posts: 3,082
Just as you'd do it with an array.
__________________
I might be wrong.

Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Quoted more than 1000 times (I hope).
anon is offline   Reply With Quote
Old 03-14-2009, 10:50 AM   #5
Registered User
 
Join Date: Aug 2006
Posts: 61
Thank you a lot, I will try it sometime soon.
All the best
simone.marras is offline   Reply With Quote
Reply

Tags
allocation, class, dynamic, structs

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Structs or classes? legit C++ Programming 4 06-28-2009 10:16 AM
classes, structs and unions Luciferek C++ Programming 24 08-09-2008 10:26 AM
Why use Classes instead of Structs? yaya C++ Programming 12 03-16-2008 12:39 AM
Are structs and classes the same thing? dwks C++ Programming 6 11-25-2005 03:21 PM
Classes or Structs faster for Lists White Rider C++ Programming 24 04-05-2002 03:57 PM


All times are GMT -6. The time now is 09:23 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22