8 Replies - 902 Views - Last Post: 28 February 2010 - 03:02 AM Rate Topic: -----

#1 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Problem with private static array

Posted 28 February 2010 - 01:57 AM

Hello. I got a problem with static bool array declared in private. The following code works fine:
#include <new>
#include <list>
#include <ctime>
#include <iostream>
#include <exception>

typedef unsigned long long ulong;

using namespace std;

class SieveOfAtkin
{
	private :
		ulong Limit;
		bool *IsPrime;
		list<ulong> Primes;
		
	public :
		SieveOfAtkin() { }
		SieveOfAtkin(ulong Limit)
		{
			this->Limit = Limit;
			
			try
			{
				IsPrime = new bool[Limit];
			}
			catch(bad_alloc&)
			{
				cerr << "\n  Memory allocation unsuccessful." << endl;
			}
		}
		
		void Display();
		
};

void SieveOfAtkin::Display()
{
	for(ulong i=0;i<Limit;i++)
		cout<<IsPrime[i]<<" ";
}

int main ()
{
	SieveOfAtkin Object = SieveOfAtkin(10000000LLU);
	Object.Display();
	return 0;
}

But if I consider IsPrime array as static I get error. How can I do the same job declaring IsPrime as static? I have to use aforesaid try and catch block. Please help.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with private static array

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Problem with private static array

Posted 28 February 2010 - 02:02 AM

Static member variables can be called without instantiating the class. What does that mean? They are independent, they do not depend on that class. They can be accessed like normal global variables. Static member functions can ONLY access static member variables. Non-static/static member variables can be accessed by non-static member functions. Just because static member veriables acts like global variables, they need separate instantiation, outside the class. In your case, I'd do something like this:
bool *SieveOfAtkin::IsPrime;


(add this line right under your class implementation)

This post has been edited by sarmanu: 28 February 2010 - 02:39 AM

Was This Post Helpful? 1
  • +
  • -

#3 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: Problem with private static array

Posted 28 February 2010 - 02:06 AM

View Postsarmanu, on 28 February 2010 - 01:02 AM, said:

Static member variables can be called without instantiating the class. What does that mean? They are independent, they do not depend on that class. They can be accessed like normal global variables, and they can only be used by static member functions. For that, they need separate instantiation, outside the class. In your case, I would probably do this:
bool *SieveOfAtkin::IsPrime;


(add this line right under your class implementation)

Where should I declare try and catch?
This way :
#include <new>
#include <list>
#include <ctime>
#include <iostream>
#include <exception>

typedef unsigned long long ulong;

using namespace std;

class SieveOfAtkin
{
	private :
		ulong Limit;
		static bool *IsPrime;
		list<ulong> Primes;
		
	public :
		SieveOfAtkin() { }
		SieveOfAtkin(ulong Limit)
		{
			this->Limit = Limit;
			
			try
			{
				IsPrime = new bool[Limit];
			}
			catch(bad_alloc&)
			{
				cerr << "\n  Memory allocation unsuccessful." << endl;
			}
		}
		
		void Display();
		
};

bool *SieveOfAtkin::IsPrime;

void SieveOfAtkin::Display()
{
	for(ulong i=0;i<Limit;i++)
		cout<<IsPrime[i]<<" ";
}

int main ()
{
	SieveOfAtkin Object = SieveOfAtkin(100000000000000000LLU);
	//Object.Display();
	return 0;
}
??

This post has been edited by Tapas Bose: 28 February 2010 - 02:09 AM

Was This Post Helpful? 0
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Problem with private static array

Posted 28 February 2010 - 02:09 AM

It's OK where it is. Just declare isPrime as static, and add the line I told you, and it should work.
EDIT:
100000000000000000LLU


The suffix should be "ULL" not "LLU"

This post has been edited by sarmanu: 28 February 2010 - 02:10 AM

Was This Post Helpful? 0
  • +
  • -

#5 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: Problem with private static array

Posted 28 February 2010 - 02:16 AM

View Postsarmanu, on 28 February 2010 - 01:09 AM, said:

It's OK where it is. Just declare isPrime as static, and add the line I told you, and it should work.
EDIT:
100000000000000000LLU


The suffix should be "ULL" not "LLU"

Okay. It works fine. Thank you very much.

Static variable can only be accessed by static member function. But the display function can display IsPrime. How it is possible?
Was This Post Helpful? 0
  • +
  • -

#6 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Problem with private static array

Posted 28 February 2010 - 02:36 AM

Sorry, I have explained half of problem only. Edited the post:)
EDIT: my engglish may not be very good, so I'd suggest you to read here: http://publib.boulde...rc13cplr039.htm and here http://www.learncpp....mber-functions/

This post has been edited by sarmanu: 28 February 2010 - 02:40 AM

Was This Post Helpful? 0
  • +
  • -

#7 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: Problem with private static array

Posted 28 February 2010 - 02:56 AM

View Postsarmanu, on 28 February 2010 - 01:36 AM, said:

Sorry, I have explained half of problem only. Edited the post:)
EDIT: my engglish may not be very good, so I'd suggest you to read here: http://publib.boulde...rc13cplr039.htm and here http://www.learncpp....mber-functions/

Thank you very much. I think you want to mean that static variables are accessible by static member function as well as non-static member function but static member function can only access static variable, it can not access non-static variable. Am I wrong?
Was This Post Helpful? 0
  • +
  • -

#8 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Problem with private static array

Posted 28 February 2010 - 02:57 AM

View PostTapas Bose, on 28 February 2010 - 11:56 AM, said:

View Postsarmanu, on 28 February 2010 - 01:36 AM, said:

Sorry, I have explained half of problem only. Edited the post:)
EDIT: my engglish may not be very good, so I'd suggest you to read here: http://publib.boulde...rc13cplr039.htm and here http://www.learncpp....mber-functions/

Thank you very much. I think you want to mean that static variables are accessible by static member function as well as non-static member function but static member function can only access static variable, it can not access non-static variable. Am I wrong?

You are absolutely right :) Glad you understood the concept so fast.

This post has been edited by sarmanu: 28 February 2010 - 02:58 AM

Was This Post Helpful? 1
  • +
  • -

#9 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: Problem with private static array

Posted 28 February 2010 - 03:02 AM

View Postsarmanu, on 28 February 2010 - 01:57 AM, said:

View PostTapas Bose, on 28 February 2010 - 11:56 AM, said:

View Postsarmanu, on 28 February 2010 - 01:36 AM, said:

Sorry, I have explained half of problem only. Edited the post:)
EDIT: my engglish may not be very good, so I'd suggest you to read here: http://publib.boulde...rc13cplr039.htm and here http://www.learncpp....mber-functions/

Thank you very much. I think you want to mean that static variables are accessible by static member function as well as non-static member function but static member function can only access static variable, it can not access non-static variable. Am I wrong?

You are absolutely right :) Glad you understood the concept so fast.

Thank you very much.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1