[Info-vax] Groovy on VMS
Arne Vajhøj
arne at vajhoej.dk
Tue Mar 5 19:39:08 EST 2024
On 3/5/2024 7:34 PM, Arne Vajhøj wrote:
> On 3/5/2024 5:53 PM, Lawrence D'Oliveiro wrote:
>> On Tue, 5 Mar 2024 17:48:21 -0500, Arne Vajhøj wrote:
>>> But I consider:
>>>
>>> o->f(a1, a2, a3) to be C++
>>
>> If you can refer to o as “this”, that’s C++. You can’t do that in C.
>
> But this is not available by magic. It is there because
> it (at least in some implementations - I doubt that the
> standard define the "how") pass it as a hidden 1st argument.
To illustrate:
$ type m.cxx
#include <iostream>
using namespace std;
class C
{
public:
virtual void M1(int v) = 0; // C++ API in C++
virtual void M2(char *s) = 0; // C++ API in C++
};
class C1 : public C
{
public:
void M1(int v) { cout << "C1 says: " << v << endl; }
void M2(char *s) { cout << "C1 says: " << s << endl; }
};
class C2: public C
{
public:
void M1(int v) { cout << "C2 says: " << v << endl; }
void M2(char *s) { cout << "C2 says: " << s << endl; }
};
extern "C"
{
void f(C **o);
}
int main()
{
C *o1 = new C1();
f(&o1);
delete o1;
C *o2 = new C2();
f(&o2);
delete o2;
return 0;
}
$ type s.c
#include <stdio.h>
struct C;
typedef void (*fptr1)(struct C **this, int v); // C++ API in C
typedef void (*fptr2)(struct C **this, char *s); // C++ API in C
struct C_vtable
{
fptr1 M1;
fptr2 M2;
};
struct C
{
struct C_vtable *vtable;
};
void f(struct C **o)
{
(*o)->vtable->M1(o, 123);
(*o)->vtable->M2(o, "ABC");
}
$ cxx m
$ cc s
$ cxxlink m + s
$ run m
C1 says: 123
C1 says: ABC
C2 says: 123
C2 says: ABC
Arne
More information about the Info-vax
mailing list