可能重复的一个实例: Default constructor with empty brackets Instantiate class with or without parentheses? 计划: class Foo
{
public:
Foo (int bar = 1)
{
cout << "bar=" << bar;
}
};
有人可以解释为什么下面的代码的结果是“class B :: 1”? 为什么派生类的虚方法使用基类的默认参数而不是他自己的?对我来说这很奇怪。提前致谢! 代码: #include <iostream>
using namespace std;
class A
{
public:
virtual void func(int a = 1)
{
cout <<
考虑下面的代码: #include <iostream>
class Bar
{
public:
void foo(bool b = false, std::string name = "");
};
void Bar::foo(bool b, std::string name)
{
if (!b)
{
std::cout << "b is
我有这样的代码: struct A{};
template<class T = A>
struct B {
void foo() {}
};
B b; //Error: missing template arguments before 'b'
//Error: expected ';' before 'b'
//More errors
b.foo()