Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

constant trouble.

Posted on 2004-08-04
4
Medium Priority
?
208 Views
Last Modified: 2010-04-01
Hi,
I wish to have an array that will remain constant in my class.

eg.

class A
{
  private:
      const BYTE myData[] = {0x1,0x1,0x1,0x1,0x1};
}


..As you may guess, the compiler throws a syntax error at the '{'.

Whats the best way to achieve this?

TIA

David.
0
Comment
Question by:dave_p_r_b
  • 3
4 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 375 total points
ID: 11715325
You should make this a static variable.
Example:
class A
{
 private:
      static const BYTE myData[5];
};

//In your cpp file
const BYTE A:myData[5] = {0x1,0x1,0x1,0x1,0x1};

0
 
LVL 30

Expert Comment

by:Axter
ID: 11715371
Correction:
//In your cpp file
const BYTE A::myData[5] = {0x1,0x1,0x1,0x1,0x1};

Since you want this to be a constant variable that is set at compile time, there really is no need for each instance of your object to carry an instance of the constant data.
And in fact it would make your code less efficient if you used a non-static data member.

By making it static, you can then initialize it in your *.cpp file.

continue....
0
 
LVL 30

Expert Comment

by:Axter
ID: 11715437
Alternate method:

class A
{
 private:
     const BYTE* GetMyData()
     {
          const BYTE myData[] = {0x1,0x1,0x1,0x1,0x1};
          return myData;
     }
};

You can use above code as in following:
BYTE xx = GetMyData()[2];
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11728054
Another method (if you don't need sizeof(myData) in your prog)

class A
{
  private:
      const BYTE* myData ;
  public:
      const BYTE* getMyData()
      {  
            BYTE* pData = new BYTE[5];
            for (int i = 0; i < 5; i++)
                  pData[i] = 0x01;
            return pData;
      }
      A() :  myData(getMyData()) {}      
};

However, if all instances have the same data, Axter's static member is the best solution.

Regards, Alex



0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question