Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
December 03, 2009 override toString | ||||
---|---|---|---|---|
| ||||
Hi All, I want to print some object information for debugging. But the name of class is incorrect. I do not know why. module test; class A { char[] data; public char[] toString() { return "<" + this.classinfo.name + ": " + data + ">"; } } class B: A { char[] data2; public override char[] toString() { return "<" + this.classinfo.name + ": " + super.toString + ", " + data2 + ">"; } } auto b = new B; b.data = "hello"; b.data2 = "world"; Cout(b.toString); // <test.B: <test.B: hello>, world> The expected result should be: <test.B: <test.A: hello>, world> But the actual result is: <test.B: <test.B: hello>, world> |
December 03, 2009 Re: override toString | ||||
---|---|---|---|---|
| ||||
Posted in reply to Qian Xu | Hello Qian, > Hi All, > > I want to print some object information for debugging. But the name of > class is incorrect. I do not know why. > > module test; > > class A { > char[] data; > public char[] toString() { > return "<" + this.classinfo.name + ": " + data + ">"; > } > } > class B: A { > char[] data2; > public override char[] toString() { > return "<" + this.classinfo.name + ": " + super.toString + ", " + > data2 > + ">"; > } > } > auto b = new B; > b.data = "hello"; > b.data2 = "world"; > Cout(b.toString); // <test.B: <test.B: hello>, world> > The expected result should be: > <test.B: <test.A: hello>, world> > But the actual result is: > <test.B: <test.B: hello>, world> use typeof(this).classinfo.name to get type of A. "this" returns runtime type of instance, "typeof(this)" return static type. http://www.digitalmars.com/d/1.0/expression.html btw. I noticed that you are using "+" for string concatenation, how is possible that your program even comiples??? "~" should be used for string concatenation. |
December 03, 2009 Re: override toString | ||||
---|---|---|---|---|
| ||||
Posted in reply to Qian Xu | On Thu, 03 Dec 2009 11:01:29 +0100, Qian Xu <qian.xu@funkwerk-itk.com> wrote:
>Hi All,
>
>I want to print some object information for debugging. But the name of class is incorrect. I do not know why.
>
>
>module test;
>
>class A {
> char[] data;
> public char[] toString() {
> return "<" + this.classinfo.name + ": " + data + ">";
> }
>}
>
>class B: A {
> char[] data2;
> public override char[] toString() {
> return "<" + this.classinfo.name + ": " + super.toString + ", " + data2
>+ ">";
> }
>}
>
>auto b = new B;
>b.data = "hello";
>b.data2 = "world";
>Cout(b.toString); // <test.B: <test.B: hello>, world>
>
>
>The expected result should be:
><test.B: <test.A: hello>, world>
>
>But the actual result is:
><test.B: <test.B: hello>, world>
>
>
this.classinfo can be seen as a virtual function returning the
classinfo for the actual class instance and typeof(this).classinfo -
as a static function returning the classinfo of the compile-time type
of this (that is the class where it is called). So, your example
should be rewritten (D2):
import std.stdio;
class A {
string data;
public string toString() {
return "<" ~ typeof(this).classinfo.name ~ ": " ~ data ~ ">";
}
}
class B: A {
string data2;
public override string toString() {
return "<" ~ typeof(this).classinfo.name ~ ": " ~ super.toString ~
", " ~ data2 ~ ">";
}
}
void main()
{
auto b = new B;
b.data = "hello";
b.data2 = "world";
writeln(b.toString);
}
|
December 03, 2009 Re: override toString | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michal Minich | Michal Minich wrote:
>
> btw. I noticed that you are using "+" for string concatenation, how is possible that your program even comiples??? "~" should be used for string concatenation.
sorry, it was my type error. the code is not real ^^)
|
December 03, 2009 Re: override toString | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samukha | Max Samukha wrote:
>
> this.classinfo can be seen as a virtual function returning the
> classinfo for the actual class instance and typeof(this).classinfo -
> as a static function returning the classinfo of the compile-time type
> of this (that is the class where it is called). So, your example
> should be rewritten (D2):
>
Thanks. You made my day
|
Copyright © 1999-2021 by the D Language Foundation