Errors, bugs, questions - page 2721

 
Koldun Zloy:

It doesn't make sense. If you're not going to change it, that doesn't mean you have to make const. It is enough to make the variable private.

It would be logical if you would get something out of it, but you've only created a problem for yourself.

const is an additional control. Otherwise, you wouldn't have to introduce this directive into the language at all.


The good thing about a public const field is that it is READ-accessible (can be passed by reference). But mistakenly changing it is ruled out.


const helps me a lot when writing programs. This is the first time a problem has arisen.


It also helps to use this and :: where they can't be written.

 
fxsaber:

const is an additional control. Otherwise, we could have avoided introducing this directive into the language altogether.


A public const field is good in that it is READ-able (can be passed by reference). But mistakenly changing it is ruled out.


const helps me a lot when writing programs. This is the first time a problem has arisen.


HH It also helps to use this and :: where they can't be written.

Make the variable private. Use a function for reading. There is no need to pass const int by reference.

 
Koldun Zloy:

Make the variable private. Use a function to read it. There is no need to pass const int by reference.

You propose to simply ignore const.

 
fxsaber:

You are suggesting that const is simply ignored.

No. But it is not needed in this particular case.

 
Man, you make up a problem out of nothing."The mice cried and cried but kept on biting the cactus".
 
fxsaber:

I experimented, in general any solution will be "flawed" no matter how you look at it

If we are talking about speed of execution and controlling access to the structure field, you can write an ugly solution, but it will solve the problem

I would give up static methods in the structure altogether - there is no sense in them, the code will still be confusing and unlogical

it looks like this:

int tmp[] = {1,2,3,99};
struct A
{
private:
   static int count;
public:
   const int a;
   A():a(tmp[count++]) {}
};
static int A::count = 0;
//+------------------------------------------------------------------+
void OnStart()
{
   A a[ArraySize(tmp)];
   for(int i=0;i<ArraySize(a);i++) Print(a[i].a);
}


....da and the count counter is also not needed, you need, in fact, to wrap A[ArraySize(tmp)]; in a macro substitution that will create an array of structures and delete a temporary array tmp

 

There are two programs working on the same file at the same time. The one that writes uses theFILE_READ|FILE_WRITE|FILE_BIN|FILE_SHARE_READ flags. The one that reads - FILE_READ|FILE_BIN|FILE_SHARE_WRITE|FILE_SHARE_READ. Although the first program periodically saves the file using FileFlush, the second program only sees the length of the file when it is opened. Tried doing FileSeek back and forth - doesn't help.

Question: how do I make the program that reads the file pick up the data to be overwritten?

 

I'll summarise:

struct A {
    const int a; 
    A() {}
};
void OnStart()
{
            A a1; //(1) нормально
    const int a2; //(2) Error: 'a2' - 'const' variable must be initialized
}

What's the difference between 1 and 2 ?

 
A100:

I'll summarise:

What is the difference between 1 and 2 ?

I was hoping that there was some inbuilt mechanism to initialise this case, but there's nothing there

struct A {
    const int a[10]; 
    A() {ArrayPrint(a);} //-396985669       32758 -1490812928       32757  2147360768           0 -1681390008         493           0           0

};
//+------------------------------------------------------------------+
void OnStart()
{
  A a1;
}
//+------------------------------------------------------------------+

ZS: this is where the compiler sees

void f()
{
   const int x;  //'x' - 'const' variable must be initialized
}
 
Igor Makanu:

I had hoped that there was some mechanism built in to initialise this case, but there's nothing there

ZS: here the compiler sees

In principle there shouldn't be such a case - the issue should have been solved already at the compiler level (as in C++). And here it's kind of possible and as a consequence, a discussion for several pages