Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3460706
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:15:44+00:00 2026-05-18T10:15:44+00:00

What is Type Conversion and what is Type Casting ? When should I use

  • 0

What is Type Conversion and what is Type Casting?

When should I use each of them?

Detail: Sorry if this is an obvious question; I’m new to C++, coming from a ruby background and being used to to_s and to_i and the like.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:15:45+00:00Added an answer on May 18, 2026 at 10:15 am

    Conversion is when a value is, um, converted to a different type. The result is a value of the target type, and there are rules for what output value results from what input (of the source type).

    For example:

    int i = 3;
    unsigned int j;
    j = i; // the value of "i" is converted to "unsigned int".
    

    The result is the unsigned int value that is equal to i modulo UINT_MAX+1, and this rule is part of the language. So, in this case the value (in English) is still “3”, but it’s an unsigned int value of 3, which is subtly different from a signed int value of 3.

    Note that conversion happened automatically, we just used a signed int value in a position where an unsigned int value is required, and the language defines what that means without us actually saying that we’re converting. That’s called an “implicit conversion”.

    “Casting” is an explicit conversion.

    For example:

    unsigned int k = (unsigned int)i;
    long l = long(i);
    unsigned int m = static_cast<unsigned int>(i);
    

    are all casts. Specifically, according to 5.4/2 of the standard, k uses a cast-expression, and according to 5.2.3/1, l uses an equivalent thing (except that I’ve used a different type). m uses a “type conversion operator” (static_cast), but other parts of the standard refer to those as “casts” too.

    User-defined types can define “conversion functions” which provide specific rules for converting your type to another type, and single-arg constructors are used in conversions too:

    struct Foo {
        int a;
        Foo(int b) : a(b) {}                   // single-arg constructor
        Foo(int b, int c) : a(b+c) {}          // two-arg constructor
        operator float () { return float(a); } // conversion function
    };
    
    Foo f(3,4);              // two-arg constructor
    f = static_cast<Foo>(4); // conversion: single-arg constructor is called
    float g = f;             // conversion: conversion function is called
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So if your converting from Void* to Type* or from Type* to Void* should
I type ArrayList memberNames = new ArrayList() { Jim, John, George }; and Visual
Consider type like this one public interface IHaveGenericMethod { T1 Method<T1>(T1 parm); T2 Method<T1,T2>(T1
I have a generic class that I'm trying to implement implicit type casting for.
I wrote a method that extracts fields from an object like this: private static
Say I have a class Code defined like this, with a user specified type
Type.GetType(System.String) Is there a lookup for the aliases available somewhere? Type.GetType(string) returns null .
Which type of input is least vulnerable to Cross-Site Scripting (XSS) and SQL Injection
What type of authentication would you suggest for the service that is: implemented as
Is type checking considered bad practice even if you are checking against an interface?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.