I'd like to ask if is it portable to rely on string literal address across translation units? I.e:
A given file foo.c
has a reference to a string literal "I'm a literal!"
, is it correct and portable to rely that in other given file, bar.c
in instance, that the same string literal "I'm a literal!"
will have the same memory address? Considering that each file will be translated to a individual .o
file.
For better illustration, follows an …
我有使用著名的container_of
宏来实现仅宏链接列表库的代码。
它可以完美地在C语言中运行。现在,我想在它上面支持C ++,因此我需要一个container_of
与以下签名匹配的C ++替代品:
container_of(ptr, type, member)
Run Code Online (Sandbox Code Playgroud)
C实现是这样的:
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr);
(type *)( (char *)__mptr - offsetof(type,member) );})
Run Code Online (Sandbox Code Playgroud) 在一本关于C编程的(德语)书中(Linux-UNIX-Programmierung,由JürgenWolf)我发现了一个声明,翻译成英文如下(由我编号的句子):
在某些情况下,您可能需要复制文件描述符[1].这样做的一个例子是,如果父进程想要与子进程交换数据,并且子进程通过使用
exec*()
[2] 被新进程覆盖.在这种情况下,没有dup() or dup2()
,将设置close-on-exec标志[3].设置此标志后,所有文件描述符都将变为无效(因为被新进程覆盖) - 也就是说,它们不再存在[4].因此,父母和子女过程之间的沟通将被停止[5].另一方面,如果使用复制文件描述符dup() or dup2()
,则删除close-on-exec标志,并且新覆盖的进程可以使用此文件描述符进行通信[6].
我认为上段包含一些误导性陈述甚至错误.
在句子[3]中,我不明白为什么没有使用dup()
或者dup2()
设置close-on-exec标志?
考虑到领域驱动设计,基础设施或系统是否可以使用域的对象(值、实体等),或者是否应该应用依赖倒置,以便基础设施仅依赖于自己定义的接口?
存储库怎么样?它同样适用吗?
是否违反了基础设施、存储库或取决于域的系统代码?
(A) 基础设施依赖于域的示例代码:
namespace Infrastrcuture {
public class Sender {
public void Send (Domain.DataValue data) { ... }
}
}
Run Code Online (Sandbox Code Playgroud)
(B) 基础设施不依赖于域的示例代码:
namespace Infrastrcuture {
public interface ISendableData {
...
}
public class Sender {
public void Send (ISendableData data) { ... }
}
}
Run Code Online (Sandbox Code Playgroud) 我想知道使用FreeType 2获取文本边界框的最佳方法是什么?
为了获得行间距边框宽度,我迭代文本的所有字符并获得它的前进和学习:
FT_Face face = ...;
text_bbox_width = 0;
while (*p_text)
{
...
FT_Get_Kerning(...);
text_bbox_width += (face->glyph->advance.x + kerning.x) >> 6;
}
Run Code Online (Sandbox Code Playgroud)
如何获得线性空间边界框高度?是否需要迭代或使用字体数据获取?即:
text_bbox_height = (face->ascender - face->descender) >> 6
Run Code Online (Sandbox Code Playgroud) 我的代码是这样的:
#define ZERO_CONSTANT 0
foo(size_t * p_size);
foo(ZERO_CONSTANT); //Doesn't warn
Run Code Online (Sandbox Code Playgroud)
哪个gcc标志会拨打电话foo(ZERO_CONSTANT)
进行警告?
如何从另一个类重写方法?情况如下:
class foot:
{
void kick() { clumsyKick(); };
}
class person: /* Person has Foot */
{
foot *personsFoot;
}
class soccerPlayer: /* SoccerPlayer has Foot too, but his foot is better */
{
foot *soccerPlayerFoot;
}
Run Code Online (Sandbox Code Playgroud)
现在我想使用以下方法覆盖soccerPlayer类的foot类Kick()方法(我理解语法不正确):
soccerPlayer::foot::Kick() { expertKick(); }
Run Code Online (Sandbox Code Playgroud)
在C++中有可能吗?对于这种情况,最好的解决方法是什么?
ps:我不能为foot :: Kick()的每个实现创建一个派生类;