473,766 Members | 2,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.prin tln("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 5307
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.prin tln("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.nowrot e:
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.prin tln("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
5332
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})() As you can guess, I want a dummy object that I can call close on with impunity.
0
2068
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 with potential to be useful, but I would like to hear more opinions first. If this is deemed to be useful, I *may* try to write a PEP for it. This is not a promise or even a proposal, at this point. Broadly generalizing, classes in Python have...
6
1294
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 following error: fatal error LNK1179: invalid or corrupt file: duplicate COMDAT '??0__unnamed@@QAE@XZ' The "??0__unnamed@@QAE@XZ" symbol undecorates to "public: __thiscall __unnamed::__unnamed(void)" so I think the compiler is using the same class...
6
6988
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 classes. I was thinking of a class Agenda. For it I would use a class Person which also uses another class Date for her birthday. When I was modeling Person, I made an atribute to be an object of class Date. Suddenly I thought that maybe it was a...
1
1268
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 function test($a)
3
1102
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
7333
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 programmer knows about the constructor he/she feels they have found a good place to put code that must be run before an object is created and sometimes don't try to look for more places to put pre-object creation code. The other places to put...
4
1503
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 the myclass instance? Thanks, Bob= ####
1
2649
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
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.