[DMD 2.064.2]

Hello,

I've been strugling with a solution before bothering you guys here (again). I have my own complex code but i made a VERY simple test case to reproduce my problem, here's the code:

private import std.stdio;

class A
{
        private const ubyte* data;
        private ubyte[] abc;
        
        this()
        {
                abc = [1,2,3,4];
                data = cast(const ubyte*)abc.ptr;
        }
        
        public void print()
        {
                for(size_t i = 0; i < 4; i++)
                {
                        writefln("%d", data[i]);
                }
        }
}

class B
{
        private const ubyte* data;
        private ubyte[] abc;
        
        this()
        {
                data = cast(const ubyte*)abc.ptr;
        }
        
        public void foo()
        {
                abc = [1,2,3,4];
        }
        
        public void print()
        {
                for(size_t i = 0; i < 4; i++)
                {
                        writefln("%d", data[i]);
                }
        }
}

void main()
{
        A a = new A();
        a.print(); // OK.
        
        B b = new B();
        b.print(); // Crash.
}

The thing is that i can not use the data that const variable "data" in class B is referencing because in my original code i get "Invalid Memory Operation", in the test case i get "access violation" but it doesn't matter the name of the exception, the only thing that matters is that the exception raises for the same reason. I understand that i should not modify the const data outside a constructor but by stricts reason i can't avoid that.

Do you guys have any other aproach to get away with this? (modify a const reference data and then manipulate that data). THANK YOU very much in advance. D2 learner by the way.

Reply via email to