Skip to content

四火的唠叨

一个纯正程序员的啰嗦

Menu
  • 所有文章
  • About Me
  • 关于四火
  • 旅行映像
  • 独立游戏
  • 资源链接
Menu

C++学习杂记

Posted on 04/21/200806/23/2019 by 四火

c  智能指针:auto_ptr 和 shared_ptr:

auto_ptr 是个 pointer-like 对象,也就是所谓的 “智能指针”,其析构函数会自动调用。

std::auto_ptr<Investment> ptr1(new Investment());
//之后,如果:
std::auto_ptr<Investment> ptr2(ptr1);
//或者:
ptr2=ptr1;
//那么 ptr2 指向那个对象,ptr1 置空。

shared_ptr 类似,但它可以追踪有多少指针指向该对象,并在无指针指向时析构。
这时的拷贝:
ptr2=ptr1;
结果则是两个指针指向同一对象。
auto_ptr 和 shared_ptr 一般都不要指向数组,否则它们只会析构掉数组首元素!

类数组和数组动态申请:

类数组:

class A{
public:
    A():a(0){
        cout<<a;
    }
    A(int t):a(t){
        cout<<a;
    }
    int a;
};
void main(){
    int p=3;
    A arr[2]={A(),A()};
    A brr[2]={A(1),A(2)};
    int n=1;
    A *a = new A[n];
}

数组的动态申请:

int n;
cin>>n;
float *ar = new float[n];

//二维的:
int **a=new int *[3];int i,j;
cout<<"请输入 3*3 矩阵: ";

for(i=0;i<3;i++){
    a[i]=new int[3];
    for(j=0;j<3;j++)
        cin>>*(a[i]+j);
}

返回引用类型的函数:

/*关于引用类型返回值之函数的使用*/
#include<iostream>
using namespace std;

int g=0;
int& func(){
    return g;
}
void main(){
    int t=func();
    cout<<t<<endl;

    func()++;
    cout<<g<<endl;

    func()=30;
    cout<<g<<endl;
}

总结几种数据类型转换的实现:

#include<stdio.h>
#include<stdlib.h>

#include <iostream>
using namespace std;

void main(){
//int 和 char*互转:
    //int 型转 char*型,保持 int 数值显示等于结果字符串,如 13 转成"13"
    int _int=123;
    char _char[10];
    itoa(_int,_char,10);
    printf("%s ",_char);
    //itoa() 函数有 3 个参数:第一个参数是要转换的数字,第二个参数是目标字符串,
    //第三个参数是转移数字时所用 的基数。

    //char*型转 int 型:
    int _int2=atoi(_char);
    printf("%d ",_int2);
    //如果使用 int _int2=(int)_char,则是_char 的地址变成 int
    //而如果使用 int _int2=(int)(_char),则是这句话是把_char[0] 变成 asc 码的 int


//char*和 string 互转:
    //char*转 string:
    string s1=(string)_char;
    string s2=_char;
    //这两种方法都是可以的

    //string 转 char*,两个方法
    const char* _char2=s1.c_str();
    printf("%s ",_char2);
    const char* _char3=s1.data();
    printf("%s ",_char3);

//string 和 int 互转:
    //string 转 int,没有直接的好方法,这里给两个方便的,其实就是先转成 char*,再转成 int
    int _int4=atoi(s1.data());
    int _int5=atoi(s2.c_str());
    printf("%d ",_int4);
    printf("%d ",_int5);

    //int 转 string,也同理,就不写了
    
}

多态 (Polymorphism) 和对象切割 (Object Slicing) 的小例子:

#include <iostream>
using namespace std;
class Grandfather{
public:
    virtual void display()=0;
    void run(){                    
        cout<<"Grandfather Run!!! ";
    }
};
class Father:public Grandfather{
public:
    int fatherValue;
    void display(){
        cout<<"Father Display!! ";
    }
    void run(){
        cout<<"Father Run!!! ";
    }
};
class Uncle:public Grandfather{
public:
    int uncleValue;
    void display(){
        cout<<"Uncle Display!! ";
    }
    void run(){
        cout<<"Uncle Run!!! ";
    }
};
class Son:public Father{
public:
    int sonValue;
    void display(){
        cout<<"Son Display!! ";
    }
};
void main(){
    Grandfather* grandfather_pt=NULL;
    Father* father_pt=NULL;
    Son* son_pt=NULL;

    Father father;
    Uncle uncle;
    Son son;

    cout<<"静态绑定 不用 virtual 关键字: ";
    grandfather_pt=&uncle;
    grandfather_pt->run();
    grandfather_pt=&father;
    grandfather_pt->run();

    cout<<" 动态绑定 纯虚函数: ";
    grandfather_pt=&uncle;
    grandfather_pt->display();
    grandfather_pt=&father;
    grandfather_pt->display();

    cout<<" 指针强制转换 ";
    ( (Father*)(&son) )->display();
//    ( (Father*)(&son) )->run();

    cout<<" 对象强制转换 ";
    ((Father)son).display();        //只允许 upcasting,不允许 downcasting
    cout<<" 编译器为了防止对象切割的发生,自动调用拷贝构造函数,因此,比较地址: ";
    cout<<( father_pt=&((Father)son) )<<endl;    //注意优先级
    cout<<&son<<endl;
    cout<<"发现二者不同! ";
}

