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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:52:17+00:00 2026-05-19T12:52:17+00:00

I am trying to understand the rules of c++ automatic and explicit conversions in

  • 0

I am trying to understand the rules of c++ automatic and explicit conversions in regular or member function calls. I wrote the following code and it fails compilation:

#include <iostream>
#include <string>

using namespace std;

class testExplicit {
    public:
        int intval;
        short shortval;
        double doubleval;
        char charval;
        string strval;
        testExplicit(int a1, short a2, double a3, char a4, string& a5):
            intval(a1),shortval(a2),doubleval(a3),charval(a4),strval(a5){}

        void getVal(int& a) { a = intval; cout << "IntVal\n"; }
        //        void getVal(short& a) { a = shortval; cout << "Short Val\n"; }
        //        void getVal(double& a) { a = doubleval; cout << "Double Val\n"; }
        //        void getVal(char& a) { a = charval; cout << "Char Val\n"; }
        //        void getVal(string& a) { a = strval; cout << "String Val\n"; }

};

int main( int argc, char **argv ) { 
    string s ("test Str");

    testExplicit test (100,10,10.05,5,s);

    int i;
    char c;
    double d;
    short f;
    test.getVal(i);
    test.getVal(c);
    test.getVal(d);
    test.getVal(f);

    return 0;
}

However, can I conclude that the functions only expect the exact matching parameter? I remember reading that automatic conversions happen according to the conversion rules. Can some shed some light on the correct rules pls?

Here is the error:

test.cpp: In function 'int main(int, char**)':
test.cpp:38: error: no matching function for call to 'testExplicit::getVal(char&)'
test.cpp:17: note: candidates are: void testExplicit::getVal(int&)
test.cpp:39: error: no matching function for call to 'testExplicit::getVal(double&)'
test.cpp:17: note: candidates are: void testExplicit::getVal(int&)
test.cpp:40: error: no matching function for call to 'testExplicit::getVal(short int&)'
test.cpp:17: note: candidates are: void testExplicit::getVal(int&)

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-19T12:52:18+00:00Added an answer on May 19, 2026 at 12:52 pm
    void getVal(int& a);
    

    This function takes an int by reference. You must pass it an int. This is similar to how if you had a function

    void getVal(int* a);
    

    you would need to pass it a pointer to an int (not a pointer to a short or any other type).

    One reason for this is that you are able to modify the int from within the function. In order to allow you to pass an object of another type (e.g. a short) to this function, a temporary object of type short would have to be created at runtime and a reference to that temporary object would have to be passed.

    This wouldn’t be ideal because you might accidentally end up passing the wrong type of object (e.g. a short) and expecting it to be modified by the function, when in fact a temporary copy of type int would be modified by the function, not the original short.

    You are, however, permitted to bind const references to temporary objects, so if your function was declared as

    void getVal(const int& a);
    

    you would be able to pass it any type that is convertible to int. This makes some sense, since the function cannot modify the referenced object (because it is a const reference), so the “oops, I’m accidentally modifying a temporary object” problem doesn’t exist.

    Conversions can also take place when you pass by value, but this too makes sense: when you pass by value, a copy has to be made anyway (the copy that is passed to the function by value), so the conversion can take place as part of that copy.

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

Sidebar

Related Questions

I'm having terrible trouble trying to understand python scoping rules. With the following script:
Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand the math of this code snippet. A token is provided which
I am new to Mathematica and am trying to understand patterns and rules. So
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
Trying to understand something. I created a d:\svn\repository on my server. I committed folders
I trying to understand if a isset is required during form processing when i
Just trying to understand that - I have never used it before. How is
Been trying to understand shared pointer for a few days now and it feels
I am trying to understand how encrypted (password protected) Office 2007 documents are bundled

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.