Souce code list.
Address.h
class Address {
};
Date.h
class Date {
};
PersonImpl.h
#include <string>
#include <tr1/memory>
using namespace std;
class Date;
class Address;
class PersonImpl {
public:
PersonImpl(const string& name, const Date& birthday, const Address& addr);
string name() const;
Date birthday() const;
Address address() const;
private:
string theName;
Date theBirthDate;
Address theAddress;
};
PersonImpl.cpp
#include <string>
#include <iostream>
using namespace std;
#include "Date.h"
#include "Address.h"
#include "PersonImpl.h"
PersonImpl::PersonImpl(const string& name, const Date& birthday, const Address& addr)
: theName(name), theBirthDate(birthday), theAddress(addr) {
}
string PersonImpl::name() const {
return theName;
}
Date PersonImpl::birthday() const {
return theBirthDate;
}
Address PersonImpl::address() const {
return theAddress;
}
Person.h
#include <string>
#include <tr1/memory>
using namespace std;
class PersonImpl;
class Date;
class Address;
class Person {
public:
Person(const string& name, const Date& birthday, const Address& addr);
string name() const;
Date birthday() const;
Address address() const;
private:
tr1::shared_ptr<PersonImpl> pImpl;
};
Person.cpp
#include "Date.h"
#include "Address.h"
#include "Person.h"
#include "PersonImpl.h"
Person::Person(const string& name, const Date& birthday, const Address& addr)
: pImpl(new PersonImpl(name, birthday, addr)) {
}
string Person::name() const {
return pImpl->name();
}
Date Person::birthday() const {
return pImpl->birthday();
}
Address Person::address() const {
return pImpl->address();
}
main.cpp
#include <iostream>
using namespace std;
#include "Date.h"
#include "Address.h"
#include "Person.h"
int main(int argc, const char *argv[]) {
string name = "yaojg";
Date d;
Address addr1;
Person p(name, d, addr1);
cout << "here we go" << endl;
return 0;
}
Compile and run:
g++ -c PersonImpl.cpp
g++ -c Person.cpp
g++ *.o -o run
./run
One thing worth of mention is Person.cpp. If I have include Address.h before PersonImpl.h, the following error will show up:
field ‘theAddress’ has incomplete type
The reason is that PersonImpl needs Address to be compiled. So Address defintion must occurs textually before PersonImpl definition. At this point, C++ is different from Java. In Java, textual layout is unimportant in most of situations.
分享到:
相关推荐
thinking in c++、effective c++、writing clean code 高质量程序设计指南 C语言常见问题 打包下载 集锦 这些书都是编写C、C++程序的非常好的指导,想深入,规范地学习C、C++必看这几本书。
"After I learned the C++ basics, I then learned how to use C++ in production code from Meyer's series of Effective C++ books. Effective Modern C++ is the most important how-to book for advice on key ...
C++: Effective Modern C++ (C++ 11, C++ 14) (guide,C Programming, HTML, Javascript, Programming,all,internet, Coding, CSS, Java, PHP Vol 1) By 作者: Paul Laurence ISBN-10 书号: 1547133244 ISBN-13 书号:...
If you want to really and truly understand C++, you must read Effective C++. After first reading Effective C++, I had a totally new appreciation for and understanding of C++. And it's not just me: ...
**C++实现OpenVINO样板Demo** OpenVINO(Open Visual Inference and Neural Network Optimization)是英特尔推出的一个高性能的计算机视觉和深度学习推理框架。它旨在加速从边缘设备到云端的深度学习应用,通过优化...
### Effective C++ 3rd Edition – Key ... As you progress through the "Effective C++ 3rd en (with bookmarks)," keep this perspective in mind to fully grasp the nuances and best practices of the language.
在这个"C++验证码识别 (CodeDemo)"项目中,我们可以通过提供的代码和资源文件来了解如何使用C++实现验证码识别的过程。下面将详细讨论相关知识点。 1. **图像预处理**: 在验证码识别的初始阶段,我们需要对输入的...
该`QR code 生成二维码 demo`旨在帮助开发者理解如何在C++环境中创建二维码。以下将详细解释这个项目的关键知识点: 1. **库的引入**:为了生成二维码,通常需要使用第三方库,如`Zint`或`libqrencode`。这些库提供...
《C# to C++ Converter——实现跨语言代码迁移的利器》 在软件开发领域,不同编程语言的选择往往取决于项目需求、团队技术栈以及性能优化等多个因素。C#和C++作为两种广泛应用的编程语言,各自拥有独特的特性和优势...
An introduction to Design Patterns in C++ with Qt 2nd Edition 中文版 C++ qt 设计模式 第二版, 中英文版 合集 Master C++ “The Qt Way” with Modern Design Patterns and Efficient Reuse This fully ...
Functional Programming in C++ teaches developers the practical side of functional programming and the tools that C++ provides to develop software in the functional style. This in-depth guide is full ...
Java to C# Converter 转换的效果很好,我实在是没有分了.收1分吧. Demo版的只能转换 试用演示版的Java文件夹转换到C#转换器2000行的代码片段转换到200行一次。... Java to C++ Converter converts Java code to C++
6. **Item 47: Use templates to eliminate redundant code** - **核心观点**:利用模板来消除重复代码,实现代码的重用。 - **示例应用**:展示如何通过模板类或模板函数来处理相似类型的多种数据类型,提高代码...
《C++编程思想》是C++领域的经典之作,由Bruce Eckel编写,旨在帮助程序员深入理解和掌握C++语言。第一版源代码的提供,对于学习和研究C++的程序员来说是一份宝贵的资源。这本书强调了面向对象编程的概念,并通过...
Thinking in C++, Volume 1, 2nd Edition Completed January 13, 2000 Bruce Eckel, President, MindView, Inc. Thinking in C++ 2nd edition Volume 2: Standard Libraries & Advanced Topics
•C++ to C# Converter converts C++ code to C# •C++ to VB Converter converts C++ code to VB •C++ to Java Converter converts C++ code to Java •C# to Java Converter converts C# code to Java •VB ...
Game Programming in C++ is a practical, hands-on approach to programming 3D video games in C++. Modeled on Sanjay Madhav’s game programming courses at USC, it’s fun, easy, practical, hands-on, and ...
Unable to compile C++ source code(解决方案).md