Thread: Access array from class

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    58

    Thumbs down Access array from class

    Lets say I have a class declared in say 'Class.h' as such:
    Code:
    class Foobar
    {
    public:
    	struct Vertex
    	{
    		float x, y, z;	// Position of vertex in 3D space
    		RGBquad color;	// Color of vertex
    	};
    
    	Foobar(){};
    	~Foobar(){};
    };
    and I want to create an array of the Vertex structure without predefined size that can be accessed globally, I was thinking something like this: "Class.cpp"
    Code:
    #include "Class.h"
    
    Foobar::Vertex *VertexArray;
    but the problem is that it is not globally accessible! It can only be used inside Class.cpp : (

    I know that I am rather new to classes and pointers, but can someone please help me understand how I can have a function such as: "ExampSource.cpp"
    Code:
    #include "Class.h"
    
    void DoSouthPark()
    {
    	float timmy = Foobar::VertexArray[10].x;
    }
    * Yes I know VertexArray is not a pointer inside the Foobar class... thats what I'm trying to learn!

    - Thank you very much in advanced!

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You can make the vertex static:
    Code:
    // Header...
    class Foobar
    {
    public:
       struct Vertex
    	{
    		float x, y, z;	// Position of vertex in 3D space
    		RGBquad color;	// Color of vertex
    	};
    
       static Vertex _myStaticVertex;
    };
    
    // .ccp file
    Vertex Foobar::_myStaticVertex;
    
    void bar()
    {
        Foobar::_myStaticVertex.x = 5;
    }

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by bithub View Post
    static Vertex _myStaticVertex;
    Eh... such names are reserved.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by brewbuck View Post
    Eh... such names are reserved.
    A habit formed by the coding standard of the company I work for.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    58
    The array has to be able to be resized at whim, reallocated more specifically

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by brewbuck
    Eh... such names are reserved.
    Not in this particular case, but it is not good practice for mere users to use a leading underscore anyway.

    Quote Originally Posted by parad0x13
    The array has to be able to be resized at whim, reallocated more specifically
    Then use a std::vector<Vertex>.
    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

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    I think you need to look at your design and determine why different objects need to access another object's data directly, and refactor that design. Put operations on the class that owns the vertex array (or vector) that are called by other objects, don't allow other objects to manipulate the vertex array directly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM