How is it possible to legally write ::: in C++ and ??? in C#?
These questions are a kind of game, and I did not find the solution for them.
It is possible to write:::
in C++ without using quotes or anything like this and the compiler will accept it (macros are prohibited too).开发者_Python百科
And the same is true for C# too, but in C#, you have to write ???
.
I think C++ will use the ::
scope operator and C# will use ? :
, but I do not know the answers to them.
Any idea?
You can write three consecutive question marks in C# without quotes, but not without whitespace, using the null-coalescing operator and the nullable alias character:
object x = 0;
int y = x as int? ?? 1;
With whitespace, it's easy:
C++
class A{};
class B : :: A{};
or
int foo;
int bar(){
return decision ? -1 : :: foo;
}
But without whitespace, these won't compile (the compiler sees :: :
, which doesn't make any sense).
Similarly, Aaronaught gave a good example of ? ??
in C#, but without whitespace, the compiler sees it as ?? ?
, which won't compile.
I think C# will use ? :
Do you mean use three question marks in the same line?
var a = true ? new Nullable<int>(1) ?? 1 : 0;
Edit: as far as I know, it's impossible to write ???
in any version of C#.
精彩评论