Solved

constant trouble.

Posted on 2004-08-04
4
194 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 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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++.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now