2011-07-01 14 views
46

मैं ++ कुछ समय के लिए सी में कोडित have't होना आवश्यक है और मैं अटक गया है जब मैं इस सरल टुकड़ाअभिव्यक्ति वर्ग प्रकार

#include "iostream" 
using namespace std; 

class A 
{ 
public: 
    void f() { cout<<"f()\n"; } 
}; 

int main() 
{ 
// A a; //this works 
A *a = new A(); //this doesn't 
a.f(); // "f has not been declared" 
system("pause"); 
} 
+0

वह पंक्ति जो "यह नहीं करता" वास्तव में ठीक है, जिससे आपका प्रश्न उलझन में दिख रहा है। – juanchopanza

उत्तर

105

यह एक सूचक है संकलन करने की कोशिश की, तो ऐसा करें:

a->f(); 

मूल रूप से ऑपरेटर . वस्तुओं और संदर्भों पर प्रयोग किया जाता है (एक वस्तु के खेतों और तरीकों का उपयोग करने के लिए प्रयोग किया जाता), इसलिए:

A a; 
a.f(); 
A& ref = a; 
ref.f(); 

आप एक सूचक प्रकार है, तो आप भिन्नता यह पहली बार एक संदर्भ प्राप्त करने के लिए करने के लिए है:

A* ptr = new A(); 
(*ptr).a(); 
ptr->a(); 

a->b अंकन आम तौर पर सिर्फ (*a).b के लिए एक आशुलिपि है।

+0

बस सी ++ शुरू करना, अभी भी यह पता लगाने के लिए एक automatism बनाना है कि पॉइंटर या संदर्भ का उपयोग करना है या नहीं। मेरे विशेष मामले में मुझे बस एक संदर्भ था, लेकिन किसी कारण से मैंने इसके बजाय एक सूचक पास किया। वैसे भी, स्पष्ट स्पष्टीकरण के लिए धन्यवाद! –

6

सारांश: a.f(); के बजाय यह होना चाहिए, a->f();

मुख्य में आपएक की वस्तु के सूचक के रूप एक परिभाषित किया है ताकि आप -> ऑपरेटर का उपयोग कार्यों पहुँच सकते हैं।

a.f(), च() का उपयोग करने के लिए इस्तेमाल किया जा सकता था अगर एक घोषित किया गया था के रूप में: A a;

4

a सूचक है। आपको -> का उपयोग करने की आवश्यकता नहीं है, .

11

विश्लेषण की अनुमति दें।

#include <iostream> // not #include "iostream" 
using namespace std; // in this case okay, but never do that in header files 

class A 
{ 
public: 
    void f() { cout<<"f()\n"; } 
}; 

int main() 
{ 
/* 
// A a; //this works 
A *a = new A(); //this doesn't 
a.f(); // "f has not been declared" 
*/ // below 


// system("pause"); <-- Don't do this. It is non-portable code. I guess your 
//      teacher told you this? 
//      Better: In your IDE there is prolly an option somewhere 
//        to not close the terminal/console-window. 
//      If you compile on a CLI, it is not needed at all. 
} 

एक सामान्य सलाह के रूप में:

0) Prefer automatic variables 
    int a; 
    MyClass myInstance; 
    std::vector<int> myIntVector; 

1) If you need data sharing on big objects down 
    the call hierarchy, prefer references: 

    void foo (std::vector<int> const &input) {...} 
    void bar() { 
     std::vector<int> something; 
     ... 
     foo (something); 
    } 


2) If you need data sharing up the call hierarchy, prefer smart-pointers 
    that automatically manage deletion and reference counting. 

3) If you need an array, use std::vector<> instead in most cases. 
    std::vector<> is ought to be the one default container. 

4) I've yet to find a good reason for blank pointers. 

    -> Hard to get right exception safe 

     class Foo { 
      Foo() : a(new int[512]), b(new int[512]) {} 
      ~Foo() { 
       delete [] b; 
       delete [] a; 
      } 
     }; 

     -> if the second new[] fails, Foo leaks memory, because the 
      destructor is never called. Avoid this easily by using 
      one of the standard containers, like std::vector, or 
      smart-pointers. 

अंगूठे का एक नियम के रूप में: यदि आप अपने दम पर स्मृति का प्रबंधन करने की जरूरत है, वहाँ आम तौर पर एक superiour प्रबंधक या पहले से ही उपलब्ध विकल्प है, एक है कि इस प्रकार आरएआईआई सिद्धांत।

संबंधित मुद्दे