char * and object

I have one question about the scope of char * with c++ object.
For Example :
I have this simple class :
class Hello

void save(char *cPtr);
void say();

char * msgPtr;
End_of Hello.h~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include "hello.h"


void Hello::save(char * cPtr) {
msgPtr = cPtr;

void Hello::say() {
std::cout << msgPtr << "
End_of Hello.cpp~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I want to use this class like this :
#include "hello.h"
int main(int argc, char *argv[]) {
Hello hello1;
char *message = "Helloworld!"; //point_1; //point_2
} //point_3
hello1.say(); //point_4
End_of main.cpp~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In my opinion, this piece of code is not correct.
At the point_1, I allocate the memory with "Helloworld!".
At the point_2, I store the pointer into the object hello1.
At the point_3, End of the scope for the char *message, the memory and data is no longer guarantee for me
At the point_4, I try to print the data.

Do you agree with me?
- If yes, how can I store char[] into one object? I have to use malloc/free to allocate memory?

- If no, plz feel free to explain me your point of view :)

Many thx
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!