Thread: question about passing arg in constructor??

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    43

    question about passing arg in constructor??

    Hi ,

    I have a question about passing arguments in a constructor. I need to be able to pass a string in a constructor. THis string in turn will be used to open a file by that same name. I am storing this name as a private member of a class

    I can pass the string, but when I get to opening a file by that name, I am not sure how to do this. I do have this in different files, but put in one file here.

    I have posted the code below, along with where I am not sure how to open a file with the same name as the string that was passed in the constructor.

    Any help will be appreciated. THanks for any help.





    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    class data
    {
     public:
     data(string); //contructor
     void somefunction();
    
     private:
     string name;  };
    
    
    data::data(string somename)
    {name = somename;}
    
    void data::somefunction()
    {
      ofstream outfile;
     
     outfile.open(name); //??? question about passing the stored string name here?
    
      /*----- some output code----*/
    
      outfile.close();  }
    
    
    int main ()
    {
      data one("somename");
     
      one.somefunction();   }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Try:
    outfile.open(name.c_str());
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    43

    thanks - another question?

    Hi ,

    THanks a lot!!, that did work..

    I do have 2 additional questions:

    1) what does name.c_str() mean??? Could you please explain what exactly this means?? THanks a lot for the explanation.



    2) I thought I had used code tags, but maybe I am not doing it correctly: I click on the "Insert Source Code" icon, it puts a [CODE] , and then I paste my code, and then I click the "Close all tags" icon which then places a [CODE]\

    THen I post my message, am I supposed to do something additional??

    THanks for the guidance!!!

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: thanks - another question?

    Originally posted by smd
    Hi ,

    THanks a lot!!, that did work..

    I do have 2 additional questions:

    1) what does name.c_str() mean??? Could you please explain what exactly this means?? THanks a lot for the explanation.



    2) I thought I had used code tags, but maybe I am not doing it correctly: I click on the "Insert Source Code" icon, it puts a [CODE] , and then I paste my code, and then I click the "Close all tags" icon which then places a [CODE]\

    THen I post my message, am I supposed to do something additional??

    THanks for the guidance!!!
    1) the method returns a pointer to a const C-Style string.

    2) No, you did it correctly.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    what does name.c_str() mean??? Could you please explain what exactly this means?? THanks a lot for the explanation.

    In C++ there are two types of strings: cstyle strings and string objects. cstyle strings are character arrays that end in '\0'. string objects are more flexible and are generally preferred. The c_str() function of the string class returns a const char* to a cstring that contains the same string literal as is in string.

    To use any function including the ofstream function open() you have to send the correct parameters to the function. open() requires a parameter of type const char*, so you cannot use the "name" variable which is a string type as a parameter to the open() function.

    Whenever you use a C++ function, you should get in the habit of checking the function prototype against the arguments you are sending to the function to make sure they are appropriate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct question... difference between passing to function...
    By Sparrowhawk in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2009, 03:59 PM
  2. Question about passing & receiving parameters
    By TCB in forum C Programming
    Replies: 9
    Last Post: 04-11-2006, 06:08 AM
  3. interesting indeed! (constructor question)
    By Chaplin27 in forum C++ Programming
    Replies: 2
    Last Post: 01-06-2005, 11:31 AM
  4. Replies: 2
    Last Post: 01-04-2003, 03:35 AM
  5. Replies: 3
    Last Post: 05-29-2002, 02:08 PM