Code fails with linker error. Why?
John Colvin via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Oct 4 08:29:55 PDT 2014
On Saturday, 4 October 2014 at 11:19:52 UTC, ketmar via
Digitalmars-d-learn wrote:
> On Sat, 04 Oct 2014 11:01:28 +0000
> John Colvin via Digitalmars-d-learn
> <digitalmars-d-learn at puremagic.com>
> wrote:
>
>> On Saturday, 4 October 2014 at 10:38:32 UTC, ketmar via
>> Digitalmars-d-learn wrote:
>> > On Sat, 04 Oct 2014 10:27:16 +0000
>> > John Colvin via Digitalmars-d-learn
>> > <digitalmars-d-learn at puremagic.com>
>> > wrote:
>> >
>> >> Sorry, but that's just not how it works. There is no
>> >> requirement for the definition of a function to be found in
>> >> the same compilation unit as it's declaration.
>> > is there any possibility to declare *class* *method* in one
>> > file and to
>> > implement it in another? O_O
>> >
>> > i doubt so.
>>
>> Yes, you can. You just have to get the mangling right.
> and how about access to class fields? manual mangling is a
> half-working
> hack. alas. i really want to have some normal and non-hackish
> way to
> separate class definition and class implementation. besides,
> your trick
> is faulty, 'cause it misses hidden 'this' parameter. try that:
>
> // methodLink.d
> class A { void foo(int n); }
> void main () { auto a = new A; a.foo(42); }
>
> // missingMethod.d
> import methodLink;
> import std.stdio;
>
> pragma(mangle, A.foo.mangleof)
> void foo(int n) { writeln("n=", n); }
>
> it writes gibberish and segfaults.
good catch. This works for me, but it's probably not portable due
to variations in where the hidden this is passed:
//methodLink
import std.stdio;
class Base
{
int baseVal;
void foo(int a) { writeln("baseVal + a: ", baseVal + a); }
}
class A : Base
{
int aVal;
this(int v) { aVal = v; baseVal = 2*v; }
override void foo(int a);
}
void main()
{
Base a = new A(42);
a.foo(3);
a.Base.foo(3);
}
// missingMethod.d
import methodLink;
import std.stdio;
pragma(mangle, A.foo.mangleof)
void foo(int a, A this_) { writeln("aVal + a: ", this_.aVal + a);
}
$./methodLink
aVal + a: 45
baseVal + a: 87
I don't really see the point though.
class A
{
void foo(int a) { Afoo(this, a); }
}
then declare and define Afoo however you like.
More information about the Digitalmars-d-learn
mailing list