Jump to page: 1 2
Thread overview
Pointer to member variable again
Jul 29, 2008
ws
Jul 29, 2008
Carl Clark
Jul 29, 2008
ws
Jul 29, 2008
Simen Kjaeraas
Jul 30, 2008
ws
Jul 30, 2008
Koroskin Denis
Jul 29, 2008
Kirk McDonald
Jul 31, 2008
Kirk McDonald
July 29, 2008
Suppose i have the following:

int function() fp;
int *ptr;
class Cls
{
 int k;
 int foo()  {  return 0;  }
}

void main()
{
 fp = &Cls.foo;

 Cls c = new Cls;
 ptr = &c.k;  // <-- why always need a new instance?
}

As compared to delegate, is there no analogous way to specify this?

ptr = &Cls.k

Thanks!
July 29, 2008
Shouldn't this work?

//...
class Cls
{
  int k;
  static int foo() { return 0; }
}
//...

The deal is that int foo() by itself is an instance method, but static declares it a class method, which is what you want -- class methods are shared, so you can simply call (or dereference) them without instances.

On 2008-07-28 19:45, ws spoke thusly:
> Suppose i have the following:
> 
> int function() fp;
> int *ptr;
> class Cls
> {
>  int k;
>  int foo()  {  return 0;  }
> }
> 
> void main()
> {
>  fp = &Cls.foo;
> 
>  Cls c = new Cls;
>  ptr = &c.k;  // <-- why always need a new instance?
> }
> 
> As compared to delegate, is there no analogous way to specify this?
> 
> ptr = &Cls.k
> 
> Thanks!
July 29, 2008
Maybe i should have written it this way:

int function() fp;
int delegate() dp;
int *ptr;
class Cls
{
  int k;
  int foo()  { return k; }
}

void main()
{
  fp = &Cls.foo;
  assert(fp() == 0);

  Cls c = new Cls;
  dp = &c.foo;
  ptr = &c.k;

  assert(is(typeof(ptr) == int*));
  assert(is(typeof(fp) == int function()));
  assert(is(typeof(dp) == int delegate()));
}

Notice fp can be used without an instance of Cls.
What i need is actually a way to get the type of the class when i reference the variable k, without actually creating an instance of the class.
Something like this:

typedef GetClass!(Cls.k) classType;
assert(is(typeof(classtype) == class) && is(typeof(classtype) == Cls));

Is there a way to do it?
July 29, 2008
On Tue, 29 Jul 2008 07:33:51 +0200, ws <wisiong@gmail.com> wrote:

> Maybe i should have written it this way:
>
> int function() fp;
> int delegate() dp;
> int *ptr;
> class Cls
> {
>   int k;
>   int foo()  { return k; }
> }
>
> void main()
> {
>   fp = &Cls.foo;
>   assert(fp() == 0);
>
>   Cls c = new Cls;
>   dp = &c.foo;
>   ptr = &c.k;
> 	
>   assert(is(typeof(ptr) == int*));
>   assert(is(typeof(fp) == int function()));
>   assert(is(typeof(dp) == int delegate()));
> }
>
> Notice fp can be used without an instance of Cls.
> What i need is actually a way to get the type of the class when i reference the variable k, without actually creating an instance of the class.
> Something like this:
>
> typedef GetClass!(Cls.k) classType;
> assert(is(typeof(classtype) == class) && is(typeof(classtype) == Cls));
>
> Is there a way to do it?

Have you tried calling fp in that example?

-- 
Simen
July 29, 2008
"ws" wrote
> Suppose i have the following:
>
> int function() fp;
> int *ptr;
> class Cls
> {
> int k;
> int foo()  {  return 0;  }
> }
>
> void main()
> {
> fp = &Cls.foo;
>
> Cls c = new Cls;
> ptr = &c.k;  // <-- why always need a new instance?
> }
>
> As compared to delegate, is there no analogous way to specify this?
>
> ptr = &Cls.k
>
> Thanks!

In fact, this should fail to compile.  The fact that it succeeds is a bug. You should enter it in bugzilla (is it already there?)

