在gcc中,可以创建一个匿名结构,如下所示:
struct test2 {
struct {
int x;
int y;
};
int z;
};
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用预定义版本的匿名结构做同样的事情时,它会失败:
struct test1 {
int x;
int y;
};
struct test2 {
struct test1;
int z;
};
Run Code Online (Sandbox Code Playgroud)
我想这样做的原因是我想要定义一个结构,它包含一组与C类的"成员函数"相对应的函数指针.如果我不匿名包含结构,那么我最终不得不做一些令人讨厌的事情:
object->API->function(object,...);
Run Code Online (Sandbox Code Playgroud)
对于我需要做的一些事情,这只是普通的不起作用(例如,因为"object.API"与"对象"不匹配,所以很难或不可能从支持隐藏的其他语言进行自动绑定对象指针.)
目前,我只是复制/粘贴函数,但这很乏味且容易出错.我可以使用C预处理器,但是创建一个包含大量函数原型的宏似乎很难看.
关于匿名类别:
但我不能第二次声明一个匿名类别来拆分我的变量和方法的定义?
我知道Xcode允许这样做,但它会没有问题吗?
更新
我解释了againg.主要问题是我可以在同一个文件中使用2个没有名称(匿名)的类别,还是会覆盖/重叠?现在清楚了吗?
我正在自学java,我真的很喜欢它,但是我来到了"Anonymous Class"主题,而我正试图理解何时,甚至在哪里使用它,从我的书中说它是一个非常流行的课程,但我似乎无法理解它,我明白如何创建它们.但我只是在寻找更多的信息,所以我可以开始在我的课程中实现它们.
我真的很感激一些例子,并且对它们何时使用它们有更多的解释.
我正在尝试实现一个简单的 websockets 应用程序,该应用程序可以将消息从一个端点发送到在其他地方建立的指定会话。到目前为止,我已经能够使用注释 @SendToUser() 以便客户端订阅频道(如本问题中所述:Spring Websockets @SendToUser without Login?)
但是,我现在想要创建一个单独的端点,当调用该端点时,它会查找与传递到该端点的数据关联的用户,并向该用户发送有关该数据的消息。
但是,我无法准确确定如何制作它,以便我可以调用 SimpMessagingTemplate ConvertAndSendToUser() 命令,因为我的用户没有主体(我没有使用 Spring Security)。
我已经能够从传递到 @MessageMapping 端点的 MessageHeaders 中获取 simpSessionId,但现在我无法弄清楚如何使用 simpSessionId 从应用程序的不同部分发送信息。
我已经做了一些研究,涉及重写 DefaultHandshakeHandler 的确定用户()方法,并在成功的 websocket 握手时将随机生成的 UUID 作为用户名分配给用户(如此问题的答案中所述:如何在 Spring 4 中回复未经身份验证的用户) STOMP over WebSocket 配置?),但由于出现的主体为 null,我不确定如何正确生成主体并将其分配给主体以供应用程序使用。
我基本上需要能够拥有匿名用户,并在他们创建 websocket 连接后从应用程序的不同部分向他们发送消息。
因此,可以声明匿名类或结构但是如何使它有用?
int main() {
class{
int ClassVal;
};
struct{
short StructVal;
};
StructVal = 5; //StructVal is undefined
ClassVal = 5; //ClassVal is undefined too?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果你把它们都放在主要功能之外,它们也将无法访问.我问的只是因为它在某种程度上有趣:)
编辑:为什么在main函数之外的联合(在全局范围内)必须是静态声明的,例如:
static struct {
int x;
};
int main() {
//...
}
Run Code Online (Sandbox Code Playgroud) 我有一个方法有2个参数:
public void ReplaceSomething(ref int code, ref string name)
{
...
}
Run Code Online (Sandbox Code Playgroud)
我想避免这种情况,因为它不是一个好的设计(并且很难缩放).我有什么选择?
我虽然使用匿名对象,但这似乎也不是一个好主意.
Object something = new { code = 1, name = "test" };
ReplaceSomething(something);
Run Code Online (Sandbox Code Playgroud) 我在两个类之间映射数据,其中一个类是在另一个类(销售订单)中创建或修改数据的采购订单.如果销售订单值不为空,我还会保留更改内容的事务日志.你能告诉一种方法来制作这种通用的吗?
private static DateTime CheckForChange(this DateTime currentValue,
DateTime newValue, string propertyName)
{
if (currentValue == newValue) return currentValue;
LogTransaction(propertyName);
return newValue;
}
private static decimal CheckForChange(this decimal currentValue,
decimal newValue, string propertyName)
{
if (currentValue == newValue) return currentValue;
LogTransaction(propertyName);
return newValue;
}
private static int CheckForChange(this int currentValue,
int newValue, string propertyName)
{
if (currentValue == newValue) return currentValue;
LogTransaction(propertyName);
return newValue;
}
Run Code Online (Sandbox Code Playgroud)
private static T CheckForChange<T>(this T currentValue, T newValue,
string propertyName) where T : ???
{ …Run Code Online (Sandbox Code Playgroud) 我有以下代码:(为了这个问题的目的非常简化,但完美地说明了我遇到的问题)
#!/usr/bin/perl
use strict;
use warnings;
&outer;
my $connected_sub;
sub outer {
print "HELLO\n";
&$connected_sub;
$connected_sub = sub {
print "GOODBYE\n";
}
}
Run Code Online (Sandbox Code Playgroud)
运行时程序会给出此输出和错误:
HELLO
Use of uninitialized value in subroutine entry at subTesting line 13.
Can't use string ("") as a subroutine ref while "strict refs" in use at subTesting.pl line 13.
Run Code Online (Sandbox Code Playgroud)
我完全忽略了什么吗?我无法理解或弄清楚这是什么问题.
在创建对象后它有一个类主体及其有用的内容时,它会调用什么?
例:
public MyObject myObject = new MyObject() /*from here*/{
void myMethod() {
//code
}
};/*to here*/
Run Code Online (Sandbox Code Playgroud)
因为通常它看起来像这样:
public MyObject myObject = new MyObject();
Run Code Online (Sandbox Code Playgroud) anonymous ×10
class ×3
java ×3
c# ×2
struct ×2
c ×1
c++ ×1
categories ×1
gcc ×1
generics ×1
ios ×1
nested ×1
objective-c ×1
overloading ×1
perl ×1
principal ×1
spring ×1
stomp ×1
subroutine ×1
websocket ×1