const,template,pointer,virtual
来源:互联网 发布:java前后端分离session 编辑:程序博客网 时间:2024/05/16 06:43
- typdef 以及 typename 声明类型 时候,需要注意与const 使用时的 修饰作用。
- //pointer operator, not allowed //error: invalid operands of types ‘int*’ and ‘int*’ to binary ‘operator+’【指针不能重载运算符】
#include <stdio.h>#include <stdlib.h>#include <iostream>#include <string>template <typename T>class base{public: virtual void print(const T i) { printf("base:: i=%x, *i=%x\n",i,*i);}};class baseDerived:public base<int*>{public:// void print(const int* i) //not override// { printf("baseDerived:: i=%x, *i=%x\n",i,*i);} void print(int* const i) { printf("baseDerived:: i=%x, *i=%x\n",i,*i);}};//pointer operator, not allowed //error: invalid operands of types 'int*' and 'int*' to binary 'operator+'//int* operator +(int* p1,int* p2)//{// return int*( (unsigned long)p1 +(unsigned long)p2 );//}int main(){ base<int*>* b1=new baseDerived; baseDerived* d1=new baseDerived; int p=10; b1->print(&p); d1->print(&p); base<int*>* b2=new base<int*>; b2->print(&p); delete b1; delete b2; delete d1;}
template <typename T>class base{ T data;public: base(){}; base(T num):data(num) {} virtual void print(const T i) { printf("base:: i=%x, *i=%x\n",i,1);} //*i virtual T operator +(T& other) { printf("base:: operator+ %x + %x\n",data,other); return data+other;}};class baseDerived1:public base<int>{ int data;public: baseDerived1(){} baseDerived1(int num):data(num) {} int operator +(int& other) { printf("base:: operator+ %x + %x\n",data,other); return data+other;} void print(int* const i) { printf("baseDerived:: i=%x, *i=%x\n",i,*i);}};int main(){ base<int>* b11=new baseDerived1(1); baseDerived1* d11=new baseDerived1(2); int oa=5; printf("b11+5=%x\n",*b11+oa); printf("d11+5=%x\n",*d11+oa); base<int>* b12=new base<int>(5); printf("b12+5=%x\n",*b12+oa);}
0 0
- const,template,pointer,virtual
- Const & Pointer
- const & Pointer
- const pointer
- const && pointer
- const pointer
- virtual template
- Pointer to Const & const pointer
- const pointer 和pointer const
- const pointer(const指针)
- smart pointer virtual destructor
- pointer and const
- 关于const于pointer
- const pointer的读法
- const pointer and array
- Pointer and Const
- const And pointer
- sequential list(template,virtual)
- Convert Sorted List to Binary Search Tree
- ubuntu 常见问题
- AngularJs轻松入门(一)创建第一个应用
- 为什么中国没有疯狂的马斯克?
- Posts Tagged 【bfs】Maximum && Minimum Depth of Binary Tree
- const,template,pointer,virtual
- 图文解释XCode常用快捷键的使用
- 招聘那些事
- 四月英语总结——英语还需要坚持
- Android数据存储之SharedPreferences
- foj 1303
- AndroidSDK和ADT下载地址
- Android 中switch case 报错 R.id.xxx没有final
- 关于SharePrefenerces的使用