Did you try running it?  I get a segfault.

fp is a member function, which means it needs a hidden 'this' pointer.  When you assign &Cls.foo to fp, this should result in the same error as if you typed Cls.foo():

Error: need 'this' to access member foo

-Steve


July 29, 2008
"Steven Schveighoffer" wrote
> fp is a member function, which means it needs a hidden 'this' pointer.

Meant to write "foo is a member function..."

Sorry,

-STeve


July 29, 2008
Steven Schveighoffer wrote:
> "ws" wrote
> 
>>Suppose i have the following:
>>
>>int function() fp;
>>int *ptr;
>>class Cls
>>{
>>int k;
>>int foo()  {  return 0;  }
>>}
>>
>>void main()
>>{
>>fp = &Cls.foo;
>>
>>Cls c = new Cls;
>>ptr = &c.k;  // <-- why always need a new instance?
>>}
>>
>>As compared to delegate, is there no analogous way to specify this?
>>
>>ptr = &Cls.k
>>
>>Thanks!
> 
> 
> In fact, this should fail to compile.  The fact that it succeeds is a bug. You should enter it in bugzilla (is it already there?)
> 
> Did you try running it?  I get a segfault.
> 
> fp is a member function, which means it needs a hidden 'this' pointer.  When you assign &Cls.foo to fp, this should result in the same error as if you typed Cls.foo():
> 
> Error: need 'this' to access member foo
> 
> -Steve 
> 
> 

A direct, C++-style pointer-to-member-function is occasionally useful. (Though rarely, since delegates are more useful nearly every time.) Allowing this syntax is the only sensible way to get access to them. The resulting pointer can then manually be shoved into a delegate:

void function() fp = &Cls.foo;
Cls c = new Cls;
void delegate() dg;
dg.funcptr = fp;
dg.ptr = c; // I forget if a cast to void* is necessary...
dg();

Pyd relies on this behavior to implement its class wrapping, and it would be fairly inconvenient if it went away.

-- 
Kirk McDonald
http://kirkmcdonald.blogspot.com
Pyd: Connecting D and Python
http://pyd.dsource.org
July 29, 2008
"Kirk McDonald" wrote
> Steven Schveighoffer wrote:
>> "ws" wrote
>>
>>>Suppose i have the following:
>>>
>>>int function() fp;
>>>int *ptr;
>>>class Cls
>>>{
>>>int k;
>>>int foo()  {  return 0;  }
>>>}
>>>
>>>void main()
>>>{
>>>fp = &Cls.foo;
>>>
>>>Cls c = new Cls;
>>>ptr = &c.k;  // <-- why always need a new instance?
>>>}
>>>
>>>As compared to delegate, is there no analogous way to specify this?
>>>
>>>ptr = &Cls.k
>>>
>>>Thanks!
>>
>>
>> In fact, this should fail to compile.  The fact that it succeeds is a bug. You should enter it in bugzilla (is it already there?)
>>
>> Did you try running it?  I get a segfault.
>>
>> fp is a member function, which means it needs a hidden 'this' pointer. When you assign &Cls.foo to fp, this should result in the same error as if you typed Cls.foo():
>>
>> Error: need 'this' to access member foo
>>
>> -Steve
>
> A direct, C++-style pointer-to-member-function is occasionally useful. (Though rarely, since delegates are more useful nearly every time.) Allowing this syntax is the only sensible way to get access to them. The resulting pointer can then manually be shoved into a delegate:
>
> void function() fp = &Cls.foo;
> Cls c = new Cls;
> void delegate() dg;
> dg.funcptr = fp;
> dg.ptr = c; // I forget if a cast to void* is necessary...
> dg();
>
> Pyd relies on this behavior to implement its class wrapping, and it would be fairly inconvenient if it went away.

I had no idea you could build delegates this way.  But even still, the function signature is not correct, it should be something like:

void function(Cls c) fp = &Cls.foo;

But in any case, the original code seems like dangerous behavior to allow without casting.  There should at least be a cast involved so the user is forced to say "yes, I know that the function signature isn't correct, do it anyways".  I still say this should be a bug.

