realllly basic char* question

Started by
10 comments, last by Zahlman 17 years ago
My problem is really elementary and yet I've been perplexed by hours by it.. I have a class called "rt_Material". I have this private class var: private: char * texture_name; In the constructor, i pass a char*, and assign it to texture_name rt_Material::rt_Material( ..., char *n,...) cout<<n<<endl; texture_name = n; cout<<texture_name<<endl; { Then, i have an accessor for it: char* rt_Material::getTextureName(){ cout<<"Texture name: "<<*texture_name<<endl; return rt_Material::texture_name; } Now, notice my couts? the first 2 in the constructor always have the correct value. However, the one in the accessor prints out garbage, or nothing at all. There has to be something easy im missing. any ideas?
Advertisement
Are you passing a string literal? If you are, then memory hasn't really been allocated and is probably being invalidated when you go out of scope, in which case you'll have to copy the data from your pointer into a member variable.

I would suggest you forget about using char and start using std::string.
Quote:Original post by CrimsonSun
Are you passing a string literal? If you are, then memory hasn't really been allocated and is probably being invalidated when you go out of scope.

I would suggest you forget about using char and start using std::string.


Ah, yes, this makes sense. I had played around with using std::string, but the accessor requires that i return a char*, and when i do string.c_str() it gives me a const char*. Im sure there is a quick way around this?
cout<<"Texture name: "<<*texture_name<<endl;

shouldnt that line be without the * before texture_name... after all you need a char pointer there... not the thing being pointed at.
Yes, you should definitely be using std::string instead of C-style strings. Code in C++, not FrankenC. [grin]

That aside, though, I see two problems with this code:
char* rt_Material::getTextureName(){cout<<"Texture name: "<<*texture_name<<endl;return rt_Material::texture_name;}
First, you're dereferencing your texture_name pointer. If your intention is to print the string, you should not do this. Note that your cout statements in the constructor do not do this.

Second, you're returning rt_Material::texture_name, as though it were a static variable, when it pretty clearly is not. This should not even compile; was this just a transcription error, perhaps?
Quote:Original post by MrAccident
Yes, you should definitely be using std::string instead of C-style strings. Code in C++, not FrankenC. [grin]

That aside, though, I see two problems with this code:
char* rt_Material::getTextureName(){cout<<"Texture name: "<<*texture_name<<endl;return rt_Material::texture_name;}
First, you're dereferencing your texture_name pointer. If your intention is to print the string, you should not do this. Note that your cout statements in the constructor do not do this.

Second, you're returning rt_Material::texture_name, as though it were a static variable, when it pretty clearly is not. This should not even compile; was this just a transcription error, perhaps?



Both of the things you pointed out were just just acts of desperation by me. :)
I think crimsonsun hit the nail on the head, Now all thats left is to figure out how to get a char* out of a std::string
Quote:Original post by OOBrad21
Quote:Original post by MrAccident
Yes, you should definitely be using std::string instead of C-style strings. Code in C++, not FrankenC. [grin]

That aside, though, I see two problems with this code:
char* rt_Material::getTextureName(){cout<<"Texture name: "<<*texture_name<<endl;return rt_Material::texture_name;}
First, you're dereferencing your texture_name pointer. If your intention is to print the string, you should not do this. Note that your cout statements in the constructor do not do this.

Second, you're returning rt_Material::texture_name, as though it were a static variable, when it pretty clearly is not. This should not even compile; was this just a transcription error, perhaps?



Both of the things you pointed out were just just acts of desperation by me. :)
I think crimsonsun hit the nail on the head, Now all thats left is to figure out how to get a char* out of a std::string


Why do you need to return a pointer to a char? If you need to modify the variable, return a reference or a pointer to a string - modifying a string is a million flavors more fun than modifying a character array.
Quote:Original post by CrimsonSun
Quote:Original post by OOBrad21
Quote:Original post by MrAccident
Yes, you should definitely be using std::string instead of C-style strings. Code in C++, not FrankenC. [grin]

That aside, though, I see two problems with this code:
char* rt_Material::getTextureName(){cout<<"Texture name: "<<*texture_name<<endl;return rt_Material::texture_name;}
First, you're dereferencing your texture_name pointer. If your intention is to print the string, you should not do this. Note that your cout statements in the constructor do not do this.

Second, you're returning rt_Material::texture_name, as though it were a static variable, when it pretty clearly is not. This should not even compile; was this just a transcription error, perhaps?



Both of the things you pointed out were just just acts of desperation by me. :)
I think crimsonsun hit the nail on the head, Now all thats left is to figure out how to get a char* out of a std::string


Why do you need to return a pointer to a char? If you need to modify the variable, return a reference or a pointer to a string - modifying a string is a million flavors more fun than modifying a character array.


Im using a precompiled function in a .o file that only accepts char*. so I somehow need to get a string to a non-const char*....

If you really need to return a non-const char pointer (which you really shouldn't), you can const_cast<char*> your string's c_str() return.
Quote:Original post by CrimsonSun
If you really need to return a non-const char pointer (which you really shouldn't), you can const_cast<char*> your string's c_str() return.


Yeah, i don't have a choice since im working with an object file. const_cast worked, thanks!

This topic is closed to new replies.

Advertisement