由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 怎么搞的?
相关主题
[合集] 又学了一招c++ operator overloading question
请问一个implicit conversion的问题(C++)关于数组动态分配的疑问???
请教各路C++大神 为什么f(3) 输出是 'dd'Help: who has gcc 4.0 or higher
a simple question about constructor请叫一个 template class constructor 的问题
小问题could anybody please tell me what " while(cin) {...}" means?
请教几个C++问题Python里面为什么range(0,5)不用0:5来表示?
one question about initializaiton list问行C++代码
c++ question*(&b1)=b编译不过,b1=b可以,区别是?
相关话题的讨论汇总
话题: double话题: int话题: class话题: 报错话题: conversion
进入Programming版参与讨论
1 (共1页)
y*h
发帖数: 107
1
我写了个class, 并只给了它一个constructor(int)
class A
{
A(int a){}
};
可现在在main function里面A(double) 照运行无误, 一点也不报错.
如何改程序, 让其robust, 见A(double)报错.
q*****g
发帖数: 72
2
implicit conversion from double to int.
try writing a private A(double)

【在 y*h 的大作中提到】
: 我写了个class, 并只给了它一个constructor(int)
: class A
: {
: A(int a){}
: };
: 可现在在main function里面A(double) 照运行无误, 一点也不报错.
: 如何改程序, 让其robust, 见A(double)报错.

y*h
发帖数: 107
3
这样行不行? 试了一下能运行, 大家说说有无memory leak?
class A
{
public:
A(int){}
A(double){exit(-1);}
}


【在 q*****g 的大作中提到】
: implicit conversion from double to int.
: try writing a private A(double)

q*****g
发帖数: 72
4
no memory leak, but not a good design.
the program will terminate suddenly if someone use A(double)

【在 y*h 的大作中提到】
: 这样行不行? 试了一下能运行, 大家说说有无memory leak?
: class A
: {
: public:
: A(int){}
: A(double){exit(-1);}
: }
:

c*r
发帖数: 278
5
explicit A(int a) {}

【在 y*h 的大作中提到】
: 这样行不行? 试了一下能运行, 大家说说有无memory leak?
: class A
: {
: public:
: A(int){}
: A(double){exit(-1);}
: }
:

X****r
发帖数: 3557
6
No, "explicit" keyword only prevents implicit conversion from int to A. It
won't prevent the floating-integral conversion from double to int. The right
way is defining a private A(double) as mentioned above.

【在 c*r 的大作中提到】
: explicit A(int a) {}
b*****n
发帖数: 2324
7
服了,够彻底,你的exit了还能有啥leak。

【在 y*h 的大作中提到】
: 这样行不行? 试了一下能运行, 大家说说有无memory leak?
: class A
: {
: public:
: A(int){}
: A(double){exit(-1);}
: }
:

l***t
发帖数: 81
8
这exit(),写得牛!
1 (共1页)
进入Programming版参与讨论
相关主题
*(&b1)=b编译不过,b1=b可以,区别是?小问题
问个copy constructor的问题请教几个C++问题
问个c++问题one question about initializaiton list
Python 这种二逼语言怎么设计的c++ question
[合集] 又学了一招c++ operator overloading question
请问一个implicit conversion的问题(C++)关于数组动态分配的疑问???
请教各路C++大神 为什么f(3) 输出是 'dd'Help: who has gcc 4.0 or higher
a simple question about constructor请叫一个 template class constructor 的问题
相关话题的讨论汇总
话题: double话题: int话题: class话题: 报错话题: conversion