Jump to page: 1 2
Thread overview
writeln an object
Apr 18, 2013
gedaiu
Apr 18, 2013
JN
Apr 18, 2013
gedaiu
Apr 18, 2013
Andrej Mitrovic
Apr 18, 2013
John Colvin
Apr 18, 2013
gedaiu
Apr 18, 2013
David
Apr 18, 2013
John Colvin
Apr 18, 2013
Ali Çehreli
Apr 19, 2013
gedaiu
Apr 19, 2013
Ali Çehreli
April 18, 2013
Hi,

how i can control what writeln outputs when I pass an object parameter?

Thanks,
Bogdan
April 18, 2013
On Thursday, 18 April 2013 at 17:36:10 UTC, gedaiu wrote:
> Hi,
>
> how i can control what writeln outputs when I pass an object parameter?
>
> Thanks,
> Bogdan

You can override the toString() method, like this http://dpaste.dzfl.pl/db7dbe28
April 18, 2013
On Thursday, 18 April 2013 at 17:42:53 UTC, JN wrote:
> On Thursday, 18 April 2013 at 17:36:10 UTC, gedaiu wrote:
>> Hi,
>>
>> how i can control what writeln outputs when I pass an object parameter?
>>
>> Thanks,
>> Bogdan
>
> You can override the toString() method, like this http://dpaste.dzfl.pl/db7dbe28

i've done that but i get this error:

Error: function base.Value.Value.toString cannot override a non-virtual function
Error: function base.Value.Value.toString override only applies to class member functions
April 18, 2013
On 4/18/13, gedaiu <szabobogdan@yahoo.com> wrote:
> i've done that but i get this error:
>
> Error: function base.Value.Value.toString cannot override a
> non-virtual function
> Error: function base.Value.Value.toString override only applies
> to class member functions
>

If it's a struct then don't put "override".
April 18, 2013
On Thursday, 18 April 2013 at 18:04:03 UTC, Andrej Mitrovic wrote:
> On 4/18/13, gedaiu <szabobogdan@yahoo.com> wrote:
>> i've done that but i get this error:
>>
>> Error: function base.Value.Value.toString cannot override a
>> non-virtual function
>> Error: function base.Value.Value.toString override only applies
>> to class member functions
>>
>
> If it's a struct then don't put "override".

Just to provide a bit more info:

Classes all derive from Object, which defines toString. Hence, you need to override it to define your own.

Structs don't have a parent (or any inheritance at all) and hence you don't override anything, you just define the method.
April 18, 2013
On Thursday, 18 April 2013 at 18:25:21 UTC, John Colvin wrote:
> On Thursday, 18 April 2013 at 18:04:03 UTC, Andrej Mitrovic wrote:
>> On 4/18/13, gedaiu <szabobogdan@yahoo.com> wrote:
>>> i've done that but i get this error:
>>>
>>> Error: function base.Value.Value.toString cannot override a
>>> non-virtual function
>>> Error: function base.Value.Value.toString override only applies
>>> to class member functions
>>>
>>
>> If it's a struct then don't put "override".
>
> Just to provide a bit more info:
>
> Classes all derive from Object, which defines toString. Hence, you need to override it to define your own.
>
> Structs don't have a parent (or any inheritance at all) and hence you don't override anything, you just define the method.

i'm realy sorry... it's my mistake...

i have a struct not an object. I have someting like this when i get the error:

struct Value {
	string strVal;

        this(string val) {
		strVal = val;
	}

        override string toString() {
		return strVal;
	}
}
April 18, 2013
Just drop the override:

> struct Value {
>     string strVal;
> 
>         this(string val) {
>         strVal = val;
>     }
> 
>         override string toString() {
>         return strVal;
>     }
> }


struct Value {
    string strVal;

        this(string val) {
        strVal = val;
    }

    string toString() {
        return strVal;
    }
}
April 18, 2013
On Thursday, 18 April 2013 at 18:46:09 UTC, gedaiu wrote:
> i have a struct not an object.

There's a slight nomenclature clash here:

Object is the base class in D. Therefore one could say that an object is an instatiation of Object and therefore a class.

However, by a wider definition of the word, a struct could also be said to be an object.
April 18, 2013
On 04/18/2013 12:37 PM, John Colvin wrote:

> However, by a wider definition of the word, a struct could also be said
> to be an object.

You are missing some words there. :) Not a struct itself, but instances of it are said to be objects.

Ali

April 19, 2013
Ok, i understand now the diference...

my question is how I should solve this problem?

Thanks,
Bogdan

On Thursday, 18 April 2013 at 20:57:23 UTC, Ali Çehreli wrote:
> On 04/18/2013 12:37 PM, John Colvin wrote:
>
> > However, by a wider definition of the word, a struct could
> also be said
> > to be an object.
>
> You are missing some words there. :) Not a struct itself, but instances of it are said to be objects.
>
> Ali

« First   ‹ Prev
1 2