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_ ...
随机推荐
- BugPhobia开发终结篇章:Beta阶段第XI次Scrum Meeting
0x01 :Scrum Meeting基本摘要 Beta阶段第十一次Scrum Meeting 敏捷开发起始时间 2015/01/06 00:00 A.M. 敏捷开发终止时间 2016/01/10 0 ...
- Linux内核分析——字符集总结与分析
一. 设置修改系统.应用默认字符集 1. 查看虚拟机的字符集: 由此可见,该虚拟机的字符集为zh_CN.UTF-8. 2. 查看服务器支持的编码方式 3. 修改字符集类型 上图可见,LANG字符 ...
- Leetcode 546. Remove Boxes
题目链接: https://leetcode.com/problems/remove-boxes/description/ 问题描述 若干个有序排列的box和它们的颜色,每次可以移除若干个连续的颜色相 ...
- [福大软工] Z班——Beta现场答辩反馈
Beta现场答辩 互评反馈 各组对于 麻瓜制造者 的评价与建议 队伍名 评价与建议 *** 功能继续完善,二手市场有闲鱼这条大鱼,要继续体现区域性的优势 *** web端的UI很好看,但安卓和web相 ...
- 请求数据传入(SpringMVC)
1. 请求处理方法签名 Spring MVC 通过分析处理方法的签名,HTTP请求信息绑定到处理方法的相应人参中. Spring MVC 对控制器处理方法签名的限制是很宽松的,几乎可以按喜欢的任 ...
- 去掉ambiguous expansion of macro警告
查看原文:http://www.heyuan110.com/?p=1221 用pod install后,pod工程里出现ambiguous expansion of macro的warning,对于有 ...
- 速读《构建之法》(Build to win)有感
通过这两天时间,我粗读了<构建之法>这本书.老实说,对于这样四百多页的一本书,刚开始把这样的任务当作是一种负担,然而当我开始真正接触它时却被它幽默有趣的风格所深深吸引,它不同于以往学习的教 ...
- C语言删除指定文件
C语言的文件操作想必大家都多多少少的有所了解,今天为大家献上删除文件的操作方法.这里我们要用到的是remove(const T& x);x使用代表文件路径及文件名的字符常量来确定需要删除的对象 ...
- phpstorm 注释模板
/** * Created by ${PRODUCT_NAME}. * User: ${USER} * Date: ${DATE} * Time: ${TIME} */
- jetty 介绍以及小例子
Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境.Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布.开发人员可以将 ...