?
Solved

Can Constructor argument have value assigned to it?

Posted on 2013-06-11
5
Medium Priority
?
518 Views
Last Modified: 2013-06-11
class UsbSocket : public Socket
{
  public:
   
    struct USBStatus
    {
      char txReady       : 1;
      char rxReady       : 1;
      char hw_connected  : 1;
      char connected     : 1;
      char suspended     : 1;
      char rxRetry       : 1;
      char txFull        : 1;
    };

    /// <summary>Constructor
    /// </summary>
    UsbSocket(unsigned short tId = 0);



In above constructor,  is argument assigned value of 0.  Can value be passed to this constructor?
0
Comment
Question by:naseeam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 1064 total points
ID: 39238831
A constructor is just a function (all be it a slightly special one) and, yes, it can have a default value just like any other function can.

In your example the default value for the parameter is, indeed, zero but you can override that by passing an explicit argument yourself.
0
 
LVL 1

Author Comment

by:naseeam
ID: 39238889
What's the use of assigning default value when it will be overridden when the functions is called?
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 1064 total points
ID: 39238898
Well, it doesn't have to be. The idea is it's a sensible default but it gives you, the consumer of that class, the ability to override it. For example, a bank account class might default to a balance of zero but it could be constructed with a balance of more than zero if the customer was planning on paying in at the same time the account is opened.
0
 
LVL 1

Author Comment

by:naseeam
ID: 39238950
What will be the syntax of the function call if customer makes the call with default argument?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 536 total points
ID: 39239181
The syntax would be like

UsbSocket a(2); // explicit argument
UsbSocket b; // default argument

Open in new window


or

UsbSocket* pa = new UsbSocket(2); // explicit argument
UsbSocket* pb = new UsbSocket; // default argument

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question