Results 1 to 8 of 8

Thread: A few C++ noob questions

  1. #1
    Join Date
    Feb 2006
    Location
    London, UK
    Beans
    161
    Distro
    Kubuntu 8.10 Intrepid Ibex

    A few C++ noob questions

    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.

  2. #2
    Join Date
    Apr 2007
    Location
    Buenos Aires - Argentina
    Beans
    43
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: A few C++ noob questions

    Hi, in response to your questions:

    1. Indeed, the function HWNext is returning an 'std::string' object as value.
    2. Yes you can do that, and - by the way - you can do this too:
    Code:
    return "a string";
    Since std::string has a constructor defined which takes a const char * as an argument.

    3. A. The operators '<<' and '>>' cannot have two arguments.
    B. There's no need to declare them as virtual but if you need do so.

    Regards.
    Last edited by DoubleQuadword; April 14th, 2007 at 07:05 PM.
    Free software, free society. - Richard M. Stallman.

  3. #3
    Join Date
    Feb 2006
    Location
    London, UK
    Beans
    161
    Distro
    Kubuntu 8.10 Intrepid Ibex

    Re: A few C++ noob questions

    Thanks for the help. So how should I declare the operator << overload?

    Also, wouldn't there be ambiguity if I removed the virtual keyword and then did
    Code:
    std::cout << InputError ("Error");
    since there are two different functions that the InputError could use - the general Error one and the InputError one?

  4. #4
    Join Date
    Apr 2007
    Location
    Buenos Aires - Argentina
    Beans
    43
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: A few C++ noob questions

    Keeping or removing the virtual keyword would not help at all, the concept of 'virtual' is different. What are you doing is called 'operator overloading', and actually you can have more than one overload for any kind of function (including constructors).

    I recommend you to ensure both declarations contain only one argument.

    I.E:

    Code:
    std::ostream& operator << (std::ostream&);
    Another thing, when you call InputError you're calling the class constructor, and there's no one defined which takes a void */char */const char */, etc; value; therefore, you're providing an InputError object to std::cout, which's not a supported value for the overloaded operator '<<' in cout.

    And finally, if you want to call your operator do this:

    Code:
    InputError ie;
    ie << (object of the type you specified or one that can be casted to it);
    Regards.
    Last edited by DoubleQuadword; April 8th, 2007 at 12:51 AM.
    Free software, free society. - Richard M. Stallman.

  5. #5
    Join Date
    Feb 2006
    Location
    London, UK
    Beans
    161
    Distro
    Kubuntu 8.10 Intrepid Ibex

    Re: A few C++ noob questions

    But what if I want to define how my Error is written on the console, so I can do something like
    Code:
    ostream& operator << (ostream&, Error&) { /* define how to write an Error to stdout */ }
    //....
    try 
    {
    readSomething();
    }
    catch ( Error & err ) 
    {
    cout << err;
    }
    I would have to define operator << including the Error type somewhere, wouldn't I?
    Last edited by khedron; April 8th, 2007 at 12:51 AM.

  6. #6
    Join Date
    Apr 2007
    Location
    Buenos Aires - Argentina
    Beans
    43
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: A few C++ noob questions

    As I said in the previous posts, the declaration regarding the left shift operator ('<<') cannot contain two arguments.

    It does not matter how you declare/define your operator '<<', you're not applying it at all to your 'Error' object. Instead, you're providing an 'Error' object to the std::cout which's not a supported type.

    For example:

    Code:
    class myclass 
    { myclass(const char *); virtual ~myclass(); void operator << (const char *); };
    
    // Now in your main...
    
    myclass m("Hi.");
    m << "Hi there!";
    In this case you're calling the operator '<<' of 'm' which is of the type 'myclass', and you should define in the operator << definition how you want/need that string to be printed.

    Regards.
    Last edited by DoubleQuadword; April 14th, 2007 at 07:06 PM.
    Free software, free society. - Richard M. Stallman.

  7. #7
    Join Date
    Feb 2006
    Location
    London, UK
    Beans
    161
    Distro
    Kubuntu 8.10 Intrepid Ibex

    Re: A few C++ noob questions

    Ok, thanks for the help.

  8. #8
    Join Date
    Jan 2007
    Beans
    34

    Re: A few C++ noob questions

    you don't gotta put std:: in if you write

    using namespace std;

    above main

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •