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_ ...
随机推荐
- 把cnblogs变成简书 - cnblogs博客自定义皮肤css样式
吐槽 博客园cnblogs作为老牌的IT技术博客类网站,为广大的开发者提供了非常不错的学习交流平台. 虽然博客内容才是重点,但是如果有赏心悦目的页面不更好吗! cnblogs可以更换博客模板,并且提供 ...
- Visual Studio2015安装过程以及单元测试
安装环境: 安装版本: Visual Studio2015 安装过程: 因为我是在第一次老师安排的作业的时候感觉VC++6.0不如VS方便所以才装的Visual Studio2015,又安装了点插件, ...
- 防止重复提交demo
利用session防止重复提交 思路: 前端控制:在点击提交按钮后设置按钮不可用. 后台控制:利用session,在初次进入表单页面的时候前生成一个随机token,将token保存到session并返 ...
- 6-Python3从入门到实战—基础之数据类型(元组-Tuple)
Python从入门到实战系列--目录 元组的定义 定义元组只需要在括号中添加元素,并使用逗号隔开即可 tup = ('Python','Java','C++','Kotlin') 元组与列表的区别 P ...
- yii框架通过IP地址来使用gii
这里使用的YII框架的版本是2.0.13 详情请参考官方文档:用Gii生成代码 使用gii的主要步骤 1.生成模型(Model Generator) 2.生成CRUD代码 注意点 1.在生成CURD代 ...
- Facebook 50%用户是虚假账号?我觉得可以更高!
0x00 背景 今天下午看新闻时,无意看到一条关于facebook虚假帐号(fake account)消息: 一下子就被这标题吸引了眼球,因为作为一个第三方机构,能够对facebook这样如此海量的帐 ...
- React 支持JS
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- Chrome disable cache & clear memory cache
Chrome disable cache & clear memory cache disable cache
- jquery添加刪除
創建元素的方法:jquery.javascript/dom,html/text var txt1="<p>Text.</p>"; ...
- Java之JSON处理(JSONObject、JSONArray)
依赖包:json-20180130.jar MAVEN地址: <dependency> <groupId>org.json</groupId> <artifa ...