Re: Changing the class data underneath some reference

2017-11-29 Thread Steven Schveighoffer via Digitalmars-d-learn

On 11/29/17 10:22 PM, Jonathan M Davis wrote:

With classes, you could also assign the entire state of the object similar
to what you'd get with structs and opAssign, but you'd have to write a
member function to do it. There's no reason that you couldn't do the
equivalent of opAssign. It's just that there's no built-in operator for it.
So, whatever member function you wrote for it would be non-standard.
It's mostly a bad idea. The whole point of disallowing assignment is to 
prevent the slicing problem. It's also why opAssign isn't overloadable 
for classes assigning to classes in the same hierarchy.


-Steve


Re: Changing the class data underneath some reference

2017-11-29 Thread Jonathan M Davis via Digitalmars-d-learn
On Wednesday, November 29, 2017 21:12:58 Steven Schveighoffer via 
Digitalmars-d-learn wrote:
> On 11/29/17 7:40 PM, David Colson wrote:
> > Hello all!
> >
> > I'm getting settled into D and I came into a problem. A code sample
> > shows it best:
> >
> > class SomeType
> > {
> >
> >  string text;
> >  this(string input) {text = input;}
> >
> > }
> >
> >
> > void main()
> > {
> >
> >  SomeType foo = new SomeType("Hello");
> >
> >  SomeType bar = foo;
> >
> >  foo = new SomeType("World");
> >
> >  writeln(bar.text); // Prints hello
> >  // I'd like it to print World
> >
> > }
> >
> > In the C++ world I could do this using pointers and changing the data
> > underneath a given pointer, but I can't use pointers in D, so I'm not
> > sure how I can get this behaviour?
> >
> > I'd be open to other ways of achieving the same affect in D, using more
> > D like methods.
>
> D does not support reassigning class data using assignment operator,
> only the class reference. You can change the fields individually if you
> need to.
>
> e.g.:
>
> foo.text = "World";
>
> structs are value types in D and will behave similar to C++
> classes/structs.

With classes, you could also assign the entire state of the object similar
to what you'd get with structs and opAssign, but you'd have to write a
member function to do it. There's no reason that you couldn't do the
equivalent of opAssign. It's just that there's no built-in operator for it.
So, whatever member function you wrote for it would be non-standard. Heck,
technically, we don't even have a standard way to clone a class object like
C# or Java do. Some folks write a clone function and others write a dup
function; either way, there's nothing built-in or standard for it.

- Jonathan M Davis



Re: Changing the class data underneath some reference

2017-11-29 Thread A Guy With a Question via Digitalmars-d-learn

On Thursday, 30 November 2017 at 00:40:51 UTC, David Colson wrote:

Hello all!

I'm getting settled into D and I came into a problem. A code 
sample shows it best:


class SomeType
{
string text;
this(string input) {text = input;}
}


void main()
{
SomeType foo = new SomeType("Hello");

SomeType bar = foo;

foo = new SomeType("World");

writeln(bar.text); // Prints hello
// I'd like it to print World
}

In the C++ world I could do this using pointers and changing 
the data underneath a given pointer, but I can't use pointers 
in D, so I'm not sure how I can get this behaviour?


I'd be open to other ways of achieving the same affect in D, 
using more D like methods.


You are dealing with a reference type. Reference types can be 
though of as a value type of an address. The new operator can be 
though of as giving the variable a new address. This means the 
foo and bar variables are not bound to the same value because 
their referencing different address. You need a struct. Which 
isn't a reference.


Re: Changing the class data underneath some reference

2017-11-29 Thread Steven Schveighoffer via Digitalmars-d-learn

On 11/29/17 7:40 PM, David Colson wrote:

Hello all!

I'm getting settled into D and I came into a problem. A code sample 
shows it best:


class SomeType
{
     string text;
     this(string input) {text = input;}
}


void main()
{
     SomeType foo = new SomeType("Hello");

     SomeType bar = foo;

     foo = new SomeType("World");

     writeln(bar.text); // Prints hello
     // I'd like it to print World
}

In the C++ world I could do this using pointers and changing the data 
underneath a given pointer, but I can't use pointers in D, so I'm not 
sure how I can get this behaviour?


I'd be open to other ways of achieving the same affect in D, using more 
D like methods.


D does not support reassigning class data using assignment operator, 
only the class reference. You can change the fields individually if you 
need to.


e.g.:

foo.text = "World";

structs are value types in D and will behave similar to C++ classes/structs.

-Steve


Re: Changing the class data underneath some reference

2017-11-29 Thread codephantom via Digitalmars-d-learn

On Thursday, 30 November 2017 at 00:52:25 UTC, codephantom wrote:

...


sorry, don't know how the int * got in there ;-)

Anyway..who said you can't use pointers in D?

Just change:


//SomeType bar = foo;
SomeType * bar = 



Re: Changing the class data underneath some reference

2017-11-29 Thread codephantom via Digitalmars-d-learn

On Thursday, 30 November 2017 at 00:40:51 UTC, David Colson wrote:

Hello all!

I'm getting settled into D and I came into a problem. A code 
sample shows it best:


class SomeType
{
string text;
this(string input) {text = input;}
}


void main()
{
SomeType foo = new SomeType("Hello");

SomeType bar = foo;

foo = new SomeType("World");

writeln(bar.text); // Prints hello
// I'd like it to print World
}

In the C++ world I could do this using pointers and changing 
the data underneath a given pointer, but I can't use pointers 
in D, so I'm not sure how I can get this behaviour?


I'd be open to other ways of achieving the same affect in D, 
using more D like methods.



void main()
{
SomeType foo = new SomeType("Hello");

int * ptr;
SomeType * bar;
bar = 

foo = new SomeType("World");

writeln(bar.text); // Prints World

}



Re: Changing the class data underneath some reference

2017-11-29 Thread David Colson via Digitalmars-d-learn

On Thursday, 30 November 2017 at 00:40:51 UTC, David Colson wrote:

Hello all!

I'm getting settled into D and I came into a problem. A code 
sample shows it best:


class SomeType
{
string text;
this(string input) {text = input;}
}


void main()
{
SomeType foo = new SomeType("Hello");

SomeType bar = foo;

foo = new SomeType("World");

writeln(bar.text); // Prints hello
// I'd like it to print World
}

In the C++ world I could do this using pointers and changing 
the data underneath a given pointer, but I can't use pointers 
in D, so I'm not sure how I can get this behaviour?


I'd be open to other ways of achieving the same affect in D, 
using more D like methods.


I made an example demonstrating what I'd do in C++:

class SomeType
{
public:
std::string text;
SomeType(std::string input) {text = input;}
};


int main()
{
SomeType foo = SomeType("Hello");

SomeType* bar = 

foo = SomeType("World");

std::cout << bar->text << "\n"; // Prints World
}



Changing the class data underneath some reference

2017-11-29 Thread David Colson via Digitalmars-d-learn

Hello all!

I'm getting settled into D and I came into a problem. A code 
sample shows it best:


class SomeType
{
string text;
this(string input) {text = input;}
}


void main()
{
SomeType foo = new SomeType("Hello");

SomeType bar = foo;

foo = new SomeType("World");

writeln(bar.text); // Prints hello
// I'd like it to print World
}

In the C++ world I could do this using pointers and changing the 
data underneath a given pointer, but I can't use pointers in D, 
so I'm not sure how I can get this behaviour?


I'd be open to other ways of achieving the same affect in D, 
using more D like methods.