tags:

views:

118

answers:

1

I don't understand why the following fails:

#include<string>
class  Foo
{
  public:
     std::string** GetStr(){return str;}    

 private:
   std::string * str[10];
};

Thanks

+1  A: 

First, you tag this as C++ and C. Which is it? C does not have a string class. If it is C++, please remove the C tag, it is misleading (they are not the same language!).

Edit: I misunderstood what you are trying to do. Your method should compile. You just have to remember to dereference the returned str to get the string.

I rarely deal with double indirection, but you have to do something like this to set the string:

*(*str) = "STR"; //or
*(str[i]) = "STR";

I don't know how you would use the address operator here, because it returns a reference and not a pointer.

to set the string in the str array. Your method is really weird. The problem is that the compiler doesn't know that you want to dereference a string, so it tries to dereference a char*.

I do not understand why you want to do it this way, though. It would be better to do this:

std::string str[10];
std::string* GetStr() { return str; }
Hooked
removed the "C" tag. You can do that w/ rep >= 500.
azheglov
Thank you, sir.
Hooked
I put string* str[] for example purposes, I use different classes in my case
vehomzzz
yes... good point, it is C++ question given string classes... though the rule I ma interested is in both languages..
vehomzzz
Oh, I misunderstood what you are trying to do. Let me edit my post.
Hooked
I don't understand what the problem with the C tag is - even if the sample code uses C++ classes, the issue about the relationship of an array of pointers and pointer-to-pointers (which is what the question seems to be about) is the same in C and C++.
Michael Burr
Yes, but he is programming in C++. He uses std::string, which is not in C, so I don't think a C tag is honest.
Hooked