PDA

View Full Version : object returning const char* to QStrng



bajarangi
4th April 2009, 01:27
Hi all,

Sorry if this is n00b question.
I'm working on some base classes now, and have a small class
that prints to a QString.
In a higher level class, I want to put that into a QString, but the
compiler complains no matter what I try.

Here's a simplified example

class Dog
{
private:
int age;
public:
Dog(int age);
QString print(); // return dog age in QString
};

class ManyDogs
{
private:
Dog1;
Dog2;
public:
QString printdogs();
}

later:

ManyDogs::printdogs()
{
QString temp;
temp = Dog1.print();
}

That last line won't compile with an error:
"passing const Dog as 'this' argument of 'QString Dog.print()' discards qualifiers.

And I tried returning "const char *" from print() also, with the same error.

But similar code such as:
printf("Dog age is %s\n". Dog1.print());
works fine. Argh!

Obviously something in the C++ object model I'm not getting.
thanks in advance for any help.

-Don

wysota
4th April 2009, 01:38
You should use the const qualifiers in your methods.


class Dog {
//...
QString print() const;
//...
};

etc.

bajarangi
4th April 2009, 15:05
Thanks! That worked great.:)

<humbled again>

-Don