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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:11:12+00:00 2026-05-27T08:11:12+00:00

I have a constructor that accepts an object of type Material : SomeClass::SomeClass( const

  • 0

I have a constructor that accepts an object of type Material:

SomeClass::SomeClass( const Material& mat ) ;

However, Material allows construction by a Vector:

Material::Material( const Vector& v ) ;

Therefore, SomeClass can allow construction by a Vector:

SomeClass m( vec ) ; // valid, since vec is constructed to a Material first,
// then is passed to the SomeClass(Material) ctor

However, after “shooting myself in the foot” more than once with ctors of this type (in different classes in the same project!) I want to disallow construction of SomeClass by Vector objects directly, instead always requiring a Material to be passed instead.

Is there a way to do this? Somehow think it has to do with the explicit keyword.

  • 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-27T08:11:13+00:00Added an answer on May 27, 2026 at 8:11 am

    You cannot do this without interfering with your ability to transparently construct Material from Vector.

    If you make Material‘s constructor explicit

    explicit Material( const Vector& v ) ;
    

    then you will always have to write Material(v) in order to construct an instance. This will prevent you from instantiating SomeClass with a Vector accidentally, but it will also break all expressions that evaluate to a Vector where a Material is expected.

    This makes sense, because by not declaring the constructor explicit you are saying “a Vector is just as good as a Material no matter what the context”. You cannot then do a half-step backward and say “oh, well, except when constructing a SomeClass“.

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

Sidebar

Related Questions

I have an object called Result<T> that has a constructor which accepts an argument
In C++ I have a constructor that accepts an object of class descriptor .
I have a mocked object that is passed as a constructor argument to another
Say you have a Price object that accepts either an (int quantity, decimal price)
I have a constructor that has a series of classes and functions within it
I have a constructor that looks like this (in c++): Interpreter::Interpreter() { tempDat ==
For my exception class i have a constructor that has multi arguments (...) which
I'm working on someone's code and they have a constructor that uses: class qwerty
Is it good practice to have a class constructor that uses default parameters, or
I have a constructor (for an auto generated class) that has 255 paremeters. Using

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.