我有以下代码,基本上,
class foo {
public:
void method();
};
void foo::foo::method() { }
Run Code Online (Sandbox Code Playgroud)
我不小心在foo :: method的定义前添加了一个额外的foo ::.这段代码在没有使用g ++(版本4.2.3)的情况下编译,但是使用Visual Studio 2005进行了编译.我没有名为foo的命名空间.
哪个编译器正确?
我有以下C++代码,其中声明中我的构造函数的参数具有与构造函数的定义不同的常量.
//testClass.hpp
class testClass {
public:
testClass(const int *x);
};
//testClass.cpp
testClass::testClass(const int * const x) {}
Run Code Online (Sandbox Code Playgroud)
我能够使用g ++编译这个没有警告,如果这个代码编译或至少给出一些警告?事实证明,64位solaris上的内置C++编译器给了我一个链接器错误,这就是我注意到存在问题的方式.
在这种情况下匹配参数的规则是什么?这取决于编译器吗?
我在C++中使用STL中的set_intersection,我想知道在创建交集时是否有关于使用哪个集合的规则.或者是行为未定义,并且可能依赖于实现.
在linux上使用g ++(版本4.4.6),它似乎总是使用传递给set_difference函数的第一个集合,但我不确定我可以依赖它.从下面的示例中可以看出,每个集合成员中的其他数据在运算符<function中未被考虑.
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
class myClass {
public:
myClass(int val, int data)
: value(val),
metaData(data) {}
// Only consider the value, not metaData
bool operator<(const myClass &other) const{
return value < other.value;
}
void print() const {
cout << "Value: " << value << " metaData: " << metaData << endl;
}
private:
int value;
int metaData;
};
int main() {
// Create two sets with some data
set<myClass> set1;
set<myClass> set2; …
Run Code Online (Sandbox Code Playgroud)