博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在一个类里面调用别一个类的函数
阅读量:4682 次
发布时间:2019-06-09

本文共 1087 字,大约阅读时间需要 3 分钟。

sample:

//part A//tmp.hpp#ifndef TEMP_HPP#define TEMP_HPPclass tmp {      public:              void print();       private:               std::string str2;};#endif

 

// part B//tmp.cpp#include 
#include "tmp.hpp"#include "test.hpp"using namespace std;void tmp::print() { str2 = " this is the tmp print func"; cout << str2 << endl; test::show();}int main() { tmp temp; temp.print(); return 0;}

 

//part C//test.hpp #ifndef TEST_HPP#define TEST_HPP#include 
using namespace std;class test { test(); ~test(); public: static void show(); //private:// string str;};#endif

 

//part D//test.cpp #include 
#include "test.hpp"using namespace std;test::test() {// str = "this is test";}test::~test() {}void test::show() { string str = "this is test func"; cout << str << endl;}

为了能在tmp::print()调用test::show()函数

需要在tmp.cpp中调用 "test.hpp"

且test::show()的定义必须为 static;

 

编译命令:

gcc -o main tmp.cpp test.cpp

运行结果如下:

$ ./main this is the tmp print functhis is test func

 

 

 

 

 

 

转载于:https://www.cnblogs.com/sn-dnv-aps/p/3370981.html

你可能感兴趣的文章
RTP Payload Format for Transport of MPEG-4 Elementary Streams over http
查看>>
Aurora — 一个在 MSOffice 内输入 LaTeX 公式的很好用插件
查看>>
关于sql优化的一个小总结
查看>>
Java语言中的正则表达式
查看>>
Java环境变量设置
查看>>
【JBPM4】判断节点decision 方法3 handler
查看>>
filter 过滤器(监听)
查看>>
Linux进程间通信---共享内存
查看>>
Computer Information
查看>>
交换机/路由器上的 S口 F口 E口
查看>>
P1298(矩阵切割)DP
查看>>
wzplayer for delphi demo截图
查看>>
团队第二周:SRS文档
查看>>
Zookeeper的安装与使用:
查看>>
密码策略限制最大与最小长度
查看>>
正则表达式模式
查看>>
使用iframe实现同域跨站提交数据
查看>>
Mouse点击之后,复制GridView控件的数据行
查看>>
ASP.NET开发,从二层至三层,至面向对象 (2)
查看>>
如何查看自己电脑支持OpenGL core版本
查看>>