基本的な文法事項は大体抑えたつもりですが、いろいろ書いてみないことにはわからないです。
C++er的にはC++との違う部分が気になる
①組み込み型
C++の組み込み型はきっちりとしたサイズが定まっていないようだけどC#はそうでないみたい。
longがほんとにロングなので紛らわしい。値型というらしい。C++のそれと似た感じ?
②クラス
C++のクラスが別に動的確保によってインスタンスを作らなくてもいいのに対してC#はいつでもnewするみたい。
参照型とか言われるみたい。
しかし参照っていうのはC++の参照と似たようなものかと思いきやそうでもない。GCとかそういう問題じゃなく
こいつは別に何も参照しないことが許されるのだ。しかも参照先もころころ変えられる。
こういった意味ではむしろC++のスマートポインタそのもののような気がする・・・
しかし基本的にC++でもそんな感じでプログラミングするので特に違和感なし。
継承するとき一つの基底クラスしか取れないみたいだけど、C++の抽象インターフェースにあたるインターフェースとかいう奴があるから特に違和感なし。
プロパティーが便利。
だけどなんか釈然としないのはC++に慣れすぎただけだろうか?w
③デリゲート
C++の関数ポインタのようなものと説明されているが、使っている感じでは別物。
std::functionみたいな感じがあるけどどっちにしても使った感じは別物ww
関数ポインタよりも頻繁に使う感じが・・・
C++のラムダ式にあたるものがC#の匿名メソッドのよう。C#にはC#でより簡潔なラムダ式があるが・・・
④ジェネリック
C++でいうテンプレート的な何か。
入門書では細かいところまでわからないのがつらい。(いつ実体化されるのか?とか)
取りあえずいくつか違いがあるけれどテンプレートっぽい。
なんか読んだ感じ物足りない気がしないでもない。
⑤コレクション
C++のコンテナ的Something
名前がおんなじようで内部の仕組みがC++とちがうものが多いからややこしい。
ジェネリック版と非ジェネリック版があるらしいけど、テンプレート大好きな自分にとってジェネリックは別に恐れるに足りない。
迷うことなくジェネリック版を使う。
⑥LINQ
なんかすごい。
入門書程度ではそれくらいしかわからなかった。
SQL文とかいう奴と似ている気がするが、どっちにしてもそれも知らんから勉強しないとわかんない。
⑦その他
ラムダ式がほんとにラムダ
構造体は値型らしい。使おうという気が起こらない。
範囲ベースのループが楽しい。C++11では導入されるみたいだけど。
取りあえず書きやすい、コードが無法地帯にならない。
うーん。しかし思ったより違いがないのはあんまりおもしろくないかもしれないww
まぁだからといってC++,C#,Java以外で書く気にはなれないんだが・・・
あ、それからC++でC#のnull許容型っぽいもの作りました
完全にエミュレーションすることは全然できていませんけどww
とりあえずそれっぽい使い方はできるのかな?
#pragma once
#include
#include
#include
namespace SafeArithmetic{
template
class Nullable:
public boost::operators >
{
static_assert( boost::is_arithmetic::value, " not arithmetic!" );
public:
//コンストラクタ
Nullable()
:null_( nullptr )
{
}
Nullable( const Nullable& other )
:value_( other.value_ )
{
null_ = other.null_ ? this : nullptr;
}
//たとえばNType がint でoperator == の右のオペランドにdoubleが来ると挙動がおかしくなるから
explicit Nullable( NType value )
:value_( value )
{
null_ = this;
}
Nullable( decltype(nullptr) null )
:null_( null )
{
}
//コピー代入演算子
Nullable& operator = ( const Nullable& other ){
null_ = other.null_ ? this : nullptr;
value_ = other.value_;
return *this;
}
Nullable& operator = ( decltype(nullptr) null ){
null_ = nullptr;
return *this;
}
//例外チェック
private:
void CheckException( const Nullable& other ) const{
if( null_ == nullptr || other.null_ == nullptr ) throw std::runtime_error( "Nullable doesn't have vaild value!" );
}
public:
//比較演算子
bool operator & other ) const {
CheckException( other );
return value_ & other ) const {
return null_ == other.null_ || value_ == other.value_;
}
//算術
Nullable& operator += ( const Nullable& other ){
CheckException( other );
value_ += other.value_;
return *this;
}
Nullable& operator -= ( const Nullable& other ){
CheckException( other );
value_ -= other.value_;
return *this;
}
Nullable& operator *= ( const Nullable& other ){
CheckException( other );
value_ *= other.value_;
return *this;
}
Nullable& operator /= ( const Nullable& other ){
CheckException( other );
value_ /= other.value_;
return *this;
}
Nullable& operator %= ( const Nullable& other ){
CheckException( other );
value_ %= other.value_;
return *this;
}
Nullable& operator &= ( const Nullable& other ){
CheckException( other );
value_ &= other.value_;
return *this;
}
Nullable& operator |= ( const Nullable& other ){
CheckException( other );
value_ |= other.value_;
return *this;
}
Nullable& operator ^= ( const Nullable& other ){
CheckException( other );
value_ ^= other.value_;
return *this;
}
Nullable& operator++ (){
CheckException( *this );
++value_;
return *this;
}
Nullable& operator-- (){
CheckException( *this );
--value_;
return *this;
}
//型変換
NType& Value(){
CheckException( *this );
return value_;
}
bool HasValue() const {
return null_ != nullptr;
}
bool operator ! () const{
return null_ == nullptr? true : !value ;
}
//その他
Nullable operator -(){
CheckException( *this );
return Nullable( -value_ );
}
Nullable operator +(){
CheckException( *this );
return Nullable( +value_ );
}
Nullable operator ~(){
CheckException( *this );
return Nullable( ~value_ );
}
private:
Nullable* null_;
NType value_;
};
}
うーん、しかしいまいちだ。見ての通り本家とはいろいろ違う・・・。
まぁ向こうは言語によってサポートされてるんだから仕方がない。
こっちじゃどう頑張っても無理そうなことあるもん。
取りあえず
Nullable i = nullptr;
みたいなことがやってみたかっただけです・・・
ポインタとかは代入できないんで安心です。(使ってないのでホントに大丈夫かは確認できていないですが・・・)