char *pString question  
Author Message
Ron62





PostPosted: Mon Apr 10 05:36:09 CDT 2006 Top

Visual Studio C++ >> char *pString question

Hi folks,

Easy one for the die-hard coders out there.

I want to use a string in my program so I'm using a global (declared in the
header file for my class) char *pString. So for example..

// In "MyClass.h"
char *pString;

In the .cpp file I wish to use this string i.e. assign it an intial value in
the constructor such as -

// In "MyClass.cpp" constructor
pString = "Hello";

But, later on in the program I wish to reuse this same pointer to have
different text. So for example, later on in the code I would like to make the
string, for example, "Goodbye" such as -

pString = "Goodbye";

Is there any harm in doing this?

Thanks,
David

Visual Studio368  
 
 
David





PostPosted: Mon Apr 10 05:36:09 CDT 2006 Top

Visual Studio C++ >> char *pString question

> Hi folks,
>
> Easy one for the die-hard coders out there.
>
> I want to use a string in my program so I'm using a global (declared in the
> header file for my class) char *pString. So for example..
>
> // In "MyClass.h"
> char *pString;
>
> In the .cpp file I wish to use this string i.e. assign it an intial value in
> the constructor such as -
>
> // In "MyClass.cpp" constructor
> pString = "Hello";
>
> But, later on in the program I wish to reuse this same pointer to have
> different text. So for example, later on in the code I would like to make the
> string, for example, "Goodbye" such as -
>
> pString = "Goodbye";
>
> Is there any harm in doing this?
>
> Thanks,
> David
>
>

David:

There could be a lot of harm in doing this.

1. If you are going to point pString at read-only memory, it should be
const char*. Use of char* is legacy C.

2. Never define global variables in header files. Use extern.

// In "MyClass.h"
extern const char *pString;

// In MyClass.cpp
const char* pString = 0; // or initialize to some string

Now, anywhere in MyClass.cpp, or in any .cpp file that includes
MyClass.h, you can do, for example

pString = "AnyString";

3. Why are you using a global variable here? If you are setting it in
the MyClass constructor why not make it a static member variable of
MyClass? Global variables are best avoided in C++.

David Wilkinson




 
 
David





PostPosted: Mon Apr 10 06:00:02 CDT 2006 Top

Visual Studio C++ >> char *pString question


>
> > Hi folks,
> >
> > Easy one for the die-hard coders out there.
> >
> > I want to use a string in my program so I'm using a global (declared in the
> > header file for my class) char *pString. So for example..
> >
> > // In "MyClass.h"
> > char *pString;
> >
> > In the .cpp file I wish to use this string i.e. assign it an intial value in
> > the constructor such as -
> >
> > // In "MyClass.cpp" constructor
> > pString = "Hello";
> >
> > But, later on in the program I wish to reuse this same pointer to have
> > different text. So for example, later on in the code I would like to make the
> > string, for example, "Goodbye" such as -
> >
> > pString = "Goodbye";
> >
> > Is there any harm in doing this?
> >
> > Thanks,
> > David
> >
> >
>
> David:
>
> There could be a lot of harm in doing this.
>
> 1. If you are going to point pString at read-only memory, it should be
> const char*. Use of char* is legacy C.
>
> 2. Never define global variables in header files. Use extern.
>
> // In "MyClass.h"
> extern const char *pString;
>
> // In MyClass.cpp
> const char* pString = 0; // or initialize to some string
>
> Now, anywhere in MyClass.cpp, or in any .cpp file that includes
> MyClass.h, you can do, for example
>
> pString = "AnyString";
>
> 3. Why are you using a global variable here? If you are setting it in
> the MyClass constructor why not make it a static member variable of
> MyClass? Global variables are best avoided in C++.
>
> David Wilkinson


Hi David,

Thanks for the reply and informative answer. Apologies for the first post,
it is in actual fact a 'private data member'. I foolishly called it a global
variable where in actual fact it is not, still learning to be more precise
with the terminology, plus the coffee has not hit yet!

// In my header file "MyClass.h"

private:

char *pString;

And then within the "MyClass.cpp" I initialize pString with some value i.e.
pString = "Hello", and I change what pString points to at various parts in
the program i.e.

pString = "Another String";

pString = "One more String";

Would this be ok? Is making it private enough? Or should I make I take
another approach?

Many thanks again, and sorry for the misleading first post,

David
 
 
David





PostPosted: Mon Apr 10 06:51:29 CDT 2006 Top

Visual Studio C++ >> char *pString question

[snip]

> Hi David,
>
> Thanks for the reply and informative answer. Apologies for the first post,
> it is in actual fact a 'private data member'. I foolishly called it a global
> variable where in actual fact it is not, still learning to be more precise
> with the terminology, plus the coffee has not hit yet!
>
> // In my header file "MyClass.h"
>
> private:
>
> char *pString;
>
> And then within the "MyClass.cpp" I initialize pString with some value i.e.
> pString = "Hello", and I change what pString points to at various parts in
> the program i.e.
>
> pString = "Another String";
>
> pString = "One more String";
>
> Would this be ok? Is making it private enough? Or should I make I take
> another approach?
>
> Many thanks again, and sorry for the misleading first post,
>
> David

David:

It should be const char*, not char*.

David Wilkinson

 
 
David





PostPosted: Mon Apr 10 08:31:10 CDT 2006 Top

Visual Studio C++ >> char *pString question
> > Hi David,
> >
> > Thanks for the reply and informative answer. Apologies for the first post,
> > it is in actual fact a 'private data member'. I foolishly called it a global
> > variable where in actual fact it is not, still learning to be more precise
> > with the terminology, plus the coffee has not hit yet!
> >
> > // In my header file "MyClass.h"
> >
> > private:
> >
> > char *pString;
> >
> > And then within the "MyClass.cpp" I initialize pString with some value i.e.
> > pString = "Hello", and I change what pString points to at various parts in
> > the program i.e.
> >
> > pString = "Another String";
> >
> > pString = "One more String";
> >
> > Would this be ok? Is making it private enough? Or should I make I take
> > another approach?
> >
> > Many thanks again, and sorry for the misleading first post,
> >
> > David
>
> David:
>
> It should be const char*, not char*.
>
> David Wilkinson
>

Excellent, thanks! Works nice :-)