आप function1 में एक चर है कि आप function2 में उपयोग करना चाहते हैं, तो आपको या तो:।
- पास यह सीधे,
- एक उच्च गुंजाइश समारोह है कि दोनों चर घोषित करने और इसे पारित, या
- कॉल यह एक वैश्विक और उसके बाद सभी कार्यों यह
उपयोग कर सकते हैं की घोषणा, अपने function2 function1 से कहा जाता है तो आप कर सकते हैं बस इसे function2 के लिए एक तर्क के रूप में पास करें।
void function1()
{
std::string abc;
function2(abc);
}
void function2(std::string &passed)
{
// function1::abc is now aliased as passed and available for general usage.
cout << passed << " is from function1.";
}
function3 function1 function2 फोन नहीं करता है, लेकिन दोनों function3 द्वारा कहा जाता है, तो चर घोषित करने और यह दोनों function1 और एक तर्क के रूप function2 लिए गुजरती हैं।
void parentFunction()
{
std::string abc;
function1(abc);
function2(abc);
}
void function1(std::string &passed)
{
// Parent function's variable abc is now aliased as passed and available for general usage.
cout << passed << " is from parent function.";
}
void function2(std::string &passed)
{
// Parent function's variable abc is now aliased as passed and available for general usage.
cout << passed << " is from parent function.";
}
अंत में, न function1 है और न ही function2 एक दूसरे से कहते हैं अगर, और न ही कोड में एक ही समारोह से, तो चर एक वैश्विक रूप में साझा करने के लिए ऐलान करते हैं और function1 और function2 सीधे इसका इस्तेमाल करने में सक्षम हो जाएगा ।
std::string global_abc;
void function1()
{
cout << global_abc << " is available everywhere.";
}
void function2()
{
cout << global_abc << " is available everywhere.";
}
मेरा सुझाव है कि आप स्कोपिंग पर पढ़ लें, और आप समझेंगे कि यह क्यों संभव नहीं है। –
यह बस कोई समझ नहीं आता है: 'function1' के प्रति आमंत्रण' abc' का एक उदाहरण है। 'के लिए (int i = 0; i <100; ++ i) {function1(); } 'आप' एबीसी 'के सौ सौ उदाहरणों में से कौन सा चाहते हैं? –
क्या आप स्थिर सदस्यों के बारे में जानते हैं? – phadaphunk