如果有两个对象:one, two, 要想把one转换成为two,有两种方法:
在one在定义operator two() const函数
在two在增加two(const one&)构造函数
一般只能使用其中一种情况,而不能两种一起使用。
operator:
#include <iostream>
using std::cout;
using std::endl;
class two {
int iTwo;
public:
two(int a = 0) : iTwo(a) {}
void print() const {
cout << "two is: " << iTwo << endl;
}
};
class one {
int iOne;
public:
one(int a = 0) : iOne(a) {}
operator two() const {
return two(iOne);
}
};
void f(const two& t) {
t.print();
}
int main() {
one o(143);
f(o);
}
构造函数转换:
#include <iostream>
using std::cout;
using std::endl;
class one {
int iOne;
public:
one(int a = 0) : iOne(a) {}
int getValue() const {
return iOne;
}
};
class two {
int iTwo;
public:
two(int a = 0) : iTwo(a) {}
two(const one& o) {
iTwo = o.getValue();
}
void print() const {
cout << "two is :" << iTwo << endl;
}
};
void f(const two& t) {
t.print();
}
int main() {
one one(222);
f(one);
}
在one在定义operator two() const函数
在two在增加two(const one&)构造函数
一般只能使用其中一种情况,而不能两种一起使用。
operator:
#include <iostream>
using std::cout;
using std::endl;
class two {
int iTwo;
public:
two(int a = 0) : iTwo(a) {}
void print() const {
cout << "two is: " << iTwo << endl;
}
};
class one {
int iOne;
public:
one(int a = 0) : iOne(a) {}
operator two() const {
return two(iOne);
}
};
void f(const two& t) {
t.print();
}
int main() {
one o(143);
f(o);
}
构造函数转换:
#include <iostream>
using std::cout;
using std::endl;
class one {
int iOne;
public:
one(int a = 0) : iOne(a) {}
int getValue() const {
return iOne;
}
};
class two {
int iTwo;
public:
two(int a = 0) : iTwo(a) {}
two(const one& o) {
iTwo = o.getValue();
}
void print() const {
cout << "two is :" << iTwo << endl;
}
};
void f(const two& t) {
t.print();
}
int main() {
one one(222);
f(one);
}
发表评论
-
const 与static 关键词在c 和 c++中的作用
2012-08-11 11:49 967一.C语言中的const ... -
error LNK2005 and error LNK2019
2012-08-09 23:08 1097error LNK2005 and error LNK2019 ... -
LIBCD.lib(crt0dat.obj) : error LNK2005: _exit already defined in msvcrtd.lib(MSV
2012-08-07 09:20 1384LIBCD.lib(crt0dat.obj) : error ... -
在VS2010下配置VC++ Directories
2012-08-07 08:29 10451:可以 在“class View” 面板 -> 右 ... -
零星笔记,待整理
2010-11-01 13:00 478//用 getline 函数从输入读取整行内容。然后为 ... -
VC: error LNK2019:unresolved external symbol *** referenced in function ***的解决方案
2010-10-30 21:13 2106当头文件中声明了一个函数,但是在相应的源文件中却没有对该函数进 ... -
stdcall cdecl fastcall thiscall naked call的具体含义
2010-10-30 18:54 959在C语言中,假设我们有 ... -
#define总结 (网上资料汇集)
2010-10-30 08:06 7161. 定义简单的常数:定义常量,便于修改(切不可在后面加上分号 ... -
(zz)编译与使用Qt4,Visual Studio Express 2008
2010-10-14 22:35 1374一、Qt版本 我现在用的是4.5.2。好像前段时间4.6出来 ... -
strcat 内部实现
2010-09-21 00:43 1798char * strcat(char * dest, ... -
deep copy & shallow copy
2010-09-15 19:27 816A shallow copy of an object cop ... -
reinterpret_cast的本质(转)
2010-09-14 23:15 1612先看看下面的代码: #include <iostr ... -
C++ 经典书籍
2010-09-14 16:18 1684下载地址: http://www.51cnnet.net/di ... -
内存对齐的规则以及作用
2010-09-14 00:14 687首先由一个程序引入话 ... -
[转]C++ reinterpret_cast,const_cast等 显式类型转换总结
2010-09-13 23:23 1012#include<iostream.h> ... -
C++提供了四种新的类型强制
2010-09-13 23:13 684static_cast const_cast reinte ... -
C++著名程序库的比较和学习经验
2010-09-13 19:30 8221、C++各大有名库的介绍——C++标准库 2、C++各大有名 ... -
c++ library and resource List
2010-09-13 18:56 8971,前言 无数次听 ... -
C++中操作符operator的两种用法
2010-09-12 02:23 11161.operator overloading C++可能通过o ... -
C++中的类拷贝构造函数和模板拷贝构造函数
2010-09-11 20:53 3002类(包括模板类)构造函数是真实的构造函数;然而模板构造函数,其 ...
相关推荐
上面两个都是C风格的强制类型转换,C++还增加了一种转换方式,比较一下上面和下面这个书写方式的不同: long int el=123; short i=int (el); float m=34.56; int i=int (m); 使用强制类型转换的最大好处...
子类继承父类大部分的资源,不能继承的有构造函数,析构函数,拷贝构造函数,operator=函数,友元函数等等 15.为什么要引入抽象基类和纯虚函数? 主要目的是为了实现一种接口的效果。 16.介绍一下模板和容器。如何...
11.10.1 温习调用构造函数实现的类型转换 11.10.2 通过构造函数将变量转换为一个对象的成员变量 11.10.3 通过operator关键字进行转换 11.11什么可以重载,什么不可以重载 第12章 继承 12.1 什么是继承和派生 12.1.1...
11.10.1 温习调用构造函数实现的类型转换 11.10.2 通过构造函数将变量转换为一个对象的成员变量 11.10.3 通过operator关键字进行转换 11.11什么可以重载,什么不可以重载 第12章 继承 12.1 什么是继承和派生 ...
你可以给构造函数设定一个名字,例如: module(L, "my_library") [ // declarations ]; 这里所有的申明都将被放置在 my_libary 表. 如果你想要嵌套名空间,你可以用 luabind::namespace_ 类. 它和 luabind::module ...
CComplex(double real=0,double image=0) //构造函数 { Real=real; Image=image; } friend istream & operator>>(istream &is,CComplex &com); //重载输入 friend ostream & operator(ostream &os,CComplex &com); /...
在C++中,有两种给出注释的方法:一种是延用C语言方法,使用"/*"和"*/"括起注释文字。另一种方法是使用"//",从"//"开始,直到它所在行的行尾,所有字符都被作为注释处理。 2-8 什么叫做表达式?x = 5 + 7是一个...
} } 在构造函数中调⽤test()函数,执⾏完过后应该会⾃动释放parent和children对象,但是由于相互引⽤,在退出之前,引⽤计数为2,退出 之后引⽤计数还是1,所以导致不能⾃动释放,并且此时这两个对象再也⽆法访问到...
有两点坐标构造 下面介绍几个成员函数: int Width( ) const; 得到宽度 int Height( ) const; 得到高度 CSize Size( ) const; 得到尺寸 CPoint& TopLeft( ); 得到左上角坐标 CPoint& BottomRight( ); 得到右下...
第三种:new 约束指定泛型类声明中的任何类型参数都必须有公共的无参数构造函数。 2.如何把一个array复制到arrayList里 foreach( object o in array )arrayList.Add(o); 3.datagrid.datasouse可以连接什么数据源 ...
也可以使⽤直接初始化的⽅式 shared_ptr<int> pint(new int(100)) 来创建⼀个 shared_ptr 并初始化,但是由于 shared_ptr 定义 的构造函数是 explicit 的,因此不能使⽤ shared_ptr<int> pint = new int(100) 来...
在大部分操作系统中,内存分配由以下两个简单的函数来处理: void *malloc(long numbytes):该函数负责分配 numbytes 大小的内存,并返回指向第一个字节的指针。 void free(void *firstbyte):如果给定一个由...
C 编程语言提供了两个函数来满足我们的三个需求: malloc:该函数分配给定的字节数,并返回一个指向它们的指针。如果没有足够的可用内存,那么它返回一个空指针。 free:该函数获得指向由 malloc 分配的内存片段...
C#(WINFORM)学习 一、 C#基础 基础 类型和变量 类型和变量 类型 C# 支持两种类型:“值类型”和“引用类型”。值类型包括简单类型(如 char、int 和 float 等)、枚举类型和结构类型。引用类型包括类 (Class)类 ...