Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

how to override operator =

I've written a programm:

class test
{
  private:
    ofstream  osf;
  public:
    test(ofstream& of);
};

test::test(ofstream& of)
{
  osf=of;
  osf<<"text......"<<endl;
}

main()
{
  char  file[256];
  ofstream fout;
  fout.open(file);
  test ts=new test(fout);
  fout.close(file);
}

As I compile the programm, it shows an error message:"Can not generate operator =()". So I add the operator overriding:

class test
{
  private:
    ofstream  osf;
  public:
    test(ofstream& of);
    friend ofstream& operator=(ofstream& s);
};

ofstream& operatoe=(ofstream& s)
{
  return *this;
}

test::test(ofstream& of)
{
  osf=of;
  osf<<"text......"<<endl;
}

main()
{
  char  file[256];
  ofstream fout;
  fout.open(file);
  test ts=new test(fout);
  fout.close(file);
}

I re-compile the programm once again, but this time it shows: "should be 1 parameter for operator". What's wrong?
0
chenwei
Asked:
chenwei
  • 4
  • 3
  • 3
  • +1
1 Solution
 
alexoCommented:
Suggestion #1:

>> ofstream& operatoe=(ofstream& s)
                                ^
A spelling mistake?

Suggestion #2:

"Can not generate operator =()" is a very meaningful comment!
The compiler says that (a) operator=() is not defined/accessible for the class and (b) it cannot create a default one.  Why?  Because the programmer of the stream classes explicitly forbid this by declering this operator private or protected.

Lo and behold:

        class fstream : public iostream { /* ... */ };

        class iostream : public istream, public ostream {
        public:
            iostream(streambuf*);
            virtual ~iostream();
        protected:
            iostream();
            iostream(const iostream&);
            inline iostream& operator=(streambuf*);
            inline iostream& operator=(iostream&); // <---- Ooga booga!!!
        private:
            iostream(ios&);
            iostream(istream&);
            iostream(ostream&);
        };



0
 
trestanCommented:
You do not need to override the = operator. Try the following code. If you are satisfied with it, do not forget to reject the answer so that the person gives you an answer can get the points.
#include<iostream>
#include<fstream>

using namespace std;

class test
{
  private:
    ofstream *osf;
  public:
    test(ofstream& of);
};

test::test(ofstream& of)
{
  osf=&of;
  *osf<<"text......"<<endl;
}

void main()
{
  char  file[256]="test.txt";
  ofstream fout;
  fout.open(file);
  test ts(fout);
  fout.close();
}


0
 
chenweiAuthor Commented:
I am very satisfied with trestan's answer.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nietodCommented:
Note that trestan's code does not work exactly like your code.  There are subtle, but profound differences.  They may not matter to you, but you should consider them before you employ that solution.  (Its possible also that the differences are even desired--but you have to make that determination.)
0
 
nietodCommented:
Also not that you said you are satisfied with trestan's answer, but you have accepted alexo's answer.  If you don't like a proposed answer and do like an answer listed in a comment, you must reject the proposed answer and then ask the person who submitted the comment to submit an answer.  Then you can grade that answer.
0
 
trestanCommented:
But you give your points to another experts. Chenwei, did you notice that?
I found it happens often.
0
 
chenweiAuthor Commented:
First I will like to thank a lot to all sites. No matter how much help I got, you have help me a lot.
Besides, I am not sure why the points I gave trestan is given to another. Perhaps as I wrote the commend and sent it, the other has put his answer.

Thanks a lot again!!!
0
 
chenweiAuthor Commented:
By the way, if tretan's code dosen't work exactly like my code, what's your answer, Mr. Nietod?
0
 
trestanCommented:
When you rate the question, you should notice whoes name is under answer column. And you can correct it if you want. You can either ask Linda to re-assign the point (although it is not prefered)or post another question indicating to whom the points belongs to.

0
 
nietodCommented:
There is no answer.  As Alexo pointed out what you want were trying to do is explicitly forbidden.  Trestan's approach on the otherhand will compile and will work similarly to what you had in some cases.  However, it will work differently than what you tried is some cases.  That does not mean it is a bad solution.  But you should be aware of the fact that it mayl behave differently than you originally intended.  

for example

ofstream S1);
S1.open(filename.txt"):;
test T1(S1);
{
   ofstream S2);
   S2.open(filename2.txt"):;
   test T2(S2);
   T1 = T2;
   // now T1 and T2 share S2.  That is different behaviour than what you had.
}
 // now S2 is destroyed, but T1 still tries to use it.  This is dangerous.
0
 
alexoCommented:
There is a very large difference between your original intent and trestan's suggestion.

Orignally, you tried to have a copy of the stream in another, independent, object.  This is explicitly forbidden and probably a bad idea anyway.

trestan suggested having a pointer to the *original* stream object.  Granted, this as a much better idea but you get different behaviour (as nietod pointed out).

It is usually preferred to use a reference instead of a pointer in such cases, but then you must *initialize* (not assign) it in the constructor.  Thus:



    class test
    {
      public:
        test(ofstream& of);
      private:
        ofstream& osf;
    };

    test::test(ofstream& of): osf(of)
    {
      osf << "text......" << endl;
    }

>> You can either ask Linda to re-assign the point (although it is not prefered)
Why not?  Fair is fair.  If you feel trestan gave you a better answer, post a 0-point question in the Expert Exchange Cutomer Support area and tell Linda that the points belong to him.

Don't forget to complain LOUDLY that the current form is non-intuitive, confusing and aggravating.
0
 
chenweiAuthor Commented:
Hi, Alexo:

Thanks a lot for your answer!

Besides, I am not sure if Linda is a beautiful girl or not. If yes, I feel shy to critisize her.
0
 
alexoCommented:
>> Thanks a lot for your answer!
You're welcome!

>> I feel shy to critisize her.
You won't be criticizing her.  She would (hopefully) pass your complaint to the programmers, who would (hopefully) make some changes.  I have it on good authority that the EE programmers are male.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now