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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:31:26+00:00 2026-06-08T09:31:26+00:00

I would like to have a constructor with default argument static_cast like: generate_word_spot(func_double_double_t& cost_f

  • 0

I would like to have a constructor with default argument static_cast like:

generate_word_spot(func_double_double_t& cost_f = static_cast<func_double_double_t&>(func_const_t(1))) :
      cost_f_(cost_f)
      {};

where

class func_const_t : public func_double_double_t 
{ 
   ...
   virtual double operator()(double x){ ... }; 
}

and func_double_double_t is the base class for many function objects similar to this.

GCC says “invalid static_cast” for the above constructor. Is there a way to achieve such a behavior?

  • 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-06-08T09:31:27+00:00Added an answer on June 8, 2026 at 9:31 am

    Are you sure you need a non-const reference in your case? If you can use a const reference then just do

    generate_word_spot(const func_double_double_t& cost_f = func_const_t(1)) :
      cost_f_(cost_f)
      {}
    

    No cast necessary. (Neither is the ; after the definition.)

    Otherwise, for non-const reference binding temporary object is out of question. You’d need to declare a standalone non-temporary object to use as default argument

    func_const_t def_const(1);
    ...
    class generate_word_spot {
      ...
      generate_word_spot(func_double_double_t& cost_f = def_const) :
        cost_f_(cost_f)
        {}
    };
    

    It makes sense to make it static member of the class

    class generate_word_spot {
      ...
      static func_const_t def_const;
      ...
      generate_word_spot(func_double_double_t& cost_f = def_const) :
        cost_f_(cost_f)
        {}
    };
    
    func_const_t generate_word_spot::def_const(1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like my default constructor to create & initialize all the objects shown
Suppose I have a class with 3 constructors, a default (no argument) constructor, a
Is there a way to have some kind of default constructor (like a C++
I have a class that has no default constructor or assignment operator so it
I have a class that is a subclass of QObject that I would like
I have a class obj1 that has no default constructor, and class obj2 that
I have a class X that I would like to put into an STL
I have a generic abstract base class from which I would like to derive
I have the following construction: for (String playerName: players) . I would like to
So in my project i would like have a nice treeview that has images.

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.