String get's recognized as boolean?

Can anyone tell me why this happens. I have a class with couple of member functions which are overloaded with different types. The goal was to mimic the behaviour of an heterogeneous container with a basic class and vector. When I assign a string directly it get's treated as if it is a boolean type. Here is a little bit of the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    //Make a vector of 10 Homo's
	vector<Homo> sapiens (10);	

	//Now hand craft some types
		sapiens[0].add(1);
		sapiens[1].add(2.00);
		sapiens[2].add("Hello");
		sapiens[3].add(true);
		sapiens[4].add("something");
		sapiens[5].add(55);
		sapiens[6].add("empty");
		sapiens[7].add(false);
		sapiens[8].add(102.543);
		sapiens[9].add("done");

	/*now run through our vector and call the print function
	  on each one....*/
	
	    for(int i = 0; i < 10; i++)
	    {
		    sapiens[i].print();
	    }


This gives me the output:
1
2
3
4
5
6
7
8
9
10
	Integer -> 1
	Double -> 2
	Boolean -> 1 !! Why?
	Boolean -> 1
	Boolean -> 1
	Integer -> 55
	Boolean -> 1
	Boolean -> 0
	Double -> 102.543
	Boolean -> 1


The problem is fixed if I declare the strings separately and pass them by reference

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	string one = "Hello";
	string two = "something";
	string three = "empty";
	string four = "done";


	sapiens[0].add(1);
	sapiens[1].add(2.00);
	sapiens[2].add(one);
	sapiens[3].add(true);
	sapiens[4].add(two);
	sapiens[5].add(55);
	sapiens[6].add(three);
	sapiens[7].add(false);
	sapiens[8].add(102.543);
	sapiens[9].add(four);


Gives me desired results:
1
2
3
4
5
6
7
8
9
10
    Integer -> 1
    Double -> 2
    String -> Hello
    Boolean -> 1
    String -> something
    Integer -> 55
    String -> empty
    Boolean -> 0
    Double -> 102.543
    String -> done


Does anybody know why this happens?
I suspect string literal (type is const char* => pointer), will be casted to bool and not std::string.

You could make an extra add() overload for const char*.

Edit : If you post the add() function prototypes, we could probably say more.
Last edited on
ahh I see. Yes here is rest of the code, or the Homo class

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Homo
	{
	    public:
		Homo();
		void add(int x);
		void add(double x);
		void add(string &x);
		void add(bool x);
		void print();	    

	    private:
		int i;
		double d;
		string s;
		bool b;
		int state;
	};

Homo::Homo() : i(0), d(0.0), s(""), b(0), state(0)
{}

void Homo::add(int x)
{
	i = x;
	    state = 1;
}

void Homo::add(double x)
{
	d = x;
	    state = 2;
}

void Homo::add(string &x)
{
	s = x;
	    state = 3;
}

void Homo::add(bool x)
{
	b = x;
	    state = 4;
}

void Homo::print()
{

	switch(state) {
	  case 1:
		cout <<"Integer -> " <<i <<'\n';
		break;
	  case 2:
		cout <<"Double -> " <<d <<'\n';
		break;
	  case 3:
		cout <<"String -> " <<s <<'\n';
		break;
	  case 4:
		cout <<"Boolean -> " <<b <<'\n';
		break;
	}


}
Ohh I see now :
Homo::add(string&); should be Homo::add(const string&);

This way you can pass temporary variables to your function as well.

O_o: 700 ;)
Last edited on
Nice, thank you very much for clearing that up :=)
Topic archived. No new replies allowed.