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.
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 ;
}
//---------------------------------------------------------------------------
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.
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:
Re: Is it possible to hide the default copy constructor.
Originally Posted by VijayDandur
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:
Re: Is it possible to hide the default copy constructor.
Originally Posted by sufood
<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: FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...
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.
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;
}
Re: Is it possible to hide the default copy constructor.
Originally Posted by VijayDandur
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.
Bookmarks