Link to home
Create AccountLog in
Avatar of pacman32689
pacman32689

asked on

Simple question about static char* member in class

How would I set a static member of type char* in my class in my driver?

Would it be along the lines of....


staticMember = new char[strlen(input)+1)];
strcpy(staticMember,input);

From the examples I have seen they always deal with ints. :( Thanks for the help!
Avatar of Infinity08
Infinity08
Flag of Belgium image

Something like this :

class Test {
  private :
    static char *str;
};

char *Test::str = "default";
>> How would I set a static member of type char* in my class in my driver?
Do you mean assign it a value? It is a char * pointer so you just treat it like you would any other char * pointer -- same rules apply. The example you give where you allocate a buffer of memory from the heap and then strcpy to that buffer is perfectly valid, but don't forget to delete the memory when you're done or you'll leak. You could also look at using strdup(). al;though again you must remember to free() or you'll leak.

http://www.mkssoftware.com/docs/man3/strdup.3.asp
Member functions in the class can always reset the static string, for example :

class Test {
  private :
    static char *str;

  public :
    void setStaticStr(char *s) {
        if (str) delete [] str;
        str = new char[strlen(s) + 1];
        strcpy(str, s);
    }
};

char *Test::str = 0;
>> if (str) delete [] str;
You don't need to do that test, it's safe to delete a NULL pointer.
True ... sometimes being overcautious lol
>> True ... sometimes being overcautious lol
Welcome to my world :)
Avatar of pacman32689
pacman32689

ASKER

So this would be valid?
har * DailyAppointmentScheduler::person = new char[strlen("Paul")+1];
strcpy(person,"Paul");

Open in new window

err char* sorry
Ah nvm no
I got a warning from g++ when literary setting person to be "Paul" because I figured I hadn't allocated any memory to it.
>> I got a warning from g++ when literary setting person to be "Paul" because I figured I hadn't allocated any memory to it.
What warning?
For char * DailyAppointmentScheduler::person = "Paul";      
   
DailyAppointmentScheduler.cpp:18: warning: deprecated conversion from string constant to 'char*'

Little example below.

The Q is, why do you want to do this? Playing with statics and heap allocation in this way can get messy. Maybe we can offer a better solution if you tell us what you are trying to do?
#include <cstring>
#include <iostream>
 
struct foo
{
	static char * bar;
};
 
char * foo::bar = NULL;
 
int main()
{
	foo::bar = new char [10];
	strcpy(foo::bar, "hello");
 
	std::cout << foo::bar << std::endl;
 
	delete [] foo::bar;
 
	return 0;
}

Open in new window

SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Haha thanks guys as usual.
excellent as usual, thanks
>> You could do the new in global scope, but not the strcpy. That needs to be done in a member function.
If you do make sure you use nothrow new as normal new can throw exceptions and if it does your program will just crash and you'll have absolutely no idea why and no way to catch/handle the thrown exception!

#include <cstring>
#include <iostream>
#include <new>
 
struct foo
{
	static char const * bar;
};
 
char const * foo::bar = new (std::nothrow) char [100]; // Won't throw but might be NULL
 
int main()
{
	delete [] foo::bar;
 
	return 0;
}

Open in new window