• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 505
  • Last Modified:

Playing around with overloading output operators. Help!!

I am messing around with this book I got from Borders. Now that I got my person class working I separated the first and last names. Now I want to overload the output to have the first and last name into just PersonFull what I have been reading in the book is hard to follow since it is showing me numbers and not the same type of class I have. This is what I have gathered so far and got 2 errors which are:

.\main.cpp(13) : error C2039: 'Add' : is not a member of 'person'
        c:\documents and settings\john\my documents\visual studio 2005\projects\week 6 assignment\week 6 assignment\persontype.h(4) : see declaration of 'person'
.\main.cpp(15) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'person' (or there is no acceptable conversion)
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(650): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(697): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(735): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(782): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(906): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(913): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(920): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(927): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(168): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits> &(__cdecl *)(std::basic_ostream<_Elem,_Traits> &))'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(174): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(181): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(188): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(208): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(short)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(241): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(261): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(int)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(286): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned int)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(306): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(326): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned long)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(347): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__int64)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(367): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned __int64)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(388): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(float)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(408): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(double)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(428): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long double)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(448): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(468): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        while trying to match the argument list '(std::ostream, person)'

Here is my main code:

#include <iostream>
#include <string>
#include "persontype.h"

using namespace std;

int main()
{

      person PersonLast, PersonFirst, PersonFull;
      PersonLast.setNameLast("Ann");
      PersonFirst.setNameFirst("Heidi");
      PersonFull = PersonFirst.Add(PersonLast);
      cout << "The persons first and last name is: ";
      cout << PersonFull << endl;

      return 0;
}
0
jschmuff
Asked:
jschmuff
  • 26
  • 21
  • 10
  • +1
3 Solutions
 
Infinity08Commented:
>>      PersonFull = PersonFirst.Add(PersonLast);

There is no Add method in the person class that takes a person as parameter and returns a person.


>>       cout << PersonFull << endl;

You did not provide an overloaded operator<<
0
 
evilrixSenior Software Engineer (Avast)Commented:
You need to provide an overloaded streaming operator. Something like: -


std::ostream & operator << (std::ostream & os, person const & psn)
{
    std::cout << psn.GetNameLast();
    std::cout << psn.GetNameFirst();
    return os;
}

Open in new window

0
 
jschmuffAuthor Commented:
I have looked at the tutorials and the book I guess I dont know what was original and what was overloaded so I am not sure I guess what I am supposed to add?? Any suggestions.

Here is my persontype.h code:

#include <string>

class person
{
public:
      std::string getNameLast();
      std::string getNameFirst();
      void setNameLast(std::string nameLast);
      void setNameFirst(std::string nameFirst);
      void introduceFirst();
      void introduceLast();
private:
      std::string itsNameLast;
      std::string itsNameFirst;
};

Here is my persontype.cpp code:

#include <iostream>
#include <string>
#include "persontype.h"

using namespace std;

string person::getNameLast()
{
      return itsNameLast;
}

void person::setNameLast(string nameLast)
{
      itsNameLast = nameLast;
}

void person::introduceLast()
{
      cout << itsNameLast;
}

string person::getNameFirst()
{
      return itsNameFirst;
}

void person::setNameFirst(string nameFirst)
{
      itsNameFirst = nameFirst;
}

void person::introduceFirst()
{
      cout << itsNameFirst;
}
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jkrCommented:
Add a

class person {

public:

ostream& operator<<(ostream& ros) { os << getName(); return os;}

};

to your class to add support for '<<'.

0
 
