Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

constant trouble.

Posted on 2004-08-04
4
Medium Priority
?
205 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 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

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

721 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