question about static member

im trying to compare two strings length and print the longest but because i had to define the string as a static one i cant find a way to do it...help??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>

using namespace std;

class String
{
	static char s[255];
	int len;
public:
	void assign(const char*);
	int length()const;
	void print()const;
};

void String::assign(const char* str)
{
	strcpy(s,str);
	len=strlen(str);
}

int String::length()const
{
	return len;
}

void String::print()const
{
	cout<<"The string: "<<s<<endl;
}

int main()
{
	String s1,s2;
	s1.assign("Goodbye");
	s2.assign("Hello");
        if (s1.length()>=s2.length())
        s1.print();
        else
        s2.print();
	
}


after this code the output is always s2's string. i understand its because the string declared as static. my question is how can i make the if-else work?? thx..
i had to define the string as a static

Why?
thats what i was asked for in the exercise..
Can you post us the actual question?
i was asked to create a class with a static char array limited to 255 and and another data member representing the string's length. then what im asked to do its creating two objects and compare them both and print the string with the bigger length...
On the face of it, it sounds an impossible task, a static member only exists once and is shared between all instances of the class.

even though you create 2 string objects, they share the character array, so the most recent assignment will always overwrite all previous assignments.

If that really is the question, then it looks like a trick one to me :)
but maybe there's a way to store it after the first initialization and then after the second one and in that way keep both of the strings?
It still doesn't make sense.

If that were the case then you wouldn't really be comparing the classes. It seems counter-intuitive for the string to be static at all.
@alsade

i couldn't figure out that, why are you using "class" to compare only 2 strings??
you can simply do it like this:

1
2
3
4
len1=strlen(string1);
len2=strlen(string2);
if(len1>len2)    cout<<string1;
if(len2>len1)    cout<<string2;


over.

[EDIT]
Now my question is: why do you need class here?

(and sorry for bad English)
Last edited on

1
2
3
4
5
6
7
8
String s;
s.assign("Goodbye");
int len1 = s.length();

s.assign("Hello");

if (len1 != s.length())
  ...


I still think its a trick question testing your understanding of static data.
@iHutch105

agree it feels counter-intuitive but that is what i was asked to do..:-/

@aalok

i have to create a class and use it, thats part of the exercise
@alsade
is it stated in the exercise to use static strings?
yes, static string limited to 255
I bet confusing terms: Not static, but static as in non-dynamic.

char array[255]; vs char * array = new char [255];

Remove the word 'static' from the class.



Note: I could call s1.assign( text_of_entire_LOTR() ); and you would happily overwrite whatever happens to be after your 255 bytes. Modify your assign to check and guard against such input.
Topic archived. No new replies allowed.