evilrixSenior Software Engineer (Avast)Commented:
BTW: You'll need to ensure GetNameLast() and GetNameFirst have const qualifiers on the functions, else you won't be able to pass person into the operator << as a const reference (so you'll need to remove the const qualifer on the psn parammeter).
Person
{
     std::string const & GetNameFirst() const // cosnt qualified function
     {
          return m_first;
     }
};

Open in new window

0
 
jschmuffAuthor Commented:
evilrx>>

does that go in my persontype.cpp file??
0
 
evilrixSenior Software Engineer (Avast)Commented:
the operator <<() ? Yes, it is a namespace level function. It goes in you .cpp file and the declaration goes into the header file...

.hpp
std::ostream & operator << (std::ostream & os, person const & psn);


.cpp

std::ostream & operator << (std::ostream & os, person const & psn)
{
    std::cout << psn.GetNameLast();
    std::cout << psn.GetNameFirst();
    return os;
}

   


persontype.hpp
std::ostream & operator << (std::ostream & os, person const & psn);
 
persontype.cpp
std::ostream & operator << (std::ostream & os, person const & psn)
{
    std::cout << psn.GetNameLast();
    std::cout << psn.GetNameFirst();
    return os;
}

Open in new window

0
 
jkrCommented:
Well, then that is

class person
{
public:
      std::string getNameLast();
      std::string getNameFirst();
      void setNameLast(std::string nameLast);
      void setNameFirst(std::string nameFirst);
      void introduceFirst();
      void introduceLast();
      std::ostream& operator<<(std::ostream& ros) { ros << getNameFirst() << " " << getNameLast(); return ros;}
private:
      std::string itsNameLast;
      std::string itsNameFirst;
};


But, where's 'Add()' defined?
0
 
Infinity08Commented:
As I said : you don't have an Add method, so you'll need to add it if you want to use it.

For the operator<<, do something like this :

std::ostream &operator<<(std::ostream &os, const person &p) {
    os << p.itsNameFirst << " " << p.itsNameLast;
}

and add it as friend to the person class :

class person
{
public:
      std::string getNameLast();
      std::string getNameFirst();
      void setNameLast(std::string nameLast);
      void setNameFirst(std::string nameFirst);
      void introduceFirst();
      void introduceLast();

      friend std::ostream &operator<<(std::ostream &os, const person &p);

private:
      std::string itsNameLast;
      std::string itsNameFirst;
};
0
 
jschmuffAuthor Commented:
Let me re-phrase that does that get added to person class or do I create a new class for it.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> add it as friend to the person class
You don't need to do that as the class has accessors! Choose your friends carefully! :)
0
 
Infinity08Commented:
>> Let me re-phrase that does that get added to person class or do I create a new class for it.

Who are you asking ?
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> Let me re-phrase that does that get added to person class or do I create a new class for it.
The operator is not a member of the class, it is a free standing function. It takes the stream as it's 1st parameter and the persona class as it's 2nd parameter.
0
 
jschmuffAuthor Commented:
ok so how do I avoid from using the add just use PersonFull = PersonFirst + PersonLast; ?
0
 
Infinity08Commented:
>> You don't need to do that as the class has accessors! Choose your friends carefully! :)

An operator<< for a class is closely related to the class, and can be safely declared a friend imo. Especially if there's a chance that it will have to output private data (without accessor) at some point.

But it's a personal choice I guess.
0
 
evilrixSenior Software Engineer (Avast)Commented:
Working example...
#include <string>
#include <iostream>
 
// HPP FILE
struct foo
{
	std::string get() const { return "hi"; }
};
 
// CPP FILE
 
std::ostream& operator<<(std::ostream & os, foo const & f)
{
	os << f.get();
	return os;
}
 
 
int main ()
{
	foo f;
	std::cout << f;
}

Open in new window

0
 
jkrCommented:
Are you thinking along the lines of

#include <string>
#include <iostream>

class person;

std::ostream& operator<<(std::ostream& ros, person& rp);


class person
{

friend std::ostream& operator<<(std::ostream& ros, person& rp);
public:
      std::string getNameLast();
      std::string getNameFirst();
      void setNameLast(std::string nameLast);
      void setNameFirst(std::string nameFirst);
      void introduceFirst();
      void introduceLast();

private:
      std::string itsNameLast;
      std::string itsNameFirst;
};


#include <iostream>
#include <string>
#include "persontype.h"

using namespace std;

std::ostream& operator<<(std::ostream& ros, person& rp) {

  ros << rp.getNameFirst() << " " << rp.getNameLast();
  return ros;
}

string person::getNameLast()
{
      return itsNameLast;
}

void person::setNameLast(string nameLast)
{
      itsNameLast = nameLast;
}

void person::introduceLast()
{
      cout << itsNameLast;
}

string person::getNameFirst()
{
      return itsNameFirst;
}

void person::setNameFirst(string nameFirst)
{
      itsNameFirst = nameFirst;
}

void person::introduceFirst()
{
      cout << itsNameFirst;
}


#include <iostream>
#include <string>
#include "persontype.h"

using namespace std;


int main()
{

      person PersonFull;
      PersonFull.setNameLast("Ann");
      PersonFull.setNameFirst("Heidi");

      cout << "The persons first and last name is: ";
      cout << PersonFull << endl;

      return 0;
}

?
0
 
Infinity08Commented:
>> ok so how do I avoid from using the add just use PersonFull = PersonFirst + PersonLast; ?

What is the Add method supposed to do ? Add a person to a person ???? What does that mean ?
0
 
jschmuffAuthor Commented:
I want to add the first name with the last name to print out one name using PersonFull
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> An operator<< for a class is closely related to the class, and can be safely declared a friend imo.
But, it doesn't need to be here.

>> Especially if there's a chance that it will have to output private data (without accessor) at some point.
YAGNI -- add it when you need it!

>> But it's a personal choice I guess.
I guess, but it's best to use friend only when needed.
0
 
Infinity08Commented:
>> I want to add the first name with the last name to print out one name using PersonFull

?? There's no need ... Just do :

      person PersonFull;
      PersonFull.setNameLast("Ann");
      PersonFull.setNameFirst("Heidi");
0
 
jkrCommented:
As in the (*cough* compiling!)  full example above...
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> As in the (*cough* compiling!)  full example above
I notice it took you 2 attempts to get the operator <<() correct though :o)
0
 
