String as a name of an object.

Hello
Is there any way to implement this in C++ ?
Or is there any alternatives to do this?
I mean using a string as a name of an object.

1
2
3
4
5
6
7
8
9
10
11
#include <string>
#include <fstream>


using namespace std;

main () {
string name = "myfile"; 
fstream name("anypath.txt")
name.close();
}
Hello mynameisalexey,

You missed the "int" in "int main()".

I would say no. Try it using the gear icon in the top right of the code box and see what errors you get.

I believe since "fsream" is a type it needs an acual name not a variable.

Hope that helps,

Andy
Perhaps use #define name myfile

Is this just asked out of curiosity, or is there a particular problem you are trying to solve this way?
I mean using a string as a name of an object.


Doesn't make much sense. The objects only have names in your source code. Once the compiler has finished with it, they don't have names anymore.

What are you trying to do?
I'm not exactly sure what OP is asking, tbh. You can't reuse the variable called "name", because C++ is statically typed, meaning type cannot change at run-time. But it doesn't make sense to call a file stream "name" in the first place...

But also note that before C++11, std::fstreams had to take in const char*'s (c strings) and couldn't take in std::strings as a argument. If you compile with C++11 or later, which you should, you can then compile by directly passing the string. Much cleaner.

modern C++11/14/17:
1
2
3
4
5
6
#include <string>
#include <fstream>
int main () {
  std::string name = "myfile"; 
  std::fstream file(name);
}


old C++ (pre C++11):
1
2
3
4
5
6
#include <string>
#include <fstream>
int main () {
  std::string name = "myfile"; 
  std::fstream file(name.c_str());
}
Last edited on
this looks like a twist on the "enum problem" to me.
that is, you can make an enum:

enum e
{
zero, one, two, three
};

but there is no simple way to print "zero" for zero etc and no simple way to have a user type "zero" and get back to the zero value.

or more generally, it is nontrivial to connect the a string version of a variable name to the variable. It can be done with a vector of strings and a class with some smoke and mirrors but the language proper does not support doing this kind of thing directly.
Thank you everybody for the replies. I too guess it is not possible. But just decided to clarify.

Thanks.
Well, theoretically you could do the following (but I can't for the life of me see why you would want to - it's a recipe for disaster):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main ()
{
   string name = "myfile";

   {
   fstream name( "anypath.txt" );
   name.close();
   }

   cout << name;
}




Are you sure that you weren't intending something like (as in @Ganado's post):
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main ()
{
   string name = "myfile";

   fstream infile( name );
   infile.close();
}
Last edited on
Topic archived. No new replies allowed.