Thread: Please Answer My Question:

  1. #1
    Registered User DeanDemon's Avatar
    Join Date
    Nov 2002
    Posts
    37

    Please Answer My Question:

    What's the difference between contructors and member accessors like SetValue(int x)?

    I already know what constructors do, and that they're automatically called when you declare an object, but what's so special about them? You can do the same thing with member accessors. Someone please explain the main importance of constructors and some practical uses for them.

    Thanks.
    -Dean

  2. #2
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    A constructor is where you put the code that should be executed when an object of that class is created.

    Example:

    [code]

    class Counter{

    private:
    int x;

    public:
    Counter() {
    x = 0;
    }

    void inc() { x++ };

    int get() { return x };

    }

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Constructors and destructors are interesting creatures. There are literally hundreds of applications for their uses.

    Here is an interesting use for the constuctor/destructor team, a mechanism that is very handy for cleaning up code and tying up loose ends in the process.

    Consider this:

    Assume you are writing a function in a class that processes highly volatile data. There are many times during it's execution where something could go wrong. Using a nested helper class, you clean up the interface and also avoid errors using a definite mechanism like this one.



    Code:
    class Processor {
     public:
    
    bool Process(void * data){
     FatalErrorTracker fet(this);
      
     //...code
    
     if(!condition)
      return false;
    
     //...code
    
     if(!condition)
      return false;
    
    //...code
    
     if(!condition)
      return false;
    
     //...code
    
     if(!condition)
      return false;
     
     fet.disable();
    
     return true;
    }
     
    
     private:
     class FatalErrorTracker {
      public:
      FatalErrorTracker (Processor * p){
      processor = p;
      }
      void disable(){
       processor = NULL;
      }
      ~FatalErrorTracker (){
       if(processor == NULL) return;
       processor->close_valve();
       processor->bad_error = true;
       processor->initiate_shutdown();
      }
      private:
      Processor * processor;
     };
    };


    As you can see, until we reach the end of the function and disable the FatalErrorTracker, any exit will result in the execution of it's destructor, an emergency shutdown of sorts. The constructors main role here is to store a pointer to the processor object, to do so without calling a member function, and besides, the fact that this object cannot be contructed without that pointer makes it very clear the intent of the class.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. some question asking for answer...
    By mat13mat in forum C++ Programming
    Replies: 6
    Last Post: 04-30-2008, 03:40 AM
  2. C++ Question with uncomplete answer..anyone can solve it?
    By jason07 in forum C++ Programming
    Replies: 9
    Last Post: 09-13-2005, 04:56 PM
  3. Can anyone help me to answer this question
    By I_need_help in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-06-2003, 11:12 PM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  5. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM