Then why doe'nt it compile
Quote:
Originally Posted by laserlight
This does not use the copy constructor, but the copy assignment operator.
I copied the same code, but this time the copy constructor is private. If we are not using the copy constructor then why does it is not compile ?
Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
class strings
{
char str[256];
public :
strings(const char *s)
{
strcpy(str, s);
}
private: // Now the class cant access the copy constructor !
strings(const strings &s)
{
cout << "copy constructor = " << s.str << endl;
strcpy(str, s.str);
}
public:
~strings(void)
{
cout << "Destructing string " << str << endl;
}
void test1(const strings Obj1)
{
cout << "in test1 " << Obj1.str << endl;
}
strings test2(const strings Obj2)
{
cout << "in test2 " << Obj2.str << endl;
return(Obj2);
}
strings test3(void)
{
strings tmp("GoodBye");
return(tmp);
}
};
int main(int argc, char *argv[])
{
strings s1("Hellow world");
// cout << "Calling test1 : " << endl;
// s1.test1(s1);
// cout << "\n\nCalling test2" << endl;
// s1.test2(s1);
cout << "\n\nCalling test3" << endl;
s1 = s1.test3();
system("PAUSE");
return EXIT_SUCCESS;
}
The errors are :
17 E:\learncpp\copy_const\main.cpp `strings::strings(const strings&)' is private
51 E:\learncpp\copy_const\main.cpp within this context
line 51 is "s1 = s1.test3();"