小弟问一个有关构造函数的有关问题
小弟问一个有关构造函数的问题
在Professional C++ 高级编程一书上有一个这样的例子,名曰:装饰器的实现
#include <iostream>
#include <string>
using namespace std;
class Paragraph
{
public:
Paragraph(const string& inInitialTest) : mText(inInitialTest){}
virtual string getHTML() const {return mText;}
protected:
string mText;
};
class ItalicParagraph : public Paragraph
{
public:
ItalicParagraph(const Paragraph& inParagraph) : Paragraph( " "), mWrapped(inParagraph){}
virtual string getHTML() const{}
protected:
const Paragraph& mWrapped;
};
class BoldParagraph : public Paragraph
{
public:
BoldParagraph(const Paragraph& inParagraph) :
Paragraph( " "), mWrapped(inParagraph){}
virtual string getHTML() const{
return " <B> " + mWrapped.getHTML() + " </B> ";
}
BoldParagraph(const BoldParagraph& inParagraph):Paragraph( " "), mWrapped(inParagraph){}
protected:
const Paragraph& mWrapped;
};
int main(){
Paragraph p( "A party? For me? Thanks! ");
cout < < ItalicParagraph(BoldParagraph(p)).getHTML() < < endl < < endl;
cout < < BoldParagraph(BoldParagraph(p)).getHTML() < < endl;
return 0;
}
以上代码的执行结果有点不解:
<I> <B> A party? For me? Thanks! </B> </I>
<B> A party? For me? Thanks! </B> <----为什么是这个样子呢?
然后我在Paragraph类构造函数中加了输出,发现ItalicParagraph(BoldParagraph(p))该调用会调用两次Paragraph类构造函数,而BoldParagraph(BoldParagraph(p))只调用一次,这是为什么呢?是编译器什么了什么手脚,还是C++标准是这样规定的?还有为什么以一次要调用两次Paragraph类构造函数呢?
------解决方案--------------------
当然是这样的了,ItalicParagraph(BoldParagraph(p))中,在构造BoldParagraph时执行一次,在构造ItalicParagraph时又执行一次。
建议楼主看看虚基类部分的内容,里面会讲这些东西的。
------解决方案--------------------
你把BoldParagraph 改成这样,编译一下看,要是没有调用拷贝构造函数的话,是可以编译过去的,要是调用了拷贝构造函数,那么就编译不过去
class BoldParagraph : public Paragraph
{
BoldParagraph(const BoldParagraph& inParagraph);
public:
BoldParagraph(const Paragraph& inParagraph) :
Paragraph( " "), mWrapped(inParagraph)
{
}
virtual string getHTML() const{
return " <B> " + mWrapped.getHTML() + " </B> ";
}
protected:
const Paragraph& mWrapped;
};
------解决方案--------------------
execptional C++ item 42
在Professional C++ 高级编程一书上有一个这样的例子,名曰:装饰器的实现
#include <iostream>
#include <string>
using namespace std;
class Paragraph
{
public:
Paragraph(const string& inInitialTest) : mText(inInitialTest){}
virtual string getHTML() const {return mText;}
protected:
string mText;
};
class ItalicParagraph : public Paragraph
{
public:
ItalicParagraph(const Paragraph& inParagraph) : Paragraph( " "), mWrapped(inParagraph){}
virtual string getHTML() const{}
protected:
const Paragraph& mWrapped;
};
class BoldParagraph : public Paragraph
{
public:
BoldParagraph(const Paragraph& inParagraph) :
Paragraph( " "), mWrapped(inParagraph){}
virtual string getHTML() const{
return " <B> " + mWrapped.getHTML() + " </B> ";
}
BoldParagraph(const BoldParagraph& inParagraph):Paragraph( " "), mWrapped(inParagraph){}
protected:
const Paragraph& mWrapped;
};
int main(){
Paragraph p( "A party? For me? Thanks! ");
cout < < ItalicParagraph(BoldParagraph(p)).getHTML() < < endl < < endl;
cout < < BoldParagraph(BoldParagraph(p)).getHTML() < < endl;
return 0;
}
以上代码的执行结果有点不解:
<I> <B> A party? For me? Thanks! </B> </I>
<B> A party? For me? Thanks! </B> <----为什么是这个样子呢?
然后我在Paragraph类构造函数中加了输出,发现ItalicParagraph(BoldParagraph(p))该调用会调用两次Paragraph类构造函数,而BoldParagraph(BoldParagraph(p))只调用一次,这是为什么呢?是编译器什么了什么手脚,还是C++标准是这样规定的?还有为什么以一次要调用两次Paragraph类构造函数呢?
------解决方案--------------------
当然是这样的了,ItalicParagraph(BoldParagraph(p))中,在构造BoldParagraph时执行一次,在构造ItalicParagraph时又执行一次。
建议楼主看看虚基类部分的内容,里面会讲这些东西的。
------解决方案--------------------
你把BoldParagraph 改成这样,编译一下看,要是没有调用拷贝构造函数的话,是可以编译过去的,要是调用了拷贝构造函数,那么就编译不过去
class BoldParagraph : public Paragraph
{
BoldParagraph(const BoldParagraph& inParagraph);
public:
BoldParagraph(const Paragraph& inParagraph) :
Paragraph( " "), mWrapped(inParagraph)
{
}
virtual string getHTML() const{
return " <B> " + mWrapped.getHTML() + " </B> ";
}
protected:
const Paragraph& mWrapped;
};
------解决方案--------------------
execptional C++ item 42