I've the class Directory which does stuff with objects of class File, and overloaded operators, like this:
class Directory
{
std::vector<char> name, timeOfCreation;
std::vector<File> arr;
public:
Directory(std::string);
Directory(const Directory&);
~Directory();
Directory operator += (File);
Directory operator += (Directory);
Directory operator -= (File);
Directory operator ~();
File operator [] (int);
operator int();
friend bool operator % (Directory, File);
friend bool operator % (Directory, Directory);
friend Directory operator + (Directory, Directory);
friend Directory operator - (Directory, Directory);
friend long int operator + (Directory);
friend std::ofstream& operator << (std::ofstream&, Directory);
friend void main();
};
Ok, now the issue arises when in main i have
void main()
{
//make object of Directory d
std::cout << d;
}
The program now calls the operator int() instead of operator <<. Thus the command
std::cout <<d
acts like std::cout << (int)d
, and it writes out the number of Files in my Directory.
Here are the implementations of operators << and int():
Directory::operator int()
{
return (int)arr.size();
}
std::ofstream& operator << (std::ofstream& out, Directory d)
{
// get the max field widths for three fields
int widthLeft = 0, widthMiddle = 0, widthRight = 10;
std::vector<File>::const_iterator i = d.arr.begin();
for(; i < d.arr.end(); ++i)
{
if((int)d.timeOfCreation.size() > widthLeft)
widthLeft = (int)d.timeOfCreation.size();
if((int)d.name.size() > widthMiddle)
widthMiddle = (int)d.name.size();
}
out<<std::setw(widthLeft)<<"Date & Time of creation";
out<<std::setw(widthMiddle)<<"Name";
out<<std::setw(widthRight)<<"Total Size\n";
return out;
}
Note: the operator << isn't finished yet, I'm just testing the setw function, but it still should write out that one line.