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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:48:44+00:00 2026-06-04T21:48:44+00:00

I would appreciate a solution to the following problem: I have 2 types of

  • 0

I would appreciate a solution to the following problem: I have 2 types of variables

class Type1Str : public string
{
public:
  Type1Str(const string & str) : string(str) {}
};

class Type2Str : public string
{
public:
  Type2Str(const string & str) : string(str) {}
};

I would like to enjoy all the benefits of a string, but prevent cross assignments:

Type1Str t1; 
Type2Str t2;
t1 = t2;                           // <= should not be allowed
Type2Str t2_1(t1);                 // <= should not be allowed
Type2Str t2_2("a string");         // <= should be allowed

How can I do this?

  • 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-04T21:48:45+00:00Added an answer on June 4, 2026 at 9:48 pm

    Unfortunately the OP presented code that didn’t compile.

    Here’s the original code corrected in the most natural way so that it compiles:

    class Type1Str : public string
    {
    public:
        Type1Str() {}
        Type1Str(const string & str) : string(str) {}
    };
    
    class Type2Str : public string
    {
    public:
        Type2Str() {}
        Type2Str(const string & str) : string(str) {}
    };
    

    Assuming that the above was what was meant, all that’s needed to prevent cross assignment is to make the converting constructor explicit.

    #include <string>
    using std::string;
    
    class Type1Str : public string
    {
    public:
        Type1Str() {}
        explicit Type1Str(const string & str) : string(str) {}
    };
    
    class Type2Str : public string
    {
    public:
        Type2Str() {}
        explicit Type2Str(const string & str) : string(str) {}
    };
    
    int main()
    {
        Type1Str t1; 
        Type2Str t2;
    
        t1 = t2;            // A. !Invalid, involves an implicit conversion.
        Type2Str t2_2(t1);  // B. Still allowed, here the conversion is explicit.
    }
    

    In the line marked B the conversion that’s invoked is not an implicit conversion of the actual argument, but the Type2Str‘s explicit conversion constructor. The actual argument t1 matches the formal argument of that constructor directly because t2 is a std::string. The OP wants to prevent also line B.

    One simple way is then to make the conversion even more explicit, namely, to name it.

    I.e., to either introduce a carrier type for the formal argument, or to introduce an extra dummy conversion name argument, or to replace the public conversion constructor with a public factory function. The factory function is simplest and is as of 2012 cost free re efficiency, but it has a cost in maintenance: a derived class must reimplement it in order to offer that functionality. Of the other two solutions mentioned here, the extra dummy name argument is the easiest to implement and the least code:

    #include <string>
    using std::string;
    
    namespace from { enum StdString { stdString }; };
    
    class Type1Str : public string
    {
    public:
        Type1Str() {}
        Type1Str( from::StdString, const string & str) : string(str) {}
    };
    
    class Type2Str : public string
    {
    public:
        Type2Str() {}
        Type2Str( from::StdString, const string & str) : string(str) {}
    };
    
    int main()
    {
        Type1Str t1; 
        Type2Str t2;
        Type2Str t3( from::stdString, t1 ); // OK.
    
        t1 = t2;            // A. !Invalid, no conversion possible.
        Type2Str t2_2(t1);  // B. !Invalid, no conversion possible.
    }
    

    So, what’s wrong with instead just declaring private constructors and assignment operators for all the the types that one wants to prohibit direct copying from?

    Well, besides being brittle, for n types that amounts to O(n2) declarations. So it’s generally not a good idea. As I’m writing this, however, the OP has selected an answer with that approach as “the solution”. Just be warned: it’ really not a good idea, in general.

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

Sidebar

Related Questions

I have a problem finding references to this subject and would appreciate some help.
I would appreciate some help with something I working on and have not done
I would appreciate suggestions as to how to implement the following with Ninject: I
I'm relatively new to Java and would appreciate any help on this! I have
I think I have a conceptual misunderstanding and would appreciate an explanation. Within a
I'm pretty new to Python programming and would appreciate some help to a problem
I would really appreciate some advise on this matter. e.g. class Foo { TData
Please see the following code to understand my question better. class compareByValue { public:
I am working with Wolfram Mathematica 8 and have the following problem. I have
I have ran into a bit of problem. Originally, I have the following input

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.