- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经高度怀疑它是可行的并且根本没有风险,但我仍然想确定地知道,所以这是我的事情......
我有一个 C++ 代码,它使用相当大的结构(15-30 个字段)作为容器,作为类构造函数的参数。这些结构的问题是我需要使用 C99 语法声明它们(与 C++ ffs 不兼容):
FooBar fb = { .foo = 12, .bar = 3.4 };
因为我无法想象拥有构造函数,因为在初始化这些结构时可以安全地跳过某些字段,而其他字段则不能,等等(它们仅由“用户提供”数据专门初始化,用户是我发生),有点像他们描述的配置。
无论如何,指出一点,我要使用一个 .h header ,它使用纯 C 语法声明结构,一个 .c 文件包含我初始化的结构,然后我使它们在我的 .cpp 文件中使用外部“C”。它工作得很好......除了我会发现能够为这些结构提供方法非常方便。
所以我的问题是,是否可以像这样声明结构
#ifdef __cplusplus
// C++ compatible declarations with methods
struct foobar {
int foo;
float bar;
int method1();
int method2();
void method3();
[etc.]
};
#else
/* C compatible declarations, no methods */
struct foobar {
int foo;
float bar;
};
#endif
这样我就可以在 C++ 中使用一些绑定(bind)到结构的方法(它更优雅,面向 OOP),并且我可以使用 C99 的指定初始化器在我的 C 代码中安全地初始化它们。
我担心的是一些我可能不知道的问题,这些问题最终可能导致 C 代码中的结构偏移与 C++ 代码中的不同。这些问题是否存在?
谢谢
最佳答案
在 C++11 中,如果您的 struct
是standard-layout,那么它与等效的 C struct 具有相同的布局。构成标准布局结构的规则如下:
A standard-layout class is a class that:
— has no non-static data members of type non-standard-layout class (or array of such types) or reference,
— has no virtual functions (10.3) and no virtual base classes (10.1),
— has the same access control (Clause 11) for all non-static data members,
— has no non-standard-layout base classes,
— either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes with non-static data members, and
— has no base classes of the same type as the first non-static data member.
由于您的结构只有几个公共(public)数据成员和一些非虚拟方法,因此您的结构是标准布局。你可以用更少的重复来表达它
struct foobar {
int foo;
float bar;
#ifdef __cplusplus
int method1();
int method2();
void method3();
#endif
};
在以前的 C++ 版本中,您的类型必须是普通旧数据 (POD) 类型,并且有更多限制。但是,您的结构仍然是 POD(没有虚函数、没有构造函数或析构函数、没有基类),因此它仍然与 C 布局兼容。
关于带方法的 C++ 结构,不带方法的 C 声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32641752/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!