🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

3 total messages Started by John Edstrom Thu, 10 Sep 1992 20:33
how to do this?
#234
Author: John Edstrom
Date: Thu, 10 Sep 1992 20:33
30 lines
1019 bytes

I am trying to convert some C++ code to g++.  What is wrong with the
following constructions and how can I do it in g++?

----------

class a {
public: 
	int x();
};

int a::x() { return 1; }

main() {
	printf("%x\n", (int(*))a::x);
}


the compiler complains that it cannot convert a::x to a pointer


what am I doing wrong?

JE
--
 RM 2104, HSc Building,  Div. Neuroscience
 U. Calgary School of Medicine,  3330 Hospital Drive NW
 Calgary, Alberta       T2N 3Y4
 (403) 220 4493  (wk)
Re: how to do this?
#239
Author: Jonas Svanberg
Date: Sat, 12 Sep 1992 16:00
34 lines
1656 bytes
> What is wrong with the
> following constructions and how can I do it in g++?
>
> ----------
>
> class a {
> public: 
>	int x();
> };
>
> int a::x() { return 1; }
>
> main() {
>	printf("%x\n", (int(*))a::x);
> }

You're trying to cast a pointer to member to a pointer to object
type. That can't be done according to the standards
documentation I've read over C++ (Annotated C++ Ref. Man. by
Ellis & Stroustrup). 
Pointer to members may even not be explicitly converted to void*!

The reason for this is that pointers to members may be
a structure containing both addresses and offsets. There is
no point in allowing a cast to any ordinary pointer.

O----------------------------O----------------------------------O
| Jonas Svanberg             | Email: svan@isy.liu.se           |
| Div. of Information Theory O----------------------------------O
| Department of E.E. (ISY)   | "And Noah said to the animals:   | 
| Linkoping University       |  - Go out and multiply!          |  
| S-581 83 Linkoping         |  The snake said:                 |
| SWEDEN                     |  - But how can I? I'm an adder!" |
O----------------------------O----------------------------------O
Re: how to do this?
#240
Author: John Edstrom
Date: Sun, 13 Sep 1992 17:01
106 lines
3563 bytes
In article <1992Sep12.160053.28778@isy.liu.se> svan@isy.liu.se writes:
>> What is wrong with the
>> following constructions and how can I do it in g++?
>>
>> ----------
>>
>> class a {
>> public: 
>>	int x();
>> };
>>
>> int a::x() { return 1; }
>>
>> main() {
>>	printf("%x\n", (int(*))a::x);
>> }
>
>You're trying to cast a pointer to member to a pointer to object
>type. That can't be done according to the standards
>documentation I've read over C++ (Annotated C++ Ref. Man. by
>Ellis & Stroustrup). 

To a member function, not just a member.  It can be done because I
encountered it while trying to port some code from AT&T C++ to g++.
It just doesn't seem to be possible in g++.

>Pointer to members may even not be explicitly converted to void*!
>
>The reason for this is that pointers to members may be
>a structure containing both addresses and offsets. There is
>no point in allowing a cast to any ordinary pointer.
>

There is if you want to choose different functions at a certain point
in a program depending on current circumstances.  Sorting elements by
different criteria, for example.


The workaround I've discovered is as follows :

typedef int (a::*afp)( void );   // can not do it without typedef and
				 // using intermediate variables
class a {
public:
  int a(void);
  int b(void);
  int c(void);
};

int a::a(void) { return 1; }
int a::b(void) { return 2; }
int a::c(void) { return 3; }

main() {
  afp y = a::x;

  printf("%x\n", y);  //  this gives me the address of the member
		      //  function 
}


But still I can't execute the function directly like its seems I
should be able to:

inline int a::do_something( afp x ) { return x(); }   // no way


The only way I can get it to go is to have another member function
like: 

int a::executor(cfp x) {
  if (x == a)
    return a();
  if (x == b)
    return b();
  if (x == c)
    return c();
}


Where to get the value from a passed member function pointer the usage
is: 

inline int a::do_something(afp x ) { return executor(x); }   


I know this is ugly and stupid and I hope there is a better way.


>O----------------------------O----------------------------------O
>| Jonas Svanberg             | Email: svan@isy.liu.se           |
>| Div. of Information Theory O----------------------------------O
>| Department of E.E. (ISY)   | "And Noah said to the animals:   | 
>| Linkoping University       |  - Go out and multiply!          |  
>| S-581 83 Linkoping         |  The snake said:                 |
>| SWEDEN                     |  - But how can I? I'm an adder!" |
>O----------------------------O----------------------------------O
>


JE
--
 RM 2104, HSc Building,  Div. Neuroscience
 U. Calgary School of Medicine,  3330 Hospital Drive NW
 Calgary, Alberta       T2N 3Y4
 (403) 220 4493  (wk)
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads