• Advertisement
Sign in to follow this  

realllly basic char* question

This topic is 4027 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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*....

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement