CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Feb 2009
    Posts
    24

    Is it possible to hide the default copy constructor.

    Hi,
    I am vijay, I have a question on default constructor.

    Question :

    Is it possible to hide the default copy constructor provide by the compiler...?

    Background :

    If the user writes a parameterised construtor , then default constructor is automatically hidde.

    I tried to modify the copy constuctor by passing the object by pointer and not by reference -> I expected a compiler error but no compiler error was thrown.

    can any one explaine me why compiler didn't throw an error.

    and why i tried to use copy constructor , the compiler used default copy contructor though i have overriden it by my custorm copy constructor.

    am using borland (codegear ) compiler.

    Please find the sample code below.

    #include <tchar.h>
    #include <iostream>
    #include <conio.h>
    using namespace std ;
    //---------------------------------------------------------------------------


    class base
    {
    public :
    int i ;
    base( ){i = 0 ; }
    base(int val){i = val ;}
    // This is interesting though i modified copy constructor to receive
    // object pointer rather object reference as parameter , i didn't get
    // compiler error.
    base(base* baseptr){ this->i = baseptr->i ;}
    };

    int main()
    {
    base objone ;
    // This statement will use default copy contructor and not the one which i have overriden.
    // My question : isn't default copy constructor is hidden
    base objtwo = objone ;
    }
    //---------------------------------------------------------------------------

  2. #2
    Join Date
    Oct 2009
    Posts
    5

    Re: Is it possible to hide the default copy constructor.

    You can always put the default constructor into the private section of your class. But if you make a non-default constructor, all other constructors are hidden.

  3. #3
    Join Date
    Feb 2009
    Posts
    24

    Re: Is it possible to hide the default copy constructor.

    Hi ,
    First of all I don't want to make any constructor private , this leads to singleton class.

    I need answer for my question if all the constructors are made public :-)

    I think you understood my problem.

  4. #4
    Join Date
    May 2002
    Posts
    1,435

    Re: Is it possible to hide the default copy constructor.

    The constructor that you have created is not standard. Copy constructors and assignment operators take a const reference as a parameter and you have used a volatile pointer. It appears to me that you are expecting this line of code:
    Code:
    base objtwo = objone ;
    To call this custom constructor:
    Code:
    base(base* baseptr)
    {
    	this->i = baseptr->i ;
    }
    If that's the case, then all you need to do is use the address-of operator:
    Code:
    base objtwo = &objone ;

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Is it possible to hide the default copy constructor.

    Quote Originally Posted by VijayDandur View Post
    I tried to modify the copy constuctor by passing the object by pointer and not by reference -> I expected a compiler error but no compiler error was thrown.
    That is because what you wrote is not a copy constructor -- it is your own user-defined constructor.

    A copy constructor must have one of the following prototypes:
    Code:
    class foo
    {
        foo(foo &);
        foo(const foo &);
        foo(const volatile foo &);
        foo(volatile foo &);
    }
    In addition to those 4, a copy constructor can have additional arguments, provided that all of the additional arguments have default values.

    A constructor with a pointer as the first argument is not a copy constructor.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Dec 2009
    Posts
    26

    Re: Is it possible to hide the default copy constructor.

    don't listen to paul. he copulates with his mother on a semi-regular basis

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Is it possible to hide the default copy constructor.

    Quote Originally Posted by sufood View Post
    don't listen to paul. he copulates with his mother on a semi-regular basis
    Your post has been reported. I hope you had a nice short stay here.

  8. #8
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Is it possible to hide the default copy constructor.

    Quote Originally Posted by sufood View Post
    <some usual crap>
    Your juvenile behavior pissed off almost everybody here (except for few poor students that will be pissed of when they get their assignment graded).
    There is probably no reason to ask you to stop. You likely enjoy this kind of things.
    Would like to advise you to seek professional help. If you disclose your location, someone here might suggest a good behavioral psychiatrist in your area.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  9. #9
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Is it possible to hide the default copy constructor.

    The copy constructor is either provided by you, or provided implicitly by the compiler(may or may not do the correct thing), or declared private by you and never implemented, which disallows copying.

    Be sure unless you declare a copy constructor yourself( as paul has already shown you), one will be provided by the compiler that is public and does shallow copying, unless it is impossible to implement. ie a base class has a private copy constructor.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  10. #10
    Join Date
    Feb 2009
    Posts
    24

    Re: Is it possible to hide the default copy constructor.

    Hi 0xC0000005 and paul,

    Thanks for your reply. so as i understand the below code

    base(base* baseptr)
    {
    this->i = baseptr->i ;
    }

    won't be considered as copy constructor at all and hence the default copy constructor is not hiddent.

    Is my understanding is correct.

    If you feel that my understanding is correct then am convinced with the answer.

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Is it possible to hide the default copy constructor.

    If you feel that my understanding is correct then am convinced with the answer.
    Well, the approach sounds quite unusual. If I were you, to be conviced with the answer I'd never need anybody to feel anything. It's just about writing a class with the two constructors (the one complying with the explained copy constructor rules and another one with the pointer), stepping through the code and seeing what specific constructor you're in.

    Code:
    #include <iostream>
    using namespace std;
    
    class base
    {
    public:
    	base() {}
    	base(const base& r)
    	{
    		cout << "In copy constructor" << endl;
    	}
    	base (base* p)
    	{
    		cout << "In special constructor" << endl;
    	}
    };
    
    int main()
    {
    	base obj; 
    	base objCopy1 = obj; 
    	base objCopy2 = &obj;
    	return 0;
    }
    Best regards,
    Igor

  12. #12
    Join Date
    May 2002
    Posts
    1,435

    Re: Is it possible to hide the default copy constructor.

    Quote Originally Posted by VijayDandur View Post
    Hi 0xC0000005 and paul,

    Thanks for your reply. so as i understand the below code

    base(base* baseptr)
    {
    this->i = baseptr->i ;
    }

    won't be considered as copy constructor at all and hence the default copy constructor is not hiddent.

    Is my understanding is correct.

    If you feel that my understanding is correct then am convinced with the answer.
    Your understanding is correct. the 'copy constructor' you have created with a pointer is not a copy constructor in the true sense of the term (as the compiler considers it). However, the C++ language is flexible enough to allow you to create and use a non-standard constructor such as you have done. In the past, I have done this myself - create a custom constructor that creates an object from a pointer to another object rather than a reference.

    It is unclear to me what you are trying to do - and your use of the word hidden is a bit confusing. In the case of the code you supplied the default copy constuctor base(const base& rValue) still exists but it is hidden - and will be used if you write this code: base objtwo = objone.

    If you want to 'unhide' the default copy constructor then you must define it yourself.
    Code:
    base(const base& rValue)
    {
      this->i = rValue.i
    }
    If you use this copy constructor then a default hidden one won't be created.

    When I went back to look at your post while typing this reply I noticed that Igor has explained very clearly the same thing as I do here. But I'll still post my reply as you may find the discussion helpful.

  13. #13
    Join Date
    Feb 2009
    Posts
    24

    Re: Is it possible to hide the default copy constructor.

    HI all,
    Thanks a lot.. I got the answer...

    Thanks for your support.

    Regards,
    Vijay.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured