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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:23:28+00:00 2026-05-25T01:23:28+00:00

Suppose I have a class that I want to make sure my compiler (GCC

  • 0

Suppose I have a class that I want to make sure my compiler (GCC in this case) doesn’t synthesize any constructors or assignment methods for. I’ve found one way to do this which is to just include a const int member in the class, but this doesn’t rub me to well. Is there an attribute or something which signifies 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-05-25T01:23:28+00:00Added an answer on May 25, 2026 at 1:23 am

    If you define (or only declare) it yourself, then the compiler will not define it for you.

    struct A
    {
         A (); /*declaration is enough to prevent the compiler from 
                 generating default constructor!*/
    };
    

    While the declaration is enough to prevent the compiler from generating default constructor, it is necessary to define it if your code requires the default constructor, otherwise you’ll get linker error.

    In C++11 (the new ISO Standard), you can disable constructors, copy-constructor, and copy-assignment as:

    struct A
    {
        A(const A&) = delete;            //disable copy-constructor
        A& operator=(const A&) = delete; //disable copy-assignment
    };
    

    Now the interesting part

    You can also selectively disable constructor(s) for selected types which makes delete more interesting. Consider this,

    struct A
    {
           A (int) {}
    };
    

    Object of this class can be created not only with int argument, but any type which implicitly converts to int. For example,

    A a1(10);  //ok
    A a2('x'); //ok - char can convert to int implicitly
    
    B b; 
    A a3(b); //ok - assume b provides user-defined conversion to int
    

    Now suppose, for whatever reason, I don’t want the users of class A to create objects with char or class B , which fortunately or unfortunately can implicitly convert to int, then you can disable them as:

    struct A
    {
         A(int) {}
         A(char) = delete;      //disable
         A(const B&) = delete;  //disable
    };
    

    Now here you go:

    A a1(10);  //ok
    A a2('x'); //error
    
    B b; 
    A a3(b); //error - assume (even if) b provides user-defined conversion to int
    

    Online Demo : http://ideone.com/ZVyK7

    The error messages are very clear:

    prog.cpp:9:5: error: deleted function ‘A::A(char)’
    prog.cpp:10:5: error: deleted function ‘A::A(const B&)’

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

Sidebar

Related Questions

Suppose I have a Python class that I want to add an extra property
Suppose I have a class that looks like this: class Derived : // some
Suppose I have a class that processes some data: class SomeClass { public: void
Suppose I have a class Baz that inherits from classes Foo and Bar ,
Suppose I have a class Customer that is mapped to the database and everything
Suppose I have a Base class and a Child class that inherits from Base
Possible Duplicate: How to Deserialize XML document Suppose that I have a class that
Suppose I have a Generic abstract class that provides some default constructor functionality so
Suppose I have a static method of my class that returns an object of
Suppose that we have a class called class1. The class1 has a method called

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.