我有以下课程:
class A: public Util<double> {}
class B: public Util<double>, public Interface {}
template<class T>
class MyTmp {
public:
void foo(const A& a);
}
class MyClass: public MyTmp<Other> {
public:
void foo(const B& b);
}
Run Code Online (Sandbox Code Playgroud)
当我foo使用MyClass带有A对象的实例调用未知原因时,调用foo方法MyClass而不是foo类MyTmp.我正在使用gcc 4.4.2使用-O3.有小费吗?
我有一个名为 A 的活动。我想添加两个名为 B 和 C 的活动别名。是否可以知道 A 在代码中是否被称为 B 或 C?当它被称为 B 或 C 时,我想应用不同的行为。
我目前正在研究一个很老的c ++代码.在这段代码中,我可以找到:
#ifdef LOG
LOG_DEBUG(bla bla bla);
#endif
Run Code Online (Sandbox Code Playgroud)
并在调试头文件中:
#ifdef LOG
LOG_DEBUG(X) ....code....
#else
LOG_DEBUG(X) (void) 0
#endif
Run Code Online (Sandbox Code Playgroud)
现在我的问题是:有没有理由在代码中的每个语句中重复ifdef?我想这种策略在一种情况下确实是值得的,即当我需要执行一些艰苦的初步工作来记录某些东西时.
#ifdef LOG
int a = mySuperComplicatedFunction();
....other worth code here with "a"...
LOG_DEBUG(the result here);
#endif
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
我有一个Foo类,必须在其他类Bar中“直接”访问。我想构建一个小框架,声明Bar方法(这是Foo的朋友方法)受保护的方法。这样,我可以为Bar培养几个班级的孩子。
Gcc抱怨这一点,并且只有在该方法是公开的情况下才起作用。
我能怎么做?我的代码示例:
class Foo;
class Bar {
protected:
float* internal(Foo& f);
};
class Foo {
private:
//some data
public:
//some methods
friend float* Bar::internal(Foo& f);
};
Run Code Online (Sandbox Code Playgroud)
Gcc讯息:
prog.cpp:4:16: error: ‘float* Bar::internal(Foo&)’ is protected
float* internal(Foo& f);
^
prog.cpp:11:43: error: within this context
friend float* Bar::internal(Foo& f);
^
Run Code Online (Sandbox Code Playgroud) 这个代码合法吗?它可以编译,但我想知道返回值会发生什么。未定义的行为?
class Foo {
public:
void test1() {
}
auto test() -> decltype(test1()) {
return test1(); //<---return void here!
}
};
Run Code Online (Sandbox Code Playgroud) 我正在使用c ++ STL集,我想知道它是否存在于集合中的等效实例.要检索实例,我正在使用find set方法.问题是它不起作用.我认为问题出在我的比较器对象中:
bool SetComparator::operator ()( const Point* i1, const Point* i2 ) const {
if ( *i1 == *i2 )
return false;
return true;
}
Run Code Online (Sandbox Code Playgroud)
以简单的方式为类Point重新定义operator ==:
bool Point::operator ==( const Point& p ) const {
if (x == p.x && y == p.y)
return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
在调试之后,我可以看到find方法调用operator(),但它找不到相同的实例,因此find返回end()但我知道有一个相等的对象.我认为问题与设定的内部订单有关.我能怎么做?