Solved

constant trouble.

Posted on 2004-08-04
4
195 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

914 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

15 Experts available now in Live!

Get 1:1 Help Now