C++中hpp的适用
本文第一部分转载百度百科,在此感谢百度。第二部分示例为独立编写。
hpp,其实质就是将.cpp的实现代码混入.h头文件当中,定义与实现都包含在同一文件,则该类的调用者只需要include该hpp文件即可,无需再将cpp加入到project中进行编译。而实现代码将直接编译到调用者的obj文件中,不再生成单独的obj,采用hpp将大幅度减少调用 project中的cpp文件数与编译次数,也不用再发布烦人的lib与dll,因此非常适合用来编写公用的开源库。
1、是Header Plus Plus 的简写。
2、与*.h类似,hpp是C++程序头文件 。
3、是VCL专用的头文件,已预编译。
4、是一般模板类的头文件。
5、一般来说,*.h里面只有声明,没有实现,而*.hpp里声明实现都有,后者可以减少.cpp的数量。
6、*.h里面可以有using namespace std,而*.hpp里则无。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <typeinfo>
#include "adapter.hpp"
using namespace std; struct Base {}; // non-polymorphic
struct Derived : Base {}; struct Base2 { virtual void foo() {} }; // polymorphic
struct Derived2 : Base2 {}; int main() {
Test ttdata;
ttdata.data = ;
Test ttdata2;
ttdata2.data = ;
TestCom::compare<struct Test>(ttdata, ttdata2); A a;
Test2 tt;
a.foo<Test2>(tt);
Test tst1;
Test2 tst2;
a.foo<Test,Test2>(tst1, tst2);
int myint = ;
//int intx = 100;
if(typeid(myint).name() == typeid(int).name())
cout << "same:" << typeid(myint).name() << endl;
std::string mystr = "string";
double *mydoubleptr = NULL; std::cout << "myint has type: " << typeid(myint).name() << '\n'
<< "mystr has type: " << typeid(mystr).name() << '\n'
<< "mydoubleptr has type: " << typeid(mydoubleptr).name() << '\n'; // std::cout << myint is a glvalue expression of polymorphic type; it is evaluated
const std::type_info& r1 = typeid(std::cout << myint);
std::cout << '\n' << "std::cout<<myint has type : " << r1.name() << '\n'; // std::printf() is not a glvalue expression of polymorphic type; NOT evaluated
const std::type_info& r2 = typeid(printf("%d\n", myint));
std::cout << "printf(\"%d\\n\",myint) has type : " << r2.name() << '\n'; // Non-polymorphic lvalue is a static type
Derived d1;
Base& b1 = d1;
std::cout << "reference to non-polymorphic base: " << typeid(b1).name() << '\n'; Derived2 d2;
Base2& b2 = d2;
std::cout << "reference to polymorphic base: " << typeid(b2).name() << '\n'; try {
// dereferencing a null pointer: okay for a non-polymorphic expression
std::cout << "mydoubleptr points to " << typeid(*mydoubleptr).name() << '\n';
// dereferencing a null pointer: not okay for a polymorphic lvalue
Derived2* bad_ptr = NULL;
std::cout << "bad_ptr points to... ";
std::cout << typeid(*bad_ptr).name() << '\n';
} catch (const std::bad_typeid& e) {
std::cout << " caught " << e.what() << '\n';
}
}
main.cpp
#include "solution.h" class TestCom{
public:
template<typename T>
static void compare(const T& v1, const T& v2);
}; template<typename T>
void TestCom::compare(const T& v1, const T& v2)
{
if(typeid(T) == typeid(struct Test))
{
if(v1.data < v2.data)
cout << v1.data << " " << v2.data<< " v1 < v2" << endl;
if(v1.data > v2.data)
cout << v1.data << " " << v2.data<< " v1 > v2" << endl;
}else{
printf("T is not Test : %s\n", typeid(Test2).name());
}
} //struct A{
// void func();
// template < typename T1 > void foo(T1 param);
//}; struct A
{
void func(){cout << "func()" << endl;}
template < typename T1 > void foo(T1 param) {
//if(typeid(Test2).name() == typeid(param).name()){
if(typeid(Test2) == typeid(T1)){
param.data = ;
string str("Test2 copy");
strncpy(param.buf, str.c_str(), str.size());
cout << "int:" << param.data << " " << param.buf << " type:" << typeid(param).name()<< endl;
}else{
printf("err, though equal,but not reg, %s\n", typeid(T1).name());
}
} template < typename T1, typename T2 > void foo(T1 param, T2 param2) {
//if(typeid(Test2).name() == typeid(param).name()){
if(typeid(T2) == typeid(T1)){
param.data = ;
param2.data = ;
printf("param:%d, param2:%d\n", param.data, param2.data);
}else if ( typeid(Test2) == typeid(param2)) {
param2.data = ;
string str("Test2 copy");
strncpy(param2.buf, str.c_str(), str.size());
cout << "int:" << param2.data << " " << param2.buf << " type:" << typeid(param2).name()<< endl;
} else{
printf("err, though equal,but not reg, %s\n", typeid(T1).name());
}
}
};
AdapterEx.hpp
#ifndef _PROXY_H_
#define _PROXY_H_
#include <string>
#include <string.h>
using namespace std; struct Test{
int data;
//string str;
float f;
}; struct Test2{
int data;
//string str;
char buf[];
Test2(){
data = ;
memset(buf, 0x00, sizeof(buf));
}
}; #endif
solution.h
C++中hpp的适用的更多相关文章
- Python开源框架
info:更多Django信息url:https://www.oschina.net/p/djangodetail: Django 是 Python 编程语言驱动的一个开源模型-视图-控制器(MVC) ...
- caffe中的filler.hpp源码的作用:
filler.hpp文件:(它应该没有对应的.cpp文件,一切实现都是在头文件中定义的,可能是因为filler只分在网络初始化时用到那么一次吧) 1,首先定义了基类:Filler,它包括:一个纯虚函数 ...
- c++中的.hpp文件
http://blog.chinaunix.net/uid-24118190-id-75239.html hpp,其实质就是将.cpp的实现代码混入.h头文件当中,定义与实现都包含在同一文件,则该类的 ...
- C++中.cpp和.hpp的区别
原文地址:https://blog.csdn.net/qzx9059/article/details/89210571 c++中 cpp和hpp我们可以将所有东西都放在一个.cpp文件内,编译器会将这 ...
- android studio 使用 jni 编译 opencv 完整实例 之 图像边缘检测!从此在andrid中自由使用 图像匹配、识别、检测
目录: 1,过程感慨: 2,运行环境: 3,准备工作: 4,编译 .so 5,遇到的关键问题及其解决方法 6,实现效果截图. (原创:转载声明出处:http://www.cnblogs.com/lin ...
- 基于Caffe的DeepID2实现(中)
小喵的唠叨话:我们在上一篇博客里面,介绍了Caffe的Data层的编写.有了Data层,下一步则是如何去使用生成好的训练数据.也就是这一篇的内容. 小喵的博客:http://www.miaoerduo ...
- [Modern OpenGL系列(四)]在OpenGL中使用Shader
本文已同步发表在CSDN:http://blog.csdn.net/wenxin2011/article/details/51347440 在上一篇文章中已经介绍了OpenGL窗口的创建.本文接着说如 ...
- 学习 opencv---(8)非线性滤波:中值滤波,双边滤波
正如我们上一篇文章中讲到的,线性滤波可以实现很多种不同的图像变换.然而非线性滤波,如中值滤波器和双边滤波器,有时可以达到更好的实现效果. 邻域算子的其他一些例子还有对 二值图像进行操作的形态学算子,用 ...
- opencv中的SIFT,SURF,ORB,FAST 特征描叙算子比较
opencv中的SIFT,SURF,ORB,FAST 特征描叙算子比较 参考: http://wenku.baidu.com/link?url=1aDYAJBCrrK-uk2w3sSNai7h52x_ ...
随机推荐
- (第十二周)final预发布视频
项目名:食物链教学工具 组名:奋斗吧兄弟 组长:黄兴 组员:李俞寰.杜桥.栾骄阳.王东涵 Final阶段视频发布 平台:优酷 链接:http://v.youku.com/v_show/id_XMTg0 ...
- Oracle的安装与配置
好久不来博客园了,有种熟悉而又陌生的感觉. 今天我装一下Oracle数据库,从头开始,因为昨天在虚拟机装了,不能用,卸掉了,系统也卸掉了,今天重新装,包括系统. 系统装好了,Oracle准备好了. 这 ...
- 浏览器的F5和Ctrl+F5
在浏览器里中,按F5键和按F5同时按住Ctrl键(简称Ctrl+F5),效果是不同,到底两者有什么区别呢? 假如我第一次访问过http://localhost/home,这个网页是个动态网页,每次访问 ...
- [转帖] iptables之四表五链
iptables之四表五链 http://www.cnblogs.com/clouders/p/6544584.html mark 学习一下 对防火墙一无所知.. iptables可谓是SA的看家本领 ...
- python对redis的常用操作 上 (对列表、字符串、散列结构操作)
这里的一切讨论均基于python的redis-py库. 安装使用: pip install redis 然后去获取一个redis客户端: redis_conn = redis.Redis(host=R ...
- iOS 页面之间的转场动画控制器间的转换
CATransition类实现层的转场动画.你可以从一组预定义的转换或者通过提供定制的CIFilter实例来指定转场效果. 例如:控制器之间的跳转 LoginViewController *myVC ...
- 训练题(代码未检验)(序列前k大和问题)
大厦 Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submission ...
- Mysql误删表中数据与误删表的恢复方法
由于头两天面试时被问了这样一个问题,如果某同事误删了某个表,你该怎么恢复? 当时想了一下,因为博主没有遇到过这个问题,但是也多少了解一些,所以就回答通过mysql的binlog日志进行恢复. 面试官当 ...
- MT【57】2017联赛一试解答倒数第二题:一道不等式的最值
注:康拓诺维奇不等式的应用
- Linux Install geoip
安装方法 http://php.net/manual/en/geoip.installation.phpgeoip中的PHP函数介绍:http://php.net/manual/en/book.geo ...