文章未经特殊标明皆为本人原创,未经许可不得用于任何商业用途,转载请保持完整性并注明来源链接 《四火的唠叨》

×Scan to share with WeChat

你可能也喜欢看:

  1. 泛型趣谈
  2. 看 JDK 源码,解几个疑问
  3. 关于“ 异步”,从 Amazon 的工作流框架中获得的思考
  4. C++程序员和 Java 程序员的差异
  5. 多重继承的演变

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

订阅·联系

四火,啰嗦的程序员一枚,现居西雅图

Amazon Google Groovy Hadoop Haskell Java JavaScript LeetCode Oracle Spark 互联网 亚马逊 前端 华为 历史 同步 团队 图解笔记 基础设施 工作 工作流 工具 工程师 应用系统 异步 微博 思考 技术 数据库 曼联 测试 生活 眼界 程序员 管理 系统设计 缓存 编程范型 美股 英语 西雅图 设计 问题 面向对象 面试

分类

  • Algorithm and Data Structure (30)
  • Concurrency and Asynchronization (6)
  • System Architecture and Design (43)
  • Distributed System (18)
  • Tools Frameworks and Libs (13)
  • Storage and Data Access (8)
  • Front-end Development (33)
  • Programming Languages and Paradigms (55)
  • Testing and Quality Assurance (4)
  • Network and Communication (6)
  • Authentication and Authorization (6)
  • Automation and Operation Excellence (13)
  • Machine Learning and Artificial Intelligence (6)
  • Product Design (7)
  • Hiring and Interviews (14)
  • Project and Team Management (14)
  • Engineering Culture (17)
  • Critical Thinking (25)
  • Career Growth (57)
  • Life Experience and Thoughts (45)

推荐文章

  • 谈谈分布式锁
  • 常见分布式系统设计图解(汇总)
  • 系统设计中的快速估算技巧
  • 从链表存在环的问题说起
  • 技术面试中,什么样的问题才是好问题?
  • 从物理时钟到逻辑时钟
  • 近期面试观摩的一些思考
  • RSA 背后的算法
  • 谈谈 Ops(汇总 + 最终篇):工具和实践
  • 不要让业务牵着鼻子走
  • 倔强的程序员
  • 谈谈微信的信息流
  • 评审的艺术——谈谈现实中的代码评审
  • Blog 安全问题小记
  • 求第 K 个数的问题
  • 一些前端框架的比较(下)——Ember.js 和 React
  • 一些前端框架的比较(上)——GWT、AngularJS 和 Backbone.js
  • 工作流系统的设计
  • Spark 的性能调优
  • “残酷” 的事实
  • 七年工作,几个故事
  • 从 Java 和 JavaScript 来学习 Haskell 和 Groovy(汇总)
  • 一道随机数题目的求解
  • 层次
  • Dynamo 的实现技术和去中心化
  • 也谈谈全栈工程师
  • 多重继承的演变
  • 编程范型:工具的选择
  • GWT 初体验
  • java.util.concurrent 并发包诸类概览
  • 从 DCL 的对象安全发布谈起
  • 不同团队的困惑
  • 不适合 Hadoop 解决的问题
  • 留心那些潜在的系统设计问题
  • 再谈大楼扔鸡蛋的问题
  • 几种华丽无比的开发方式
  • 我眼中的工程师文化
  • 观点的碰撞
  • 谈谈盗版软件问题
  • 对几个软件开发传统观点的质疑和反驳
  • MVC 框架的映射和解耦
  • 编程的未来
  • DAO 的演进
  • 致那些自嘲码农的苦逼程序员
  • Java 多线程发展简史
  • 珍爱生命,远离微博
  • 网站性能优化的三重境界
  • OSCache 框架源码解析
  • “ 你不适合做程序员”
  • 画圆画方的故事

近期评论

  • panshenlian.com on 初涉 ML Workflow 系统:Kubeflow Pipelines、Flyte 和 Metaflow
  • panzhixiang on 关于近期求职的近况和思考
  • Anonymous on 闲聊投资:亲自体验和护城河
  • 四火 on 关于近期求职的近况和思考
  • YC on 关于近期求职的近况和思考
  • mafulong on 常见分布式基础设施系统设计图解(四):分布式工作流系统
  • 四火 on 常见分布式基础设施系统设计图解(八):分布式键值存储系统
  • Anonymous on 我裸辞了
  • https://umlcn.com on 资源链接
  • Anonymous on 我裸辞了
© 2025 四火的唠叨 | Powered by Minimalist Blog WordPress Theme