【ThinkingInC++】62、类中指针

类中指针

CopyingWithPointers.cpp

/**
* 书本:【ThinkingInC++】
* 功能:类中指针
* 时间:2014年10月4日14:26:19
* 作者:cutter_point
*/

#include "../require.h"
#include <iostream>
#include <string>

using namespace std;

class Dog
{
    string nm;
public:
    Dog(const string& name) : nm(name) {cout<<"Creating Dog: "<<*this<<endl;}
    //这里有一个类似于拷贝构造函数的函数
    Dog(const Dog* dp, const string& msg) : nm(dp->nm+msg)
    {
        cout<<"Copied dog "<<*this<<" from "<<*dp<<endl;
    }
    ~Dog() {cout<<"Deleting Dog: "<<*this<<endl;}
    void rename(const string& newName)
    {
        nm=newName;
        cout<<"Dog rename to: "<<*this<<endl;
    }
    friend ostream& operator << (ostream& os, const Dog& d)
    {
        return os<<"["<<d.nm<<"]";
    }
};

class DogHouse
{
    Dog* p;
    string houseName;
public:
    DogHouse(Dog* dog, const string house) : p(dog), houseName(house) {}

    //拷贝构造函数 Dog(const string& name) : nm(name) {cout<<"Creating Dog: "<<*this<<endl;}
    // Dog(const Dog* dp, const string& msg) : nm(dp->nm+msg)
    DogHouse(const DogHouse& dh) : p(new Dog(dh.p, "copy-construct")), houseName(dh.houseName+"copy-constructed") {}

    //为了避免自赋值,应该这样重载operator=
    DogHouse& operator=(const DogHouse& dh)
    {
        if(&dh != this)
        {
            p=new Dog(dh.p, "assigned");
            houseName=dh.houseName+" assigned";
        }

        return *this;
    }

    //改掉houseName的名字
    void renameHouse(const string& newName) {houseName=newName; }

    //得到dog这个对象
    Dog* getDog() const {return p;}

    //析构函数
    ~DogHouse() {delete p;}

    friend ostream& operator << (ostream& os, const DogHouse& dh)
    {
        return os<<"["<<dh.houseName<<"] contains "<<*dh.p;
    }
};

int main()
{
    DogHouse fidos(new Dog("Fido"), "FidoHouse");
    cout<<fidos<<endl;
    DogHouse fidos2=fidos;
    cout<<fidos2<<endl;
    fidos2.getDog()->rename("Spot");
    fidos2.renameHouse("SpotHouse");
    cout<<fidos2<<endl;
    fidos=fidos2;
    cout<<fidos<<endl;
    fidos.getDog()->rename("Max");
    fidos2.renameHouse("MaxHouse");

    return 0;
}
时间: 2024-08-04 01:11:02

【ThinkingInC++】62、类中指针的相关文章

c++中基类与派生类中隐含的this指针的分析

先不要看结果,看一下你是否真正了解了this指针? 1 #include<iostream> 2 using namespace std; 3 4 class Parent{ 5 public: 6 int x; 7 Parent *p; 8 public: 9 Parent(){} 10 Parent(int x){ 11 this->x=x; 12 p=this; 13 } 14 virtual void f(){ 15 cout<<"Parent::f()&q

基类中定义的虚函数在派生类中重新定义时,其函数原型,包括返回类型、函数名、参数个数、参数类型及参数的先后顺序,都必须与基类中的原型完全相同 but------&gt; 可以返回派生类对象的引用或指针

您查询的关键词是:c++primer习题15.25 以下是该网页在北京时间 2016年07月15日 02:57:08 的快照: 如果打开速度慢,可以尝试快速版:如果想更新或删除快照,可以投诉快照. 百度和网页 http://bbs.csdn.net/topics/380238133 的作者无关,不对其内容负责.百度快照谨为网络故障时之索引,不代表被搜索网站的即时页面. 首页 精选版块 移动开发 iOS Android Qt WP 云计算 IaaS Pass/SaaS 分布式计算/Hadoop J

多文档中建立一个对话框类,通过这个方法来在其他类中得到对话框对象指针,访问对话框成员

{ // 添加内容 m_pDrawTool = new CDrawToolDlg; m_pDrawTool->Create(IDD_DRAWTOOLS, this); m_pDrawTool->ShowWindow(SW_SHOW); // 让窗口出现在屏幕右下方 CRect dlgRect; CRect mainRect; m_pDrawTool->GetClientRect(&dlgRect); GetWindowRect(mainRect); // 计算显示的坐标 int

【ThinkingInC++】37、更安全的union可以封装在一个类中

/** * 书本:[ThinkingInC++] * 功能:更安全的union可以封装在一个类中 * 时间:2014年9月6日14:53:04 * 作者:cutter_point */ #include<iostream> using namespace std; class SuperVar { //enum没有类型名(他是一个没有加标记的枚举),如果想立即定义enum的实例时, //这种做法是可取的 enum {character, integer, floating_point}vart

转载:C++中两个类中互相包含对方对象的指针问题

原文链接:http://www.cnblogs.com/hanxi/archive/2012/07/25/2608068.html 前几天很不爽,因为C++中两个类中互相包含对方对象的指针编译时提示某一个类未定义...所以我就想啊想,这样也对,我的头文件都有#ifndef的,包含了一次就不能再包含了,以为就实现不了这样的功能,于是就改了设计方案: class A { public: A(B* pB):m_pB(pB) { } private: B* m_pB; }; class B { publ

【C++】通过基类的指针变量访问派生类中由基类继承来的隐藏对象

//<img src="http://img.blog.csdn.net/20150512213309005?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZG91ZG91d2ExMjM0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /> // 可以将一个派生类的对象的地址赋给其基类的指针变量,但

C++学习 之 类中的特殊函数和this指针

1.构造函数 构造函数是一种特殊的函数,它在对象被创建时被调用,与类同名无返回类型,可以被重载.构造函数的可以在类内实现也可以在类外实现. 构造函数的声明类似于下面的代码: class Human { public: Human();//构造函数声明 }; 构造函数在类声明中实现类似于下面的代码: class Human { public: Human () { //构造函数的实现部分 } }; 构造函数在类的声明外实现类似于下面的代码: class Human { public: Human

Spring MVC普通类或工具类中调用service报空空指针的解决办法(调用service报java.lang.NullPointerException)

当我们在非Controller类中应用service的方法是会报空指针,如图: 这是因为Spring MVC普通类或工具类中调用service报空null的解决办法(调用service报java.lang.NullPointerException) 按上述步骤解决完自己的工具类后,你会发现项目运行后仍然报空指针此时你需要在applicationContext.xml 配置文件中添加一行配置文件 如图: 对自己工具类所在的包进行注解扫描,使Spring能够识别自己上面所配置的注解 原文地址:htt

类中函数

[1]空类为什么可以创建对象呢? 示例代码如下: 1 class Test 2 { 3 }; 4 void main() 5 { 6 Test t1; 7 cout<<sizeof(Test)<<endl; //1 8 } 让我们先看看这个例子.既然都没有构造函数,怎么实现对象t1的构建呢? 哦,经过大脑的回旋式搜索,忆得有一本书上说过,当用户定义一个空类(如上)时,编译器就会为这个类默认生成六个方法. 既然这是编译器完成的工作,那我们只要知道是那些方法就好了,其余就学习编译器的结