`
yaojingguo
  • 浏览: 208978 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Code to demo Item 31 in Effective C++

阅读更多

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语言常见问题 打包下载 集锦

    thinking in c++、effective c++、writing clean code 高质量程序设计指南 C语言常见问题 打包下载 集锦 这些书都是编写C、C++程序的非常好的指导,想深入,规范地学习C、C++必看这几本书。

    Effective.Modern.C++.zip

    "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)

    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 书号:...

    Effective C++ 3rd Edition 55 Specific Ways to Improve Your Programs and Designs (PDF)

    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

    **C++实现OpenVINO样板Demo** OpenVINO(Open Visual Inference and Neural Network Optimization)是英特尔推出的一个高性能的计算机视觉和深度学习推理框架。它旨在加速从边缘设备到云端的深度学习应用,通过优化...

    Effective C++ 3rd en(with bookmarks)

    ### 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++验证码识别 (CodeDemo)"项目中,我们可以通过提供的代码和资源文件来了解如何使用C++实现验证码识别的过程。下面将详细讨论相关知识点。 1. **图像预处理**: 在验证码识别的初始阶段,我们需要对输入的...

    QR code 生成二维码 demo (VS2008-C++)

    该`QR code 生成二维码 demo`旨在帮助开发者理解如何在C++环境中创建二维码。以下将详细解释这个项目的关键知识点: 1. **库的引入**:为了生成二维码,通常需要使用第三方库,如`Zint`或`libqrencode`。这些库提供...

    C# to C++ Converter

    《C# to C++ Converter——实现跨语言代码迁移的利器》 在软件开发领域,不同编程语言的选择往往取决于项目需求、团队技术栈以及性能优化等多个因素。C#和C++作为两种广泛应用的编程语言,各自拥有独特的特性和优势...

    An introduction to Design Patterns in C++ with Qt 2nd Edition 中英文版 合集

    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++

    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

    Java to C# Converter 转换的效果很好,我实在是没有分了.收1分吧. Demo版的只能转换 试用演示版的Java文件夹转换到C#转换器2000行的代码片段转换到200行一次。... Java to C++ Converter converts Java code to C++

    effective c++

    6. **Item 47: Use templates to eliminate redundant code** - **核心观点**:利用模板来消除重复代码,实现代码的重用。 - **示例应用**:展示如何通过模板类或模板函数来处理相似类型的多种数据类型,提高代码...

    c++编程思想 第一版 源代码 thinking in c++ 1st sourcecode

    《C++编程思想》是C++领域的经典之作,由Bruce Eckel编写,旨在帮助程序员深入理解和掌握C++语言。第一版源代码的提供,对于学习和研究C++的程序员来说是一份宝贵的资源。这本书强调了面向对象编程的概念,并通过...

    Thinking in C++, 2nd Edition and source code

    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

    Tangible Code Converters

    •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++

    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

    Unable to compile C++ source code(解决方案).md

Global site tag (gtag.js) - Google Analytics