Solved

Can Constructor argument have value assigned to it?

Posted on 2013-06-11
5
505 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
  • 2
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 266 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
 

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 266 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
 

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 134 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now