C 實現C 的get,set屬性操作

2021-05-23 08:55:30 字數 833 閱讀 3633

// properties.h

#ifndef _properties_h

#define _properties_h

#define property(t,n) __declspec( property ( put = property__set_##n, get = property__get_##n ) ) t n;/

typedef t property__tmp_type_##n

#define readonly_property(t,n) __declspec( property (get = property__get_##n) ) t n;/

typedef t property__tmp_type_##n

#define writeonly_property(t,n) __declspec( property (put = property__set_##n) ) t n;/

typedef t property__tmp_type_##n

#define get(n) property__tmp_type_##n property__get_##n()

#define set(n) void property__set_##n(const property__tmp_type_##n& value)

#endif /* _properties_h */

// main.cpp

#include #include #include "properties.h"

class vector2

};int main()

C 屬性和字段 get set

做了一年的asp.net的開發了,最近有乙個疑問,在專案中經常用到實體類,每次寫get set 我就有點不理解了,是不是吃多了沒事情做,何必不把字段設定成public就行了,下面做了乙個實驗 class program public class entity public class entity2...

c 中get set 的使用

在早期學習c 的過程中,經常遇到這樣的語句 public string studentname get set 當時也不是很明白為什麼要這樣?學深了c 後,又沒法對其做乙個總結。今天看了 visual c net程式設計教程 這本書,總結得不錯,做下筆記 在許多物件導向程式設計語言中,屬性 上面 中...

《C 中get set的用法》

c 中get set 的用法 c get set用法中,get是讀取屬性時進行的操作,set是設定屬性時進行的操作。定義乙個屬性如果只有 get,這個屬性就是唯讀的。同樣,只有 set,屬性就是只寫的,當然只寫的屬性是沒有任務意義的。假設類是乙個銀行,既能存錢也能取錢.money就像銀行裡的自動訪問...