Can we have a statement that calls a constructor, and does nothing with it?
Basically, am overloading the constructor, and using the constructors without assigning it to a variable, as we would usually.
(Normally we wouldn’t do this, but I could see this arising when using functors, possibly.)
Any ideas?…. (I have declared the copy constructor as private, just to make sure that this was not the cause of the problem.)
class myClass
{
public:
myClass (int n, int x) { }
myClass (int n ) { }
private:
myClass (const myClass & t){} // copy constructor is private.....
};
int main()
{
int r = 5;
myClass A( r ); // OK (as per usual)
myClass ( r, r ); // OK
myClass ( 5 ); // OK
myClass ( r ); // not OK : error C2371: 'r' : redefinition; different basic types
// myClass B = myClass ( r ); // this would not work as copy constructor
// has been declared as private
return 0;
}
You have to say
(myClass(r));, with the extra parentheses, due to the parsing rules of C++.(What you said is a declaration of a new variable of name
r, which already exists. Note that you can also sayint(r);to declarer.)