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 7617833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:14:42+00:00 2026-05-31T03:14:42+00:00

Here is my code: 1 #include <cstdio> 2 3 struct Foo; 4 5 struct

  • 0

Here is my code:

  1     #include <cstdio>
  2 
  3     struct Foo;
  4 
  5     struct Bar {
  6         Foo *foo;
  7         Bar(const Foo& fo) {
  8             printf("In Bar copy_contr\n");
  9         }
 10 
 11         Bar&  operator=(Foo& fo) {
 12             printf("In Bar assignment\n");
 13             this->foo = &fo;
 14         }
 15     };
 16 
 17     struct Foo {
 18         Bar *bar;
 19         Foo() {
 20             printf("In default\n");
 21         }
 22 
 23         Foo(const Bar& b) {
 24             printf("In Foo copy constructor\n");
 25         }
 26 
 27         operator Bar() {
 28             printf("In typecast from Foo to Bar\n");
 29             return *(this->bar);
 30         }
 31 
 32     };
 33 
 34 
 35     int f(Bar b) {
 36         return 0;
 37     }
 38 
 39     int main() {
 40         Foo fo;
 41         f(fo);
 42         Bar *b = &static_cast<Bar>(fo);
 43 
 44         return 0;
 45     }

Here is the print out:

In default
In typecast from Foo to Bar
In Bar copy_contr

I am confused at the 3rd printout: how come copy constructor is called, it should be type casting, there is specific static_cast there.

I also have an interesting find – if I comment out line27 – line30, basically make disappear the typecasting function, the printout changes to
In default
In Bar copy_contr
In Bar copy_contr

So it seems the compiler has some mechanism to search some conversion function in some order, anyone has standard hardy to tell what is the mechanism?

I am using g++ v4.1.2.

Thanks!

  • 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-31T03:14:44+00:00Added an answer on May 31, 2026 at 3:14 am

    I am confused at the 3rd printout: how come copy constructor is called, it should be type casting, there is specific static_cast there.

    The result of the expression static_cast<T>(v) is the result of converting the expression v to type T.

    An expression e can be explicitly converted to a type T using a static_cast of the form static_cast<T>(e) if the declaration T t(e); is well-formed, for some invented temporary variable t.

    The effect of such an explicit conversion is the same as performing the declaration and initialization and then using the temporary variable as the result of the conversion.

    It should be clear from this why the converting constructor is called then in this case.

    I also have an interesting find – if I comment out line27 – line30, basically make disappear the typecasting function, the printout changes to In default In Bar copy_contr In Bar copy_contr

    You are asking why the conversion function Foo::operator Bar is selected (if available) to convert the parameter fo from Foo to Bar instead of the converting constructor Bar::Bar(const Foo&)….

    So it seems the compiler has some mechanism to search some conversion function in some order, anyone has standard hardy to tell what is the mechanism?

    User-defined conversions are applied only where they are unambiguous. However note the following in your paticular case:

    1. Foo fo is non-const (ie not “const Foo fo;”)
    2. Foo::operator Bar() is non-const (ie not “Foo::operator Bar() const”)
    3. Bar::Bar(const Foo&) is const (ie not “Bar::Bar(Foo&)”)

    Therefore the conversion chosen is Foo::operator Bar() for the same reason that:

    class X
    {
        void f();
        void f() const;
    };
    
    X x;
    x.f();
    

    the non-const version of f is selected above. That Foo fo is non-const, so the non-const version is selected.

    If the conversion function was const OR the converting constructor took a non-const reference, than there would be an ambiguity and it would not compile.

    If the conversion function was const AND the converting constructor took a non-const reference, it would select the converting constructor instead of the conversion function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here is my code for xor linked list implementation #include<iostream> using namespace std; struct
Consider this code #include <iostream> #include <cstdio> using namespace std; class Dummy { public:
here's the code: #include <string> class Config { public: static const std::string asdf =
My question is related to this thread. Here is the code #include <stdio.h> int
Test code: #include <cmath> #include <cstdio> const int N = 4096; const float PI
i was solving the problem https://www.spoj.pl/problems/ACPC11A/ and here is my code : #include<iostream> #include<cstdio>
Here is my code: #include <stdio.h> int main(void) { FILE *fp; unsigned int i;
Here is my code: #include <iostream> #include <stdlib.h> #include <fstream> using namespace std; int
Here's a sample code: #include <stack> #include <cstddef> template <std::size_t N, template <class> class
I am going to paste here my code and errors: #include stdio.h #include winsock2.h

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.