Thread: allocatable arrays in CLASSes and STRUCTS

  1. #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		
    
    };

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #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

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Just as you'd do it with an array.
    I might be wrong.

    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).

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Thank you a lot, I will try it sometime soon.
    All the best

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread