看看下面的代码:
struct A {
public:
virtual void f(){std::cout << "in A";};
};
struct B : A{
public:
virtual void f(){std::cout << "in B";};
int a;
};
struct C : B{
using A::f;
void test(){f();}
};
int main()
{
C c;
c.f(); // calls B::f, the final overrider
c.C::f(); // calls A::f because of the using-declaration
c.test(); //calls B::f
return 0;
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,B::f()in C应该隐藏使用声明A::f()带来的内容C; 如果是的话,为什么c.C::f()还要打电话A::f()?
如果c.C::f()调用A::f(),这应该意味着,在范围 …
我有一个嵌入式系统,并希望在此系统中使用boost,但需要禁用异常,因为我不想支付异常费用.
Boost给出了一个user.hpp和可设置的宏选项BOOST_NO_EXCEPTIONS和BOOST_NO_EXCEPTION_STD_NAMESPACE,但是如果定义了这两个宏,则无法编译boost :: shared_ptr(更确切地说,无法链接).
shared_ptr_boost.cpp:(.text._ZN5boost6detail12shared_countC2IiEEPT_[_ZN5boost6detail12shared_countC5IiEEPT_]+0x7a): undefined reference to `boost::throw_exception(std::exception const&)'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
为什么boost会提供宏选项,但不承诺使用这些选项进行编译?
最近我一直在研究lambda表达式,跟随lambda代码让我感到惊讶:
#include <iostream>
class lambda_this_test
{
private:
int mNumber;
public:
lambda_this_test()
{
mNumber = 11;
};
void print_member()
{
//lambda expression
[this]{mNumber = 12; std::cout<< "mNumber = \n"<<mNumber<<std::endl;};
}
};
int main()
{
lambda_this_test testClass;
testClass.print_member();
}
Run Code Online (Sandbox Code Playgroud)
执行时,看不到任何打印,因此似乎根本不执行lambda表达式的主体,然后我使用gdb来证明这一点,因为print_member()函数中没有代码.
请问我使用lambda有什么问题?
#include <cstdint>
enum struct option_t : uint8_t
{
INVALID = 0,
RESERVED = 1,
TEST_OPTION = 2
};
struct Trace_1
{
option_t option {option_t::INVALID};
uint32_t iter {0};
};
struct Trace_2
{
uint32_t iter {0};
option_t option {option_t::INVALID};
};
int main()
{
Trace_1 trace1{};
Trace_2 trace2{};
trace2 = {0};
trace1 = {0};
}
Run Code Online (Sandbox Code Playgroud)
对于上面的代码,列表初始化对一个临时对象进行了赋值。
Trace_1成员类型相同Trace_2但声明顺序不同;因此分配结果不同。
trace2 = {0}可以编译但是trace1 = {0}不能编译,报错:
<source>:32:12: error: no viable overloaded '='
trace1 = {0};
~~~~~~ ^ ~~~
<source>:15:8: note: …Run Code Online (Sandbox Code Playgroud)