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

  • SEARCH
  • Home
  • 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 6528637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:33:11+00:00 2026-05-25T09:33:11+00:00

Is it possible and why one would want to do it? class Foo; class

  • 0

Is it possible and why one would want to do it?

class Foo;
class Bar;

......

Foo foo;
Bar bar = static_cast<Bar>(foo);

Normally static_cast is used with numeric types and pointers, but can it work with user defined data types, a.k.a classes?

  • 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-25T09:33:11+00:00Added an answer on May 25, 2026 at 9:33 am
    Bar bar = static_cast<Bar>(foo);
    

    This cast will fail. Foo and Bar are incompatible types, unless atleast one of the following is true:

    • Foo is derived from Bar, Or
    • Bar has a constructor that takes Foo, Or
    • Foo has a user-defined conversion to Bar.

    The bigger question here is not whether it will cast successfully or not. The bigger and the actual question should be: what do you want to get out of such cast? Why would you want to do such a thing in the first place? What is it supposed to do? I mean, how would the Bar object be initialized from Foo object?

    The sensible way to convert one type to another is one of the following ways:

    Either define Foo as:

    class Foo : public Bar
    {
       //...
    };
    

    Or define Bar as:

    class Bar
    {
      public: 
           Bar(const Foo &foo); //define this constructor in  Bar!
    };
    

    Or provide a conversion function in Foo as:

    class Foo
    {
      public: 
           operator Bar(); //provide a conversion function Foo to Bar!
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a class that can use one of four algorithms (and
Is it possible to see which constructor was the generic one? internal class Foo<T>
Would it be possible through c# to actually do a windows search (the one
Ok, this is possibly borderline-subjective, but I wonder where one would put a simple
One possible solution could be simple HTTP get or post request, but that wouldn't
Not sure if this is possible in one Makefile alone, but I was hoping
Can functors be defined locally within the body of a function? One possible use
If possible one that supports at least spell checking: C# string literals HTML content
Is it possible one way or another to, within a Perl script, effectively execute
Is it possible for one to check the strong name of a .NET application

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.