Hi everyone,
I am just learning C++. I have learnt C already, so this has not been too much of a shock to me.
However, I have a few questions that don't seem to be answered from looking at cplusplus.com or the tutorials I have found on the internet.
1. When I return a complex object from a function (i.e. not just an int, char or float) is it passed by value? I.e. could I write
Code:
#include <iostream>
std::string HWText ()
{
std::string s ("Hello World!");
return s;
}
int main (void)
{
std::cout << HWText() <<endl;
return 0;
}
This should print the Hello World text and exit. I'm not on my dev machine atm, but I seem to remember getting reference to local variable warnings when I used such a construct. Maybe it's just because I declared my function as std::string& (I know this would be wrong), but I'd like to be sure on such an important concept.
2. Is there a way of quickly defining a nameless object? For example, would the function below be equivalent to the one shown in question 1?
Code:
std::string HWText ()
{
return std::string ("Hello World!");
}
When I type this (not for a string actually, but for my own class) I get an error. Again, I'm not on my dev machine atm, so if you need to know the actual error and code, please ask.
3. If I have a derivative of a class, how can I declare this << operator overload for both of them? Could I do something like
Code:
class Error;
class InputError : public Error;
virtual std::ostream& operator << ( std::ostream&, Error& );
std::ostream& operator << ( std::ostream&, InputError& );
Thanks for your time.
Bookmarks