Thread: C++ pointer arrays as static class members

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    C++ pointer arrays as static class members

    Hello everyone.

    So i can get this code to work:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class test
    {
    public:
    	test(){}
    	~test(){}
    
    	void printInts()
    	{
    		locPtr = pointer;
    
    		for (int i=0; i<5; i++)
    		{
    			cout<<*locPtr;
    			locPtr++;
    		}
    	}
    		
    private:
    
    static int* pointer;
    int* locPtr;
    
    static int* getInts()
    {
    	int* intPtr = new int[5];
    	for (int i=0; i<5; i++)
    		intPtr[i] = i;
    
    	return intPtr;
    }
    };
    
    int *test::pointer = test::getInts();
    
    int main ()
    {
    	test a;
    	a.printInts();
    	return 0;
    }
    Now i would like to use the same convention but with
    Code:
     test::pointer
    being a pointer array like this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class test
    {
    public:
    	test(){}
    	~test(){}
    
    	void printInts()
    	{
    		locPtr = pointer[1];
    
    		for (int i=0; i<5; i++)
    		{
    			cout<<*locPtr;
    			locPtr++;
    		}
    	}
    		
    private:
    
    static int* pointer[2];
    int* locPtr;
    
    static int* getInts()
    {
    	int* intPtr = new int[5];
    	for (int i=0; i<5; i++)
    		intPtr[i] = i;
    
    	return intPtr;
    }
    };
    
    int *test::pointer[1] = test::getInts();
    
    int main ()
    {
    	test a;
    	a.printInts();
    	return 0;
    }
    ,which i know doesn't work. Please could you share any ideas of how I could make the latter code compile. Thanks
    Last edited by l1F; 03-25-2010 at 01:01 PM. Reason: posted before completing post

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If you just want to simply get it to compile then this:
    Code:
    int *test::pointer[1] = test::getInts();
    Could be changed to this:
    Code:
    int *test::pointer[2] = {0,test::getInts()};
    Where is the code that frees up the allocated memory?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Hi,

    Thanks for the speedy reply, you suggestion seems to be working. However, i was wondering if there was a way i could initialize the array elements separately.

    As for deallocating memory - i can't seem to find the correct place for that since the memory is required throughout the program.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    It is not possible to separately initialize static class member in the global scope of code. But it is possible to allocate your array separately in any function like initialize_pointer() written by yourself and call it before first use of test class. There you may put the array initialization code.

    It is a good technique to always deallocate the memory allocated by new[] operator:
    Code:
    int* intPtr = new int[5];
    But in this example it is placed inside a static function, so the memory will be freed automatically when the application is closed. But anyway, it is evil technique which leads to memory leaks when used not so carefully.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by davidk View Post
    It is not possible to separately initialize static class member in the global scope of code.
    Rubbish. It is possible to initialise a static class member at the point where it is defined. What is not allowed is doing that via a simple function call

    Code:
    class test
    {
    		
    private:
    
    static int* pointer;
    };
    
    // at file scope   aka global
    
    int x[3];
    int * test::pointer = x;   // or &x[0]
    What is not permitted, however, is a non-constructor function call for doing such an initialisation.

    The initialisation has to involve a constant known at compile time. There has been some discussion of relaxing this constraint slightly slightly in future versions of the standard.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    It is possible to initialise [...] versions of the standard.
    Rubbish.

    What you can't do is initialize a static constant integer member at the point it is declared within a class definition with a non-constant, non-literal expression.

    If the member type is non-integer, it can't be initialized at the point of declaration; it must be separately defined. (The member must be defined "outside" the class definition.)

    If the member is separately defined, the initialization expression need not be a constant, literal expression.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C#] Intercept SysListView32 item added
    By Devils Child in forum Windows Programming
    Replies: 9
    Last Post: 03-26-2010, 07:29 AM
  2. static class members
    By SterlingM in forum C++ Programming
    Replies: 4
    Last Post: 11-05-2009, 11:20 PM
  3. problem - pointer arrays to class
    By miroslav_karpis in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2009, 04:45 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM

Tags for this Thread