为什么会有“error: declaration of ‘A ca’ shadows a parameter”的错误?
我看书上写派生类构造函数只能合法调用其直接基类的构造函数,如果调用其非直接基类的构造函数是错误的。我自己试着写了这样的一个多继承函数,具体见下面代码,但是编译之后报如下错误:
4_4_4.cpp: In constructor ‘C::C(int, int, int)’:
4_4_4.cpp:37: error: declaration of ‘A ca’ shadows a parameter
不知道为什么,请大家帮我解释一下!
#include <iostream>
using namespace std;
class A
{
public:
A (int aa = 0) : a(aa)
{
cout << "call A ()" << endl;
}
private:
int a;
};
class B : public A
{
public:
B (int ba = 0, int bb = 0) : A(ba), b(bb)
{
cout << "call B ()" << endl;
}
private:
int b;
};
class C : public B
{
public:
C (int ca = 0, int cb = 0, int cc = 0)
: B(ca, cb), c(cc)
{
A (ca);
cout << "call C ()" << endl;
}
private:
int c;
};
int main ()
{
C tmp (1, 2, 3);
return 0;
}
C++
#include <iostream>
using namespace std;
class A
{
public:
A (int aa = 0) : a(aa)
{
cout << "call A ()" << endl;
}
private:
int a;
};
class B : public A
{
public:
B (int ba = 0, int bb = 0) : A(ba), b(bb)
{
cout << "call B ()" << endl;
}
private:
int b;
};
class C : public B
{
public:
C (int ca = 0, int cb = 0, int cc = 0)
: A(ca), B(ca, cb), c(cc)
{
cout << "call C ()" << endl;
}
private:
int c;
};
int main ()
{
C tmp (1, 2, 3);
return 0;
}
function foo(const string& str) { /*..do something with str..*/ }
foo(string("wtf")); // 这里的 string("wtf") 与 B(ca, cb) 异曲同工