Default Constructors

This is a discussion on Default Constructors within the C++ Programming forums, part of the General Programming Boards category; Book : "A constructor with default arguments is equivalent to a default constructor" Is it always?...

  1. #1
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46

    Default Constructors

    Book : "A constructor with default arguments is equivalent to a default constructor"

    Is it always?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Yes.

  3. #3
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46
    But a default constructor, provided by the compiler gives some value to the variables, but with my own constructor i can give any particular value to the variables.. say 10?
    So my constructor is different.

    I feel like a fool right now.

    Am i totally confused?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Try giving more context to your questions.

    A constructor with default arguments is equivalent to a default constructor, meaning that if you provide a constructor with all default arguments then you have provided a default constructor. You can no longer write a separate default constructor, and the compiler will not create one for you. If code that uses your class requires a default constructor, then the constructor you created with all default arguments will be used.

    It does not mean that they will necessarily be implemented in exactly the same way. You are correct that you can give your variables values if you write your constructor that are different than what the compiler gives if it provides a default constructor.

    I'd suggest you keep reading your book to the end of the section or chapter, work through any examples and problems it provides. Then read it a second time. When you read it a second time, you will have a better idea what each statement it makes is referring to, and so you will have fewer questions. If after you've read the chapter a second time you still are confused, then post the question here.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    A default constructor is just one that can be called without any arguments. Therefore, if you have a constructor such that every one of its arguments has a default (for example, a constructor with no arguments at all qualifies), then that constructor can be called without any arguments, and hence is a default constructor. There can be only one default constructor, otherwise the compiler wouldn't know which one to call.

  6. #6
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46
    Thanks guys . That helped a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utilizing another compiled program for a task.
    By kotoroshinoto in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 01:43 PM
  2. Array members copied by the default copy constructors?
    By cyberfish in forum C++ Programming
    Replies: 4
    Last Post: 02-17-2008, 11:46 PM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  4. Default Constructors
    By MethodMan in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2002, 06:30 PM
  5. Default Constructors w/Classes as Parameters
    By GrNxxDaY in forum C++ Programming
    Replies: 22
    Last Post: 07-31-2002, 07:50 AM

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