是否可以为枚举定义运算符?例如,我在课堂上有枚举月份,我希望能够写出++ my_month.
谢谢
PS
为了避免溢出我做了这样的事情:
void Date::add_month()
{
switch(my_month_)
{
case Dec:
my_month_ = Jan;
add_year();
break;
default:
++my_month_;
break;
}
}
Run Code Online (Sandbox Code Playgroud) Exception规范是否是方法签名的一部分?我的意思是:
public void someMethod(String myString) throws IOException
Run Code Online (Sandbox Code Playgroud)
是' 抛出IOException '这个方法签名的一部分?
谢谢
在代码中:
template<class T>
struct is_builtin
{
enum {value = 0};
};
template<>
struct is_builtin<char>
{
enum {value = 1};
};
template<>
struct is_builtin<int>
{
enum {value = 1};
};
template<>
struct is_builtin<double>
{
enum {value = 1};
};
template<class T>
struct My
{
typename enable_if<is_builtin<T>::value,void>::type f(T arg)
{
std::cout << "Built-in as a param.\n";
}
typename enable_if<!is_builtin<T>::value,void>::type f(T arg)
{
std::cout << "Non - built-in as a param.\n";
}
};
struct A
{
};
int main()
{
A a;
My<int> …Run Code Online (Sandbox Code Playgroud) 在下面描述的情况下使用cerr是好的风格吗?
try
{
cout << a + b;
}
catch(const IntException& e)
{
cerr << "Exception caught: " << typeid(e).name(); //using cerr not cout
}
catch(...)
{
cerr << "Unknown exception.";//using cerr not cout
}
Run Code Online (Sandbox Code Playgroud)
还是应该使用cout?请参阅代码中的注释.
我正在阅读一本名为"C++编码标准"的书作者:Herb Sutter,Andrei Alexandrescu和本书第42章就是一个例子:(章节很短,所以我正在冒昧并粘贴它的一部分)
考虑:
class Socket {
public:
// … constructor that opens handle_, destructor that closes handle_, etc. …
int GetHandle() const {return handle_;} // avoid this - (1) <-why this is bad code?
// and why there is a comment to avoid such code??
private:
int handle_; // perhaps an OS resource handle
};
Run Code Online (Sandbox Code Playgroud)
数据隐藏是一种功能强大的抽象和模块化设备(参见第11和41项).但是隐藏数据然后给它提供句柄是弄巧成拙的,就像锁住房子并将钥匙留在锁中一样.这是因为:
客户端现在有两种实现功能的方法:它们可以使用类的抽象(Socket)或直接操作类所依赖的实现(套接字的C风格句柄).在后一种情况下,对象不知道它认为拥有的资源发生了重大变化.现在,类无法可靠地丰富或修饰功能(例如,代理,记录,收集统计信息),因为客户端可以绕过装饰的受控实现以及它认为正在添加的任何不变量,这使得接下来的错误处理成为可能(参见条款70) .
该类无法更改其抽象的底层实现,因为客户端依赖于它:如果稍后升级Socket以支持具有不同低级原语集的不同协议,则调用获取底层handle_并错误地操作它的代码将是静默的破碎.
该类不能强制执行其不变量,因为调用代码可以改变类不知道的状态:例如,有人可以关闭Socket对象使用的句柄而不通过Socket成员函数,从而使对象无效.
客户端代码可以存储您的类返回的句柄,并在您的类的代码使它们失效后尝试使用它们.
这是本书的摘要:
不要过多地自愿:避免将句柄返回到由您的班级管理的内部数据,因此客户端不会无法控制地修改您的对象认为它拥有的状态.
基本上我要求的是:
为什么标记为(1)的行被列为坏代码的示例(我一直认为返回指针或引用是一个坏主意,但按值返回是正常的.这里他们说按值返回也是个坏主意?)
是否可能存在'&'缺失,它们的真正含义是不通过引用或指针返回内部数据?
谢谢.
构造函数初始化列表中的执行顺序是否可确定?我知道成员中的成员顺序是这些成员初始化的顺序,但如果我有这样的场景:
class X()
{
X_Implementation* impl_;
};
and then providing that allocator is available:
X::X():impl_(Allocate(sizeof(X_Implementation)))//HERE I'M ALLOCATING <--1
,impl_(Construct<X_Implementation>(impl_))//AND HERE I'M CONSTRUCTING <--2
{
}
Run Code Online (Sandbox Code Playgroud)
但为了使这个可靠,这个顺序必须是从左到右.它是由GREAT BOOK OF std保证还是不保证?如果不是,我总是可以将第二条线移动到身体中.
我如何懒惰评估std :: conditional中的第二个arg?
#include "stdafx.h"
#include <type_traits>
struct Null{};
struct _1{enum {one = true,two = false};};
struct _2{enum {two = true, one = false};};
template<class T>
struct is_nulltype
{
enum {value = false};
};
template<>
struct is_nulltype<Null>
{
enum {value = true};
};
template<class T>
struct X : std::conditional<is_nulltype<T>::value,Null,typename std::conditional<T::one,_1,_2>::type>::type
{
};
int _tmain(int argc, _TCHAR* argv[])
{
X<Null> x;//won't compile no Null::one but I don't need that member in Null at all
return 0;
}
Run Code Online (Sandbox Code Playgroud) fncs:operator new和operator new [](不是new和new []运算符)之间有什么区别吗?当然除了调用语法?我问,因为我可以使用:: operator new(sizeof(T)*numberOfObject)为我的objs分配X个字节,然后使用数组表示法访问它们,那么:: operator new []有什么大不了的.它只是语法糖吗?
#include <new>
#include <iostream>
#include <malloc.h>
using namespace std;
struct X
{
int data_;
X(int v):data_(v){}
};
int _tmain(int argc, _TCHAR* argv[])
{
unsigned no = 10;
void* vp = ::operator new(sizeof(X) * no);
cout << "Mem reserved: " << _msize(vp) << '\n';
X* xp = static_cast<X*>(vp);
for (unsigned i = 0; i < no; ++i)
{
new (xp + i) X(i);
}
for (unsigned i = 0; i < …Run Code Online (Sandbox Code Playgroud)