__box problem

This is a discussion on __box problem within the C++ Programming forums, part of the General Programming Boards category; I am reading into Managed C++, came across this code Code: #using <mscorlib.dll> using namespace System; int main() { double ...

  1. #1
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162

    __box problem

    I am reading into Managed C++, came across this code
    Code:
    #using <mscorlib.dll>
    using namespace System;
    
    int main()
    {
    	double salary = 12.84;
    	Console::Write("Salary: ");
    	Console::WriteLine(__box(salary));
    	Console::WriteLine();
    
    	return 0;
    }
    It was said that Write and WriteLine that we have used so far expect a specific type of value. To convert a regular type to this type of value, Managed C++ provides a unary operator called __box. This operator uses parentheses in which you type the value that needs to be converted.

    So I tried this
    Code:
    #using <mscorlib.dll>
    using namespace System;
    
    int main()
    {
    	double salary = 12.84;
    	Console::Write("Salary: ");
    	Console::WriteLine(salary);
    	Console::WriteLine();
    
    	return 0;
    }
    This also works fine....then why do I need to use __box.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    There's probably an automatic conversion that is performed. However, in programming it is always better to make what is happening explicit in your code, so someone reading your code can easily understand it. For instance, in C++ you can assign a double(a number with a decimal point) to an int type:
    Code:
    int n = 0;
    double d = 3.5;
    ...
    ...
    n = d;
    But, since an int is a whole number, the .5 is truncated and n is set equal to 3. That is an automatic conversion that takes place. However, when you are reading the code, you may mistakenly think n=3.5. So, it's better to make the conversion explicit by casting the double to an int before assigning it to n:
    Code:
    int n = 0;
    double d = 3.5;
    ...
    ...
    n = static_cast<int>(d);
    Now, that ugly cast syntax really sticks out and warns the reader that something is happening to d before it is assigned to n.
    Last edited by 7stud; 02-04-2006 at 07:50 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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