jschmuffAuthor Commented:
This whole overloading thing makes me feel overloaded... lol, anyways, so in my case for code since my book only gives me numbers as the tutorial and the online tutorial I found is in numbers too, which is the best way to overload this output operator? I am having a hard time relating there information to my code.
0
 
Infinity08Commented:
>> I guess, but it's best to use friend only when needed.

Well, I really don't see the issue. Making it a friend will make the code more readable (no need to use accessors), it will make the code faster (no need to copy strings), and makes more sense in most cases (since you usually don't need/have public accessors for every piece of data you want to output).

I see an operator<< as part of the class (even though it's defined outside of it), and treat it that way, so it's almost always a friend for me ... ;)
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> which is the best way to overload this output operator
You've only been shown 1 way.
0
 
Infinity08Commented:
>> >> which is the best way to overload this output operator
>> You've only been shown 1 way.

Albeit by 3 different persons lol
0
 
jschmuffAuthor Commented:
so my main code just use PersonFull. for both and that would be the output so the actual overloading of the output operator is in the class then correct? holy this is confusing.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> Well, I really don't see the issue.
There isn't one, I'm just saying on this ocassion it is not necessary!

>> Making it a friend will make the code more readable (no need to use accessors)
Eh? I don't agree!

>> it will make the code faster (no need to copy strings)
Accessors return members -- by const reference if the code is written properly!

>> and makes more sense in most cases
Some cases -- in this case it doesn't as it's just not necessary!

>> since you usually don't need/have public accessors for every piece of data you want to output
But in this case it does :-p

>> I see an operator<< as part of the class (even though it's defined outside of it), and treat it that way, so it's almost always a friend for me ... ;)
Yes, I do agree but it's still not a class member so don't allow it access unless absolutely necessary -- encapsulation!

Anyway, we could debate this all night but I'm still having nightmares from the last time you and Alex ganged up on me, so let's just agree to disagree :-p
   

0
 
jschmuffAuthor Commented:
ok so now I am getting a error that says:

c:\documents and settings\john\my documents\visual studio 2005\projects\week 6 assignment\week 6 assignment\persontype.cpp(40) : error C4716: 'operator<<' : must return a value
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> so my main code just use PersonFull. for both and that would be the output so the actual overloading of the output operator is in the class then correct?
No... it is not int he class -- it cannot be a member of the class, it is (for the 3rd time) a free standing function!

