在C99中,您可以声明结构的灵活数组成员:
struct blah
{
int foo[];
};
Run Code Online (Sandbox Code Playgroud)
但是,当有人在这里工作时尝试使用C++中的clang编译一些代码时,该语法不起作用.(它一直在与MSVC合作.)我们不得不将其转换为:
struct blah
{
int foo[0];
};
Run Code Online (Sandbox Code Playgroud)
通过C++标准,我发现根本没有提到灵活的成员数组; 我一直认为这[0]是一个无效的声明,但显然对于一个灵活的成员数组它是有效的.灵活的成员数组在C++中实际上是否有效?如果是这样,是正确的声明[]还是[0]?
给出以下代码:
class foo
{
};
class bar: public foo
{
public:
~bar() { printf("~bar()\n"); }
};
class zab: public foo
{
public:
~zab() { printf("~zab()\n"); }
};
struct foo_holder
{
const foo &f;
};
int main()
{
foo_holder holder[]= { {bar()}, {zab()} };
printf("done!\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
~bar()
~zab()
done!
Run Code Online (Sandbox Code Playgroud)
C++ 0x有一个子句规定这可以在用作新的初始化器时创建悬空引用,但它没有说明(至少我没有找到)有关临时引用的const引用的初始化.
这是不明确的行为吗?
F12是追踪UI阻止操作的一个奇迹,但我无法弄清楚如何使它与VS 2008和托管代码一起使用.
救命!或不...
编辑:事实证明它在Vista x64上无法在VS 2005中运行,所以我想根据你的观点,它可以扩大或缩小它:(
MSN
C++标准(引自草案n3242)说明以下关于子对象[intro.object]:
除非对象是零字段或零大小的基类子对象,否则该对象的地址是它占用的第一个字节的地址.两个不同的对象既不是位字段也不是零大小的基类子对象应具有不同的地址.
现在,给出以下代码段:
struct empty { };
struct member: empty { };
struct derived: empty { member m; };
int main(void)
{
printf("%d", sizeof(derived));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
gcc我相信打印出来2,而Visual C++ 2010打印出来1.我怀疑gcc正在采用标准来表示如果它们代表不同的对象,则不能对类型的存储进行别名.我打赌MSVC正在采用标准来表示如果一个子对象是零大小,你可以做任何你想做的事情.
这是不明确的行为吗?
我知道私有成员变量上的公共成员变量和访问器之间的差异,并且已经看到关于此的堆栈溢出的一些帖子.我的问题更多与实践有关.除了不破坏类不变量之外,通常在实用性方面的标准是使成员变量是公共的而不是私有的访问者,反之亦然?提前感谢您的建议.
我有一个UIImageView,它有一个png图像序列加载到它.
我的问题是 - 你知道我能用什么方式"打乒乓"动画片段吗?因此它从1-24向前播放然后从24-1向后播放并循环播放.
(技术上应该是:1-24然后23-1然后2-24然后23-1 ......等)
- (void) loadAnim01 {
mon01 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mon01_01.png"]];
mon01.center = CGPointMake(258,69);
NSMutableArray *array = [NSMutableArray array];
for (int i = 1; i <= 24; i++)
[array addObject:[UIImage imageNamed:[NSString stringWithFormat:@"mon01_%02d.png",i]]];
mon01.animationImages = array;
mon01.animationDuration = 1.0;
mon01.animationRepeatCount = 0;
[self.view addSubview:mon01];
[mon01 release];
Run Code Online (Sandbox Code Playgroud)
}
非常感谢!
是否可以保证每个库标题看起来都像这样?
#ifndef STDIO_H
#define STDIO_H
/* contents here... */
#endif
Run Code Online (Sandbox Code Playgroud)
你能告诉我一个消息来源吗?
谢谢!
编辑:这与C++标准核心语言活动问题上的活动问题232有关
所以真正的问题是标准如何允许间接通过空指针?
请考虑以下代码:
struct a { int x; };
struct b { int y; };
struct c: a, b { };
b *f(c *pointer_to_c) { return pointer_to_c; }
Run Code Online (Sandbox Code Playgroud)
f(...)必须测试是否pointer_to_c是NULL,如果是,则返回NULL.(无论你如何施放指针,NULL指针总是一个NULL指针.)
现在,请考虑以下事项:
b *f_ref(c &reference_to_c) { return &reference_to_c; }
Run Code Online (Sandbox Code Playgroud)
第一个问题:是否f_ref需要检查是否&reference_to_c是NULL?第二个问题:如果C++最终通过空指针定义间接行为,这f_ref是否必须检查NULL?(我想答案取决于标准允许的内容,即,如果它表示"为多重继承而未定义对基类进行NULL引用",那么答案显然是否定的.)
对于最后一个难题,现在考虑一下:
const b *f_const_ref(const c &reference_to_c) { return &reference_to_c; }
Run Code Online (Sandbox Code Playgroud)
是否f_const_ref必须检查&reference_to_c到NULL?顺便说一句,在VC++ 2010中,所有三个函数都经过优化f …
void pushSynonyms (string synline, char matrizSinonimos [1024][1024]){
stringstream synstream(synline);
vector<int> synsAux;
int num;
while (synstream >> num) {synsAux.push_back(num);}
int index=0;
while (index<(synsAux.size()-1)){
int primerSinonimo=synsAux[index];
int segundoSinonimo=synsAux[++index];
matrizSinonimos[primerSinonimo][segundoSinonimo]='S';
matrizSinonimos [segundoSinonimo][primerSinonimo]='S';
}
}
Run Code Online (Sandbox Code Playgroud)
和电话..
char matrizSinonimos[1024][1024];
pushSynonyms("1 7", matrizSinonimos)
Run Code Online (Sandbox Code Playgroud)
matrizSinonimos通过引用传递给我很重要.
编辑:带走了&&matrizSinonimos.
编辑:运行时错误是:
An unhandled win32 exception occurred in program.exe [2488]![alt text][1]
Run Code Online (Sandbox Code Playgroud) 我有一个一直存在的二进制文件.它有一个C级,它一直存在.我们必须向C类引入一个新的方法M,但我们只希望一些用户知道这种方法M的存在.
通过从.h文件中删除这样的方法,我们可以介绍哪个问题?这种方法是否会向后兼容?
编辑:我们实际上并不关心是否有办法找到方法.我们只想确保只有人知道他们在做什么,才能使用它.