Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's actually going on in this AnonymousClass(variable) declaration?

Tags:

c++

Trying to compile:

class AnonymousClass
{
public:
    AnonymousClass(int x)
    {
    }
};


int main()
{
    int x;
    AnonymousClass(x);
    return 0;
} 

generates errors from MSVC:

foo.cpp(13) : error C2371: 'x' : redefinition; different basic types
    foo.cpp(12) : see declaration of 'x'
foo.cpp(13) : error C2512: 'AnonymousClass' : no appropriate default constructor available

g++'s error messages are similar:

foo.cpp: In function ‘int main()’:
foo.cpp:13: error: conflicting declaration ‘AnonymousClass x’
foo.cpp:12: error: ‘x’ has a previous declaration as ‘int x’
foo.cpp:12: warning: unused variable ‘x’

It's easily fixable by giving the AnonymousClass object an explicit name, but what's going on here and why? I presume that this is more declaration syntax weirdness (like the cases described in Q10.2 and Q10.21 of the comp.lang.C++ FAQ), but I'm not familiar with this one.

like image 237
jamesdlin Avatar asked Jun 14 '11 10:06

jamesdlin


People also ask

Can an anonymous class access variables that are not final?

An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final. Like a nested class, a declaration of a type (such as a variable) in an anonymous class shadows any other declarations in the enclosing scope that have the same name.

How do you declare an anonymous class?

Anonymous Class Declaration Anonymous classes are inner classes with no name. Since they have no name, we can't use them in order to create instances of anonymous classes. As a result, we have to declare and instantiate anonymous classes in a single expression at the point of use. We may either extend an existing class or implement an interface.

What is an anonymous class declaration in Python?

As we already mentioned, an anonymous class declaration is an expression, hence it must be a part of a statement. This explains why we have put a semicolon at the end of the statement.

How to assign an anonymous class to a variable in Java?

Once we have instantiated an anonymous class, we can assign that instance to a variable in order to be able to reference it somewhere later. We can do this using the standard syntax for Java expressions: Runnable action = new Runnable () { @Override public void run() { ...


2 Answers

AnonymousClass(x);

It defines a variable x of type AnonymousClass. That is why you're getting redefinition error, because x is already declared as int.

The parentheses are superfluous. You can add even more braces like:

AnonymousClass(x);
AnonymousClass((x));
AnonymousClass(((x)));
AnonymousClass((((x))));
//and so on

All of them are same as:

AnonymousClass x;

Demo: http://www.ideone.com/QnRKH


You can use the syntax A(x) to create anonymous object, especially when calling a function:

int x = 10;
f(A(x));        //1 - () is needed
f(A((((x)))));  //2 - extra () are superfluous

Both line 1 and 2 call a function f passing an object of type A :

  • http://www.ideone.com/ofbpR

But again, the extra parentheses are still superfluous at line 2.

like image 199
Nawaz Avatar answered Nov 09 '22 01:11

Nawaz


You're missing an actual name for your variable/object:

AnonymousClass myclass(x);

Instead of that you could as well write...

AnonymousClass (myclass)(x);

So your line of code results in this:

AnonymousClass (x);

Or more common:

AnonymousClass x;

Why it happens? Brackets are just there for logical grouping ("what belongs together?"). The only difference is, they're forced for arguments (i.e. you can't just write AnonymousClass myclass x).

like image 40
Mario Avatar answered Nov 08 '22 23:11

Mario