converting from Java

G

Gabest

Perhaps this was not the best example. There is more use of inner classes
when dealing with event listeners. Here is another example I found quickly
with google at
http://www.iam.ubc.ca/guides/javatut99/java/javaOO/innerClassAdapter.html .
In this case it would be a hustle to declare the class somewhere else.

public class AnonymousSpot extends Applet {
private Point clickPoint = null;
private static final int RADIUS = 7;
public void init() {
addMouseListener(
new MouseAdapter() {
public void mousePressed(MouseEvent event) {
clickPoint = event.getPoint();
repaint();
}
}
);
}
/* paint method not shown */
}
 
I

Ioannis Vranos

Gabest said:
Perhaps this was not the best example. There is more use of inner classes
when dealing with event listeners. Here is another example I found quickly
with google at
http://www.iam.ubc.ca/guides/javatut99/java/javaOO/innerClassAdapter.html .
In this case it would be a hustle to declare the class somewhere else.

public class AnonymousSpot extends Applet {
private Point clickPoint = null;
private static final int RADIUS = 7;
public void init() {
addMouseListener(
new MouseAdapter() {
public void mousePressed(MouseEvent event) {
clickPoint = event.getPoint();
repaint();
}
}
);
}
/* paint method not shown */
}


Well, the above can be easily implemented in C++ by defining a

AnonymousMouseAdapter: public MouseAdapter inside AnonymousSpot.


The important thing to note about C++, is that is not an "exotic
language", you know the cost of everything, so since a class gets
created, a class definition should be provided explicitly.


Still, imagine if C++ provided as an "exotic" built in std::string,
without any clues of its definition.


And that is the strength of C++, it is *both* high-level and low level,
that is it does not lack any real high level facilities.

In usual applications you can ignore the internals of std::string by
using "selective ignorance" and use it as an "exotic" type.


However C++ aims also to be a powerful language, and power has details.


Also, again, GC is available to C++ when it exists.


Can you do this in Java?




// (Compile time) template
template <class T>
ref class SomeClass1
{
array<T> ^someArray;
};


// (Run time) generic
generic<class T>
ref class SomeClass2
{
array<T> ^someArray;
};

ref class SomeOtherClass {};

int main()
{
// In the GC heap
SomeClass1<SomeOtherClass ^> ^h1= gcnew SomeClass1<SomeOtherClass ^>;

// In the GC heap with stack semantics
// Deterministic destruction at the end of scope
SomeClass2<SomeOtherClass ^> obj1;

SomeClass1<SomeClass2<SomeOtherClass ^> ^>obj2;


SomeClass2<SomeClass1<SomeOtherClass ^> ^> ^h2;

//Deterministic destruction
delete h2;
}


C:\c>cl /clr:safe temp.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.41013
for Microsoft (R) .NET Framework version 2.00.41013.0
Copyright (C) Microsoft Corporation. All rights reserved.

temp.cpp
Microsoft (R) Incremental Linker Version 8.00.41013
Copyright (C) Microsoft Corporation. All rights reserved.

/out:temp.exe
temp.obj

C:\c>



As you see both compile-time templates and run-time generics are
available, can be mixed, and produce pure IL code (/clr:safe) making the
produced executable binary portable to all CLI VMs.
 
I

Ioannis Vranos

Ioannis said:
Well, the above can be easily implemented in C++ by defining a

AnonymousMouseAdapter: public MouseAdapter inside AnonymousSpot.


The important thing to note about C++, is that is not an "exotic
language", you know the cost of everything, so since a class gets
created, a class definition should be provided explicitly.


Still, imagine if C++ provided as an "exotic" built in std::string,
without any clues of its definition.


And that is the strength of C++, it is *both* high-level and low level,
that is it does not lack any real high level facilities.

In usual applications you can ignore the internals of std::string by
using "selective ignorance" and use it as an "exotic" type.


However C++ aims also to be a powerful language, and power has details.


Also, again, GC is available to C++ when it exists.


Can you do this in Java?




// (Compile time) template
template <class T>
ref class SomeClass1
{
array<T> ^someArray;
};


// (Run time) generic
generic<class T>
ref class SomeClass2
{
array<T> ^someArray;
};

ref class SomeOtherClass {};

int main()
{
// In the GC heap
SomeClass1<SomeOtherClass ^> ^h1= gcnew SomeClass1<SomeOtherClass ^>;

// In the GC heap with stack semantics
// Deterministic destruction at the end of scope
SomeClass2<SomeOtherClass ^> obj1;

SomeClass1<SomeClass2<SomeOtherClass ^> ^>obj2;


SomeClass2<SomeClass1<SomeOtherClass ^> ^> ^h2=
 
D

Daniel O'Connell [C# MVP]

int main(int const argc, char** const argv)
{
I *i= new J(){
int f(){
return argc + argv[0][0];
}
};
}

and have the i pointer be valid even after main has exited (and therefore
the original argc and argv variables are out of scope). Basically, it
doesn't work too well if you haven't got GC.

Best to read up on this. Does C# have anything similar?

In C# 2.0 there is support for anonymous methods which do provide parameter
and local capture(via a heap allocated container object, if memory serves),
but do not define whole, user accessible types(they do generate classes
behind the scene to contain the method and the parameter object, if memory
serves.)

These methods, when created, are delegate objects which can be assigned to
events or passed into some of the newer, delegate based API's that are being
introduced in the 2.0 framework.

the syntax would be something like...

static void Main(string[] args)
{
MethodInvoker f = delegate() { Console.WriteLine(args[0]); };
}

FWIW, I think this is *close* to the concept of a closure, but I am not sure
if it satisfies all the requirements or not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top