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_ ...
随机推荐
- 安装Visual Studio 2013以及简单使用
首先,在网上找到安装Visual Studio 2013的教程以及相关软件资源http://jingyan.baidu.com/article/09ea3ede3b2496c0afde3944.htm ...
- Alpha冲刺之事后诸葛亮
组长博客 作业博客 项目Postmortem 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件针对的是福大学子来到食堂会犹豫不决无法决定吃什么 ...
- JAVA面对对象(四)——抽象类
抽象类的作用类似“模板”,可以根据它的格式来修改.创建新的类:但是不能直接由抽象类创建对象只能通过抽象类派生出新的类,再由它来创建对象:抽象类的使用同样是单继承,即一个子类只能继承一个抽象类 抽象类的 ...
- Enterprise Integration Patterns
https://camel.apache.org/enterprise-integration-patterns.html 企业集成模式,各种模式算法,挺棒的. https://camel.apach ...
- An internal error has occurred. Java heap space
http://stackoverflow.com/questions/11001252/running-out-of-heap-space issue: I am having a heap spac ...
- RAID 磁盘阵列说明
Copy From wiki RAID档次 最少硬盘 最大容错 可用容量 读取性能 写入性能 安全性 目的 应用产业 单一硬盘 (引用) 0 1 1 1 无 JBOD 1 0 n 1 1 无( ...
- Linux环境(CentOS)安装维护过程中用到的常见命令
1. yum 安装时需要选择仓库 一般的路径 /etc/repos.d/ 2. 查看安装了哪些软件的 yum list |grep docker installed 的就是已经安装的软件. 3. 卸载 ...
- 项目引入android-support-v7-appcompat遇到的问题,no resource found that matches the given name 'android:Theme.AppCompat.Light'
一.问题 今天准备使用v7包中的ToolBar来用,但是在styles.xml中引入Theme.AppCompat.Light的时候,报错“no resource found that matches ...
- java中父进程与子进程
http://blog.csdn.NET/seelye/article/details/8269705
- Jenkins中使用GitLab的配置
1. 概述 在Jenkins中从GitLab上拉取代码进行打包或测试. 2. 安装 Jenkins和GitLab默认已经安装好,安装过程此处不再赘述. 在Jenkins上安装Git和Gitlab插件, ...