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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:26:04+00:00 2026-05-13T15:26:04+00:00

I got into a problem with my classes, passing a const object (polymorphic structure)

  • 0

I got into a problem with my classes, passing a const object (polymorphic structure) to an explicit constructor which takes a const reference to the base class of that polymorphic structure.
Here is the sample (this is not from my code, it is for explanation here)

class Base 
{
...
}

class Derived:public Base
{
...
}

class Problem 
{
    Problem(const Base&);
...
}

void myFunction(const Problem& problem) 
{
    ...
}

int main() 
{
    //explicit constructor with non const object
    Derived d;
    Problem no1(d); //this is working fine 
    myFunction(no1);

    //implicit constructor with const object
    Problem no2=Derived(); //this is working fine, debugged and everything called fine
    myFunction(no2);   //is working fine

    //explicit constructor with const object NOT WORKING
    Problem no3(Derived());  //debugger jumps over this line (no compiler error here)
    myFunction(no3);   //this line is NOT COMPILING at all it says that:
    //no matching function for call to myFunction(Problem (&)(Derived))
    //note: candidates are: void MyFunction(const Problem&)
}

It seems that it is working fine with the second version (explicit constructor call for Problem) only if i explicitly cast the Derived object to its base class Base it like:

Problem(*(Base*)&Derived);

I do not realize the difference between calling impicitly and explicitly the constructor of the Problem class.
Thank you!

  • 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-13T15:26:04+00:00Added an answer on May 13, 2026 at 3:26 pm

    The problem is you aren’t declaring an object, but a function:

    Problem no3(Derived());
    // equivalent to:
    Problem no3(Derived); // with parameter name omitted
    

    Use:

    Problem no3((Derived()));
    // extra parens prevent function-declaration interpretation
    // which is otherwise required by the standard (so that the code isn't ambiguous)
    

    This is a quirk of C’s declaration syntax inherited by C++.

    More examples:

    void f(int(a)); /* same as: */ void f(int a);
    
    void g() {
      void function(int);    // declare function
      void function(int());  // we can declare it again
      void function(int=42); // add default value
      function();            // calls ::function(42) ('function' in the global scope)
    }
    // 'function' not available here (not declared)
    
    void function(int) {} // definition for declarations inside g above
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 297k
  • Answers 297k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not directly possible (not as simply as ECC6,… May 13, 2026 at 7:15 pm
  • Editorial Team
    Editorial Team added an answer Use this property in your pom.xml : <project> ... <build>… May 13, 2026 at 7:15 pm
  • Editorial Team
    Editorial Team added an answer Well, there's got to be some way of relating the… May 13, 2026 at 7:15 pm

Related Questions

I've created a couple of Comparer classes for FileInfo objects to allow sorting by
Well, first of all sorry about this question it must be pretty straight forward
Disclaimer: the question is completely different from Inheritance instead of typedef and I could
I have a complex RIA client that communicates with a WCF SOAP web service,
I've got about 100 unit tests and with a coverage of %20, which I'm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.