using a member const int to initialize a char array

A

abubakarm

Hi,

If i have the following struct:

struct t
{
const int x;// = 0;
char name [ x ];

};

its gives an error saying that x is undefined. So I want to initialize
x and use it to initalize other members. How do I do this? Or should I
use some other way?

Thanks,

...ab
 
L

Leandro Melo

Hi,

If i have the following struct:

struct t
{
  const int x;// = 0;
  char name [ x ];

};

its gives an error saying that x is undefined. So I want to initialize
x and use it to initalize other members. How do I do this? Or should I
use some other way?

Thanks,

..ab

Hi.

You can use the following (don't use 0 however):

struct A
{
static const int x = 10;
char name [ x ];

};
 
A

abubakarm

Leandro said:
Hi,
If i have the following struct:
struct t
{
  const int x;// = 0;
  char name [ x ];
};
its gives an error saying that x is undefined. So I want to initialize
x and use it to initalize other members. How do I do this? Or should I
use some other way?
Thanks,
..ab

You can use the following (don't use 0 however):
struct A
{
  static const int x = 10;
  char name [ x ];

If the value of 'x' is dynamic (determined only in run-time), then you
can't use this.  You need to either use a dynamic array or a std::vector
for your 'name':

     struct A
     {
         const int x;
         std::vector<char> name;
         A(int x) : x(x), name(x) {}
     };

V

Thank you guys !

..ab
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top