>> holy this is confusing.
Nah, simple :-p
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> c:\documents and settings\john\my documents\visual studio 2005\projects\week 6 assignment\week 6 assignment\persontype.cpp(40) : error C4716: 'operator<<' : must return a value

Return the stream...
std::ostream& operator<<(std::ostream & os, foo const & f)
{
	os << f.get();
	return os;
}

Open in new window

0
 
Infinity08Commented:
>> so my main code just use PersonFull. for both and that would be the output so the actual overloading of the output operator is in the class then correct? holy this is confusing.

You're confusing yourself.


"person" is the class and describes a person. Every person has a first and last name.
You then make an instance of that class named PersonFull with first and last name Heidi Ann.
You could make another instance of the class named PersonOther with first and last name John Doe.

The operator<< that is defined is just there to show the name of a person object. So, all it does is print the first and last name of the person in question.
0
 
Infinity08Commented:
>> Anyway, we could debate this all night but I'm still having nightmares from the last time you and Alex ganged up on me, so let's just agree to disagree :-p

By now you should know that I love to debate ;)


Just one point : since you seem to refer to "in this case" a lot, allow me to do the same :

>> >> it will make the code faster (no need to copy strings)
>> Accessors return members -- by const reference if the code is written properly!

in this case it was a copy that was returned ;) So in this case, making it a friend would make it faster hehe.
0
 
jschmuffAuthor Commented:
I don't quite understand all of this but then again I just tried to tackle it.. lol. so I added return os;
I compiled the code and everything appears to be working. Thank you all for your help.
0
 
evilrixSenior Software Engineer (Avast)Commented:
std::ostream& operator<<(std::ostream & os, person const & psn) is NOT part of the person class... it is just a function that takes a stream and a person class and then uses accessors (or if it is declared a friend members) to stream out to std::cout the contents of the class. It is a proxy for the real std::cout streaming operator.

The reason it can't be a class member is because the person object must be an rvalue and the stream an lvalue for the << operator. It could be a member of the stream class but since you don't own the code for this you can't modify that so the way you do it is to write it as a free standing namespace level function with the stream as the lhs param and the person object as the rhs param. When you stream the person class ADL (Argument Dependent Lookup) -- http://en.wikipedia.org/wiki/Argument_dependent_name_lookup -- figures out what operator <<() to use to stream the person object. You just need to ensure the operator is defined in the same namespace as the person object for this to work properly.
0
 
jschmuffAuthor Commented:
You guys are awesome. I love this site.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> in this case it was a copy that was returned ;) So in this case, making it a friend would make it faster hehe.
I qualified my assertion with, "if the code is written properly" -- so I still win :-p
0
 
evilrixSenior Software Engineer (Avast)Commented:
And so we move on... :)
0
 
Infinity08Commented:
>> I qualified my assertion with, "if the code is written properly" -- so I still win :-p

I guess you missed the irony, or chose to ignore it lol ... either way, I win because I had the last word ... ;) And if you post after this, then I still win, since my suggestion runs faster "in this case" ... j/k

ps. : j/k means "just kidding" ... You'll see me use it often :)
0
 
evilrixSenior Software Engineer (Avast)Commented:
>>  guess you missed the irony
Ignored in favour of being victorious :-p

ps. :-p means I'm poking fun at you :-)

0
 
evilrixSenior Software Engineer (Avast)Commented:
You're from Belgium right? The place made famous by Douglas Adams and Zaphod Beeblebrox!
0
 
Infinity08Commented:
>> You're from Belgium right? The place made famous by Douglas Adams and Zaphod Beeblebrox!

Oh it's famous for way more than that ... The inventor of the saxophone was a Belgian (Adolphe Sax). Jean-Claude Van Damme is a Belgian, as was Audrey Hepburn. Hercule Poirot (from the Agatha Christie books) was a (fictional) Belgian detective. The famous cartographer Mercator was Belgian, as well as painters like Jan van Eyk, René Magritte, Pieter Breughel, and lots more.

And if you ever heard of Tintin ... its writer (Hergé) is Belgian too :)

