Solved

Can Constructor argument have value assigned to it?

Posted on 2013-06-11
5
516 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
 
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 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
 
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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

733 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