is use of pointer variable right?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: is use of pointer variable right?

  1. #1
    Join Date
    May 2005
    Posts
    58

    Question is use of pointer variable right?

    hi i have the follwing problem.

    i have one function openWriteStream ()

    now this function is having protype like

    ofstream *openwritestream()

    this function opens a file and return the pointer of type ofstream

    now i want to open a file andwrite somting to that file so i am trying somthing like this tell me if this is correct?


    ofStream *out;
    out = openWriteStream ()
    *out <<"i want to write this to file";


    is it right?and is this piece of code will help in writing to thefile opened by openwritestream?

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    this is terrible, really, but yes it will work provided your function allocated the pointer inside it (var = new ofstream)

    what is wrong with
    ofstream out;
    out.open("filename");
    out << "text";

    which
    1) gets rid of potential pointer errors
    2) uses debugged, language availble code
    3) is faster
    4) is easier to read, and easier to write

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    I odn't see the point in calling a function that does what the fstream class's constructor already does: open the stream. However, if you must use such a function, at least pass the stream object by reference to the function:

    int OpenStream (fstream & str);

    Then check the return status to see whether the operation succeeded. Again, this is pretty redundant because you don't need to reinvent the wheel here: simply write:

    oftsream myofs("myfile.dat");
    if (myofs)
    myofs<<"my message";
    Danny Kalev

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center