2011-04-05 16 views
5

मुझे नीचे पढ़ने के लिए कोड बहुत मुश्किल लगता है, और मैंने इसे लिखा है! वहाँ प्रत्येक कार्यान्वित सदस्य समारोह के लिएकार्यान्वयन में कक्षा के नाम और टेम्पलेट कॉल को दोहराने से कैसे बचें?

  1. बचने बुला खाके के लिए कुछ है
  2. से बचने के प्रत्येक कार्यान्वित सदस्य समारोह के लिए ClassName::member_function_name आ रही हैं? मुझे इस संबंध में जावा ड्रियर मिल गया है। आप हर जगह कक्षा का नाम दोहराते नहीं हैं।

धन्यवाद!

template <class KeyType, class ObjectType> 
class Vertex 
{ 
private: 
    KeyType key; 
    const ObjectType* object; 
public: 
    Vertex(const KeyType& key, const ObjectType& object); 
    const KeyType getKey(); 
}; 

template <class KeyType, class ObjectType> 
class Graph 
{ 
private: 
    map<KeyType, Vertex<KeyType, ObjectType> > vertexes; 
public: 
    const Vertex<KeyType, ObjectType>& createVertex(const KeyType& key, const ObjectType& object); 
}; 

template <class KeyType, class ObjectType> 
Vertex<KeyType, ObjectType>::Vertex(const KeyType& objectKey, const ObjectType& newObject) 
{ 
    key = objectKey; 
    object = &newObject; 
}; 

template <class KeyType, class ObjectType> 
const KeyType Vertex<KeyType, ObjectType>::getKey() 
{ 
    return key; 
}; 

template <class KeyType, class ObjectType> 
const Vertex<KeyType, ObjectType>& Graph<KeyType, ObjectType>::createVertex(const KeyType& key, const ObjectType& object) 
{ 
    Vertex<KeyType, ObjectType> *vertex = new Vertex<KeyType, ObjectType>(key, object); 
    vertexes.insert(make_pair(vertex->getKey(), *vertex)); 
    return *vertex; 
}; 
+2

सिर्फ एक छोटे से टिप: मैं आमतौर पर कक्षाएं आसान सार्वजनिक इंटरफेस के साथ पढ़ने के लिए लगता है की घोषणा की पहली – jeffythedragonslayer

+0

"सौंदर्य देखने वाले की आंखों में है" मैं कुछ भी नहीं देख सकते हैं कि कोड के साथ अनाकर्षक। –

+0

आप टाइपिफ़ को कोड को सरल बनाने में सहायता करेंगे: उदाहरण के लिए, 'Vertex ' के लिए एक को कुछ स्थानों में उपयोग किया जा सकता है। –

उत्तर

1

यह एक टेम्पलेट है, क्यों सदस्य वर्ग के अंदर सदस्य कार्यों को परिभाषित नहीं करते?

कोड को किसी भी संकलन इकाई में तत्कालता के लिए उपलब्ध होना आवश्यक है, इसलिए आपको परिभाषा से घोषणा को अलग करने से कोई संकलन समय गति नहीं मिलेगी और कंपाइलर्स आजकल अपने आप को तय करने के लिए पर्याप्त स्मार्ट हैं कि इनलाइनिंग आवश्यक है या नहीं।

1

यह आपके कोड के बराबर "लगभग" होना चाहिए। "लगभग", क्योंकि एक्सडीडी ने कहा, सदस्य समारोह की इन-बॉडी परिभाषा स्पष्ट रूप से उन्हें इनलाइन के रूप में चिह्नित करती है।

कक्षा डिफ़ॉल्ट रूप से निजी होती है और संरचना डिफ़ॉल्ट रूप से सार्वजनिक होती है।

template <class KeyType, class ObjectType> 
class Vertex 
{ 
    KeyType key; 
    const ObjectType* object; 

    public: 
     Vertex(const KeyType& _key, const ObjectType& _object) : key(_key), object(&_object) {} 

     const KeyType getKey() 
     { 
      return key; 
     } 
}; 

template <class KeyType, class ObjectType> 
class Graph 
{ 
    map<KeyType, Vertex<KeyType, ObjectType> > vertexes; 

    public: 
     const Vertex<KeyType, ObjectType>& createVertex(const KeyType& key, const ObjectType& object) 
     { 
      Vertex<KeyType, ObjectType> *vertex = new Vertex<KeyType, ObjectType>(key, object); 
      vertexes.insert(make_pair(vertex->getKey(), *vertex)); 
      return *vertex; 
     } 
}; 

या typedef साथ:

template <class KeyType, class ObjectType> 
class Vertex 
{ 
    KeyType key; 
    const ObjectType* object; 

    public: 
     Vertex(const KeyType& _key, const ObjectType& _object) : key(_key), object(&_object) {} 
     const KeyType getKey() 
     { 
      return key; 
     } 
}; 

template <class KeyType, class ObjectType> 
class Graph 
{ 
    typedef Vertex<KeyType, ObjectType> tVertex; 
    map<KeyType, tVertex > vertexes; 

    public: 
     const tVertex& createVertex(const KeyType& key, const ObjectType& object) 
     { 
      tVertex *vertex = new tVertex(key, object); 
      vertexes.insert(make_pair(vertex->getKey(), *vertex)); 
      return *vertex; 
     } 
}; 
+0

सदस्य कार्यों की इन-बॉडी परिभाषा के बाद सख्ती से समकक्ष नहीं है, जो उन्हें इनलाइन के रूप में चिह्नित करता है। – xDD

+0

@xDD: आप सही हैं, मैं संपादित करूंगा। – Valkea

+0

@xDD: प्रश्न पर मेरे दिमाग में आता है; क्या यह वैरिएडिक टेम्पलेट्स के साथ भी लागू होता है? चूंकि तर्क संख्या अनिर्धारित है, इसलिए शरीर में परिभाषित होने पर भी उन्हें इनलाइन करने में सक्षम नहीं होना चाहिए। – Valkea

1

मुझे लगता है कि इस मामले में, आप आसानी से काम करता है घोषणा में परिभाषित कर सकते हैं, और वाक्य रचना स्पष्ट करने के लिए कुछ typedefs का उपयोग करें।

template <class KeyType, class ObjectType> 
class Vertex { 
    public: 
    Vertex(const KeyType& key, const ObjectType& object) : 
      key(objectKey), object(&newObject) { }; 
    const KeyType getKey() const { return key; }; 
    private: 
    KeyType key; 
    const ObjectType* object; 
}; 

template <class KeyType, class ObjectType> 
class Graph { 
    public: 
    typedef Vertex<KeyType, ObjectType> vertex_type; 

    const vertex_type& createVertex(const KeyType& key, const ObjectType& object) { 
     vertex_type* vertex = new vertex_type(key, object); 
     vertexes.insert(make_pair(vertex->getKey(), *vertex)); 
     return *vertex; 
    }; 
    private: 
    map<KeyType, vertex_type > vertexes; 
};