Thread: Class Constructors

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    9

    Class Constructors

    Hi, Im not sure bout the following question, i think i might be being lazy and not reading things i should. Anyway could anyone plz tell me the point of using non-default constructors in some cases over using member functions and leaving the compiler to put in the default constructor and destructor.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    consider:

    Code:
    class something
    {
    	int data_;
    	
    	public:
    	
    	int & data;
    	
    	something(void)
    	: data_(0), data(data_)
    	{
    	
    	}
    };
    
    int
    main(void)
    {
    	something a, b = a;
    	cout << a.data << endl;
    	b.data = 1024;
    	cout << a.data << endl;
    }
    b's default constructor assigns the reference to a's data!
    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;
    }

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    oh, is using constructors the only way data can be copied to other objects?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    no, of course not. the declaration:

    something a, b = a;

    resolves to:

    something a, b(a);
    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;
    }

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    would this task be difficult without constructors?

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    It would be more than difficult reference members must be initialised by the constructor's initialiser list, Since you cannot reassign a reference variable.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> could anyone plz tell me the point of using non-default constructors in some cases over using member functions

    In general, you want to initialize an object with all the data it needs as soon as you can. If you do it with member functions, this means that there is a point at which the object is not correctly initialized (between the call to the default constructor and the calls to the member functions to set the data). It also means that when somebody is using the class, they might forget to call the member functions and their class instance will be unintialized.

    By using a constructor to set the values, you are combining the creation and initialization into a single step, and your class instance always has valid values. There are only a few cases where you want to postpone initialization and use a member function, most of the time you want to do it together.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    Ty for explanations. Im just about to approach a chapter in
    "C++ Without Fear" which is dedicated to constructors so perhaps i shall learn more there provided im in the right mindset when i read (sometimes i let it all go over my head, lol)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  3. Replies: 1
    Last Post: 11-10-2002, 07:37 PM
  4. Constructors + Derived class
    By MethodMan in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2002, 05:05 PM
  5. Constructors of a class
    By casanova0o7 in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2002, 03:46 PM