विजुअल स्टूडियो 2008 में, संकलक नीचे _tmain
में SetCustomer
करने के लिए कॉल को हल नहीं कर सकता है और यह स्पष्ट हो जाता है:अस्पष्ट पहुँच
// error C2385: ambiguous access of 'SetConsumer'
// could be the 'SetConsumer' in base 'Producer<AppleConsumer>'
// or could be the 'SetConsumer' in base 'Producer<MeatConsumer>'
:
template <typename TConsumer>
struct Producer
{
void SetConsumer(TConsumer* consumer) { consumer_ = consumer; }
TConsumer* consumer_;
};
struct AppleConsumer
{
};
struct MeatConsumer
{
};
struct ShillyShallyProducer : public Producer<AppleConsumer>,
public Producer<MeatConsumer>
{
};
int _tmain(int argc, _TCHAR* argv[])
{
ShillyShallyProducer producer;
AppleConsumer consumer;
producer.SetConsumer(&consumer); // <--- Ambiguous call!!
return 0;
}
यह संकलन त्रुटि है
मैंने सोचा था कि टेम्पलेट तर्क लुकअप तंत्र सही आधार Producer
को कम करने के लिए पर्याप्त स्मार्ट होगा। यह क्यों नहीं है
मैं इस के आसपास
template <typename TConsumer>
struct Producer
{
template <typename TConsumer2>
void SetConsumer(TConsumer2* consumer) { consumer_ = consumer; }
TConsumer* consumer_;
};
को Producer
बदलकर हो और कह सकते हैं SetConsumer
रूप
producer.SetConsumer<AppleConsumer>(&consumer); // Unambiguous call!!
लेकिन यह अगर मैं नहीं था अच्छे होगा ...
मैं सिर्फ बाहर बिंदु करना चाहते हैं कि आपके प्रस्तावित कार्यवाही में, आपको 'SetConsumer (& उपभोक्ता)', 'SetConsumer (& उपभोक्ता)' को कॉल करने की आवश्यकता नहीं है, और पर्याप्त रूप से टेम्पलेट तर्क को कम करेगा)। –
sbk
@ एसबीके: नहीं, यह नहीं होगा। मैंने पोस्ट करने से पहले कोशिश की, और अब एक बार फिर से सुनिश्चित हो। अभी भी अस्पष्ट –