Solved

constant trouble.

Posted on 2004-08-04
4
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 125 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

Industry Leaders: 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

737 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