Oh, and the man who did this :

        http://www.youtube.com/watch?v=W2Hcxt_HNZg

was ... guess what ... Belgian !! lol


Just the ones I can think of off the top of my head heh.
0
 
jkrCommented:
0
 
evilrixSenior Software Engineer (Avast)Commented:
I am not Belgium. :)

>> it will make the code faster (no need to copy strings)
I forgot to mention that RVO (Return value Optimization) will almost definitely prevent this too. Try it yourself...

#include <iostream>
#include <string>

struct A
{
      A()
      {
            std::cout << "A" << std::endl;
      }
};

struct B
{
      A get() { return a; }
      A const &getref() { return a; }
      A a;
};

int main()
{
      B b;
      b.get();
      b.getref();

      return 0;
}

... if you compiler supports RVO you'll only see one 'A' output
0
 
Infinity08Commented:
Ah yes, Westvleteren ... If I'm not mistaken, it has been chosen as the best beer in the world a few years ago. I can't blame them ... it's a truly good beer.

You probably won't find it abroad though, as you can only buy it in the Westvleteren abbey, and you can only buy one case of beer (24 bottles) at a time.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> best beer in the world
/me prefers Cider.
0
 
Infinity08Commented:
>> ... if you compiler supports RVO you'll only see one 'A' output

That's not surprising since you don't use the return value at all ;) But yes, it might indeed optimize it in the case of the operator<<
0
 
Infinity08Commented:
>> >> best beer in the world
>> /me prefers Cider.

That's not a beer heh ... it's apple wine ;)
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> That's not surprising since you don't use the return value at all ;) But yes, it might indeed optimize it in the case of the operator<<
I makes no odds, a temporary is still returned! RVO and NRVO (Named Return value Optimization) are supported by most industrial grade compilers!

http://msdn2.microsoft.com/en-us/library/ms364057(vs.80).aspx
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> That's not a beer heh ... it's apple wine ;)
Beer, wine, cider -- it's all the same to me -- get's me drunk and that's a good thing :)
0
 
Infinity08Commented:
>> I makes no odds, a temporary is still returned!

What I meant is that if you don't use the return value, then the compiler has more options to optimize the return value away. Anyway, as I said, it'll probably indeed be optimized in case of the operator<< too.


>> get's me drunk and that's a good thing :)

And it's weekend !!
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> Anyway, as I said, it'll probably indeed be optimized
Probably? Tsk! It still doesn't change the fact that it should be a const & return :)

>> And it's weekend !!
And I am just starting to develop the flu that my wife is just getting over -- I am be OOA from EE for a few days :(
0
 
Infinity08Commented:
>> It still doesn't change the fact that it should be a const & return :)

Hey, tell that to jschmuff ;)


>> And I am just starting to develop the flu

Hey ... stay away from me then !! :)
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> Hey ... stay away from me then
I think the distance between Belgium and the UK means you are probably safe. I don't think it's going to be like "28 Weeks Later"!
0
 
Infinity08Commented:
The mad cows disease made it across the channel ... lol
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> The mad cows disease made it across the channel
I am not a bloody mad cow (that's my wife)!
0
 
Infinity08Commented:
I think somebody's gonna sleep on the couch tonight heh.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> I think somebody's gonna sleep on the couch tonight heh
Damn right, she's coughing and spluttering everywhere! :)
0
 
Infinity08Commented:
lol ... well time for me to head to bed ... Good night !
0
 
evilrixSenior Software Engineer (Avast)Commented:
Night 8 -- sleep well.
0
 
jkrCommented:
>>You probably won't find it abroad though, as you can only buy it in the
>>Westvleteren abbey, and you can only buy one case of beer (24 bottles) at a >>time.

The 1st time I found it was in NYC, too bad it is so hard to get hold of here in Europe (that is, without paying fancy prices - yeah, the 0.3l bottle was ~$13 in the US)
0
 
Infinity08Commented:
>> The 1st time I found it was in NYC

Wow ... must have been before they limited the sales then. Price-wise, we can go get them here for around 1 euro per bottle ;) So, if you want some, make a trip to Belgium lol.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 26
  • 21
  • 10
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now