2012-08-11 50 views
1

我不知道如何隱藏真實屬性域(不能讓它私有或公共但力使用getter和setter方法),併爲他提供簡單的setter和getter。所以,我不知道如何創建API,如:如何通過定義隱藏一個字段並僅提供setter和getter?

private: 
    Property(int my_a); 
public: 
    Property(int my_b); 
... 
{ 
set_my_a(1); 
cout << get_my_a() << endl; 
// my_a = 13; // will cause compiler error 
... 

如何通過升壓預處理器創建這樣的事嗎?

+0

這是關係到提高?一般的做法是製作一個可變的私人 – Gir 2012-08-11 21:18:08

+1

你爲什麼要做這樣的事情?如果getters和setter沒有被定製,屬性是沒有意義的。 – Puppy 2012-08-11 21:18:29

+0

@DeadMG:的確 - 我想定製Geters和Setter,但是我想知道用geter和setter函數封裝字段的一般方法。 – myWallJSON 2012-08-11 21:28:35

回答

1

你真的需要使用升壓預處理器? 你有一個解決方案,而下面升壓:

// property.h  
#include <stdio.h> 

#define property(type) struct : public Property <type> 

template <typename T> 
class Property 
{ 
protected: 
    T value; 
public: 
    virtual T get() { 
    return value; 
    } 
    virtual void set(T new_value) { 
    value = new_value; 
    } 
}; 

使用示例:

// test.cpp 
#include "property.h" 

class Test { 
public: 
    property(int) {} a; 

    property(int) { 
    int get() { 
     return value * 10; 
    } 
    } b; 

    property(int) { 
    void set(int x) { 
     value = x * 200; 
    } 
    } c; 

    property(int) { 
    int get() { 
     return value * 3000; 
    } 
    void set(int x) { 
     value = x * 443; 
    } 
    } d; 
}; 

main() 
{ 
    Test t; 

    printf("i\ta\tb\tc\td\t\n"); 
    for (int i=0; i<10; i++) { 
    t.a.set(i); 
    t.b.set(i); 
    t.c.set(i); 
    t.d.set(i); 
    printf("%i\t%i\t%i\t%i\t%i\n", i, t.a.get(), t.b.get(), t.c.get(), t.d.get()); 
    } 
} 

http://en.wikipedia.org/wiki/Property_(programming)#C.2B.2B維基百科的解決方案是好的,但需要一個很小的修改,成爲有用的,因爲沒有保護的聲明,你不能寫你自己的getter和setter。

#include <iostream> 

template <typename T> 
class property { 
protected: 
    T value; 
public: 
    T & operator = (const T &i) { 
    ::std::cout << i << ::std::endl; 
    return value = i; 
    } 
    operator T const &() const { 
    return value; 
    } 
}; 

struct Bar { 
    property <bool> alpha; 
    struct :public property <int> { 
    int & operator = (const int &i) { 
     ::std::cout << "new setter " << i << ::std::endl; 
     return value = i; 
    } 
    } bravo; 
}; 

main() 
{ 
    Bar b; 
    b.alpha = false; 
    b.bravo = (unsigned int) 1; 
} 

你可以改變多一點,如果你想:

#include <iostream> 
#define SETTER(type) public: type& operator=(const type new_value) 
#define GETTER(type) public: operator type const &() const 

template <typename T> 
class Property { 
protected: 
    T value; 
public: 
    T & operator = (const T &i) { 
    ::std::cout << i << ::std::endl; 
    return value = i; 
    } 
    template <typename T2> T2 & operator = (const T2 &i) { 
    ::std::cout << "T2: " << i << ::std::endl; 
    T2 &guard = value; 
    throw guard; // Never reached. 
    } 
    operator T const &() const { 
    return value; 
    } 
}; 

struct Bar { 
    Property <bool> alpha; 
    struct:Property <int> { 
    SETTER(int) { 
     value = new_value * 1000; 
     ::std::cout << "new method " << new_value << ::std::endl; 
     return value; 
    } 
    GETTER(int) { 
     return value/1000; 
    } 
    } bravo; 
}; 

main() 
{ 
    Bar b; 
    b.alpha = false; 
    b.bravo = (unsigned int) 1; 
    ::std::cout << b.bravo << ::std::endl; 
} 
0

而不是重寫實現的例子,這是維基百科的鏈接之一:http://en.wikipedia.org/wiki/Property_(programming)#C.2B.2B

這基本上強制通過getter/setter方法訪問的屬性。升級你需要得到你想要的效果是能夠傳遞函子到這些屬性。有很多關於實施這些的想法;我無法建議的最佳方法取決於您的發展需求。就個人而言,這感覺就像在工程上,並且寧願僅僅使用Pimpl來隱藏我的私人信息,而只是明確地提供獲取者/設置者。

相關問題