-Steve


July 30, 2008
Simen Kjaeraas Wrote:

> On Tue, 29 Jul 2008 07:33:51 +0200, ws <wisiong@gmail.com> wrote:
> 
> > Maybe i should have written it this way:
> >
> > int function() fp;
> > int delegate() dp;
> > int *ptr;
> > class Cls
> > {
> >   int k;
> >   int foo()  { return k; }
> > }
> >
> > void main()
> > {
> >   fp = &Cls.foo;
> >   assert(fp() == 0);
> >
> >   Cls c = new Cls;
> >   dp = &c.foo;
> >   ptr = &c.k;
> > 
> >   assert(is(typeof(ptr) == int*));
> >   assert(is(typeof(fp) == int function()));
> >   assert(is(typeof(dp) == int delegate()));
> > }
> >
> > Notice fp can be used without an instance of Cls.
> > What i need is actually a way to get the type of the class when i
> > reference the variable k, without actually creating an instance of the
> > class.
> > Something like this:
> >
> > typedef GetClass!(Cls.k) classType;
> > assert(is(typeof(classtype) == class) && is(typeof(classtype) == Cls));
> >
> > Is there a way to do it?
> 
> Have you tried calling fp in that example?
> 
> -- 
> Simen

Sorry the fp has to be called after Cls is instanstiated. Fixed as below:
 fp = &Cls.foo;
 Cls c = new Cls;
 assert(fp() == 0);  <-- after Cls instantiation.

July 30, 2008
On Wed, 30 Jul 2008 06:39:30 +0400, ws <wisiong@gmail.com> wrote:

> Simen Kjaeraas Wrote:
>
>> On Tue, 29 Jul 2008 07:33:51 +0200, ws <wisiong@gmail.com> wrote:
>>
>> > Maybe i should have written it this way:
>> >
>> > int function() fp;
>> > int delegate() dp;
>> > int *ptr;
>> > class Cls
>> > {
>> >   int k;
>> >   int foo()  { return k; }
>> > }
>> >
>> > void main()
>> > {
>> >   fp = &Cls.foo;
>> >   assert(fp() == 0);
>> >
>> >   Cls c = new Cls;
>> >   dp = &c.foo;
>> >   ptr = &c.k;
>> > 	
>> >   assert(is(typeof(ptr) == int*));
>> >   assert(is(typeof(fp) == int function()));
>> >   assert(is(typeof(dp) == int delegate()));
>> > }
>> >
>> > Notice fp can be used without an instance of Cls.
>> > What i need is actually a way to get the type of the class when i
>> > reference the variable k, without actually creating an instance of the
>> > class.
>> > Something like this:
>> >
>> > typedef GetClass!(Cls.k) classType;
>> > assert(is(typeof(classtype) == class) && is(typeof(classtype) ==  
>> Cls));
>> >
>> > Is there a way to do it?
>>
>> Have you tried calling fp in that example?
>>
>> --
>> Simen
>
> Sorry the fp has to be called after Cls is instanstiated. Fixed as below:
>  fp = &Cls.foo;
>  Cls c = new Cls;
>  assert(fp() == 0);  <-- after Cls instantiation.
>

No, that's wrong. Suppose you have two instances:

Cls first = new Cls();
first.k = 1;

Cls second = new Cls();
second.k = 2;

int result = fp();	

Question is what is the result of the fp() call?

Lets do some renaming:

class Human {
    char[] name;
    char[] getName() { return name; }
}

Human me = new Human();
me.name = "Denis"

Human you = new Human();
you.name = "Rui";

char[] name = Human.getName(); // what's the result?

char[] function() fp = &Human.getName();
char[] name2 = fp();

The answer is... none. The code is wrong. You can't access a property, which is different among class instances, like there is no such thing as common human name. Or age. Or anything else. There should be someone whose properties you are trying to determine - an object, class instance.

char[] name1 = me.getName(); // returns "Denis"
char[] name2 = you.getName(); // returns "Rui"
« First   ‹ Prev
1 2