anonymous class

J

josh

Hi, is there the possibility to create anonymous class (not object) in
C++ like do Java?

as an example:

in Java if I do:

// here Shape is an Interface that is like a c++ class wih only pure
virtual functions
public static void doShape(Shape s)
{
double v, a;

v = s.volume();

a = s.area();
System.out.println("Volume: " + v + " Area: "+ a);
}

than,
doShape(
new Shape() //
{
public double area() {return 0.0;}
public double volume() {return 0.0;}
}
);

the Java compiler creates a new anonymous class that implements the
Shape interface and
than returns its reference. So the above code is like I was doing:

class Implement implements Shape
{
public double area() {return 1.0;}
public double volume() {return 1.0;}
}

Implement i = new Implement();
doShape(i);

Thanks
 
A

anon

josh said:
Hi, is there the possibility to create anonymous class (not object) in
C++ like do Java?

as an example:

in Java if I do:

// here Shape is an Interface that is like a c++ class wih only pure
virtual functions
public static void doShape(Shape s)
{
double v, a;

v = s.volume();

a = s.area();
System.out.println("Volume: " + v + " Area: "+ a);
}

than,
doShape(
new Shape() //
{
public double area() {return 0.0;}
public double volume() {return 0.0;}
}
);

the Java compiler creates a new anonymous class that implements the
Shape interface and
than returns its reference. So the above code is like I was doing:

class Implement implements Shape
{
public double area() {return 1.0;}
public double volume() {return 1.0;}
}

Implement i = new Implement();
doShape(i);

Thanks


You can declare/define a class in your source (cpp) file, and it would
be the same as an anonymous class in java.
Even better - put it in anonymous (nameless) namespace.
 
J

josh

You can declare/define a class in your source (cpp) file, and it would
be the same as an anonymous class in java.
Even better - put it in anonymous (nameless) namespace.

yes but it doesn't anwer my question...
 

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

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top