Re: Modify const reference data

2013-12-09 Thread Heinz
Apparently the OP intended to set it in foo(). If the data is 
actually mutable and there really is no way other than going 
against the type system, foo() must be called at least once and 
can be implemented like this:


public void foo()
{
abc = [1,2,3,4];
cast(ubyte*)data = abc.ptr;
}

// ...

B b = new B();
b.foo();
b.print(); // now OK

Ali


Wow, i didn't know i could cast out constness from an lvalue. 
This is what i needed. I'll try it in my code as soon as i can. 
Thanks.


Re: Modify const reference data

2013-12-09 Thread Heinz
Wow, i didn't know i could cast out constness from an lvalue. 
This is what i needed. I'll try it in my code as soon as i can. 
Thanks.


Yep, it compiles and works! By the way, i forgot to call b.foo() 
in my example. Thanks for your help guys.


Modify const reference data

2013-12-08 Thread Heinz

[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.


Re: Modify const reference data

2013-12-08 Thread Heinz
Duhhh! i got the pink avatar by default. That sucks. Pink is just 
not my color.


Re: Modify const reference data

2013-12-08 Thread Adam D. Ruppe

Easy problem in class B: data is null!

On Monday, 9 December 2013 at 02:53:01 UTC, Heinz wrote:

class B
{
private const ubyte* data;
private ubyte[] abc;

this()
{
data = cast(const ubyte*)abc.ptr;
}



Since abc isn't initialized in this constructor, abc.ptr is null. 
So data is null too.




public void print()
{
for(size_t i = 0; i  4; i++)
{
writefln(%d, data[i]);
}
}
}



And since data is null, data[i] will be a memory 
err/segfault/access violation/whatever it is called.


Re: Modify const reference data

2013-12-08 Thread Ali Çehreli

On 12/08/2013 07:24 PM, Adam D. Ruppe wrote:

Easy problem in class B: data is null!

On Monday, 9 December 2013 at 02:53:01 UTC, Heinz wrote:

class B
{
private const ubyte* data;
private ubyte[] abc;

this()
{
data = cast(const ubyte*)abc.ptr;
}



Since abc isn't initialized in this constructor, abc.ptr is null. So
data is null too.



public void print()
{
for(size_t i = 0; i  4; i++)
{
writefln(%d, data[i]);
}
}
}



And since data is null, data[i] will be a memory err/segfault/access
violation/whatever it is called.


Apparently the OP intended to set it in foo(). If the data is actually 
mutable and there really is no way other than going against the type 
system, foo() must be called at least once and can be implemented like this:


public void foo()
{
abc = [1,2,3,4];
cast(ubyte*)data = abc.ptr;
}

// ...

B b = new B();
b.foo();
b.print(); // now OK

Ali