Please Answer My Question:

This is a discussion on Please Answer My Question: within the C++ Programming forums, part of the General Programming Boards category; What's the difference between contructors and member accessors like SetValue(int x)? I already know what constructors do, and that they're ...

  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,670
    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.



    ITSA
    Socket Library!

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, 10:12 PM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21