473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

anonymous class

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

Apr 12 '07 #1
4 5293
josh wrote:
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.
Apr 12 '07 #2
On 12 Apr, 11:17, anon <a...@no.nowrote:
josh wrote:
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.
yes but it doesn't anwer my question...

Apr 12 '07 #3
josh wrote:
Hi, is there the possibility to create anonymous class (not object) in
C++ like do Java?
....

That would be NO.
Apr 12 '07 #4
josh wrote:
Hi, is there the possibility to create anonymous class (not object) in
C++ like do Java?
No, C++ does not support this code obfuscation tool.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Apr 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dan Williams | last post by:
Python experts, Is there a more pythonic way to do something evquilent to what this line does without creating a dummy class? self.file = type("", (object,), {'close':lambda slf: None})() ...
0
by: Carlos Ribeiro | last post by:
I thought about this problem over the weekend, after long hours of hacking some metaclasses to allow me to express some real case data structures as Python classes. I think that this is something...
6
by: Christopher Bohn | last post by:
It looks like the c++ source for a single object file can not contain more than one anonymous class that derives from a base class. When I attempted to do this, the linker failed with the...
6
by: Gaijinco | last post by:
I have always felt that there are a lot of topics that you learned the facts but you only grasp the matter sometime down the road. For me, two of those topics are inner classes and anonymous...
1
by: Yarco | last post by:
There exists anonymous function, and you could also use a variable to contain such function. But why no class? I mean, you could do: $func = create_function('$a', 'print $a;'); $func(); or ...
3
by: gsreenathreddy | last post by:
An anonymous class extending a class that has a nondefault constructor. I am not able to find the difference can any one explain about this.
1
by: r035198x | last post by:
Inspiration Inspired by a post by Jos in the Java forum, I have put together a little article on class initializers. Initializers are indeed underutilized by most Java programmers. Once the Java...
4
by: philly_bob | last post by:
In the sample program below, I want to send a random method to a class instance. In other words, I don't know which method to send until run-time. How can I send ch, which is my random choice, to...
1
by: Iw | last post by:
Hi, How to sort by UserName using linq and anonymous class ? var select = from u in db.Users select new { Id = u.UserId, Name = u.UserName, }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.