Access array from class

This is a discussion on Access array from class within the C++ Programming forums, part of the General Programming Boards category; Lets say I have a class declared in say 'Class.h' as such: Code: class Foobar { public: struct Vertex { ...

  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,246
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    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,246
    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
    21,394
    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>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 08: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

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