• Advertisement
Sign in to follow this  

c++ magic or ?

This topic is 3610 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How can this work?

struct Vec
{
	float x, y, z;

	Vec(float a, float b, float c)
	{
		x = a;
		y = b;
		z = c;
	}

	void operator *= (float a)
	{
		x = a;
		y = a;
		z = a;
	}
};

int main()
{
	Vec a(0, 0, 0);

	a *= (1.3f, 0.5f, 0.8f, 0.4f);

	cout << a.x << ", " << a.y << ", " << a.z;
}


This seems to be legal, and the result is:
0.4, 0.4, 0.4

Share this post


Link to post
Share on other sites
Advertisement
Because of the comma operator.
This:
a *= (1.3f, 0.5f, 0.8f, 0.4f);
Means:
1.3f; 0.5f; 0.8f; a *= 0.4f;
(More or less)

Share this post


Link to post
Share on other sites
Comma operator. It performs consecutive assignments. Put a std::cout into your *= operator to see what happens.

Not the best idea though.

Share this post


Link to post
Share on other sites
Ahhh, I see. Interesting, so comma is actually a operator. I didn't know that. Thanks! :)

Share this post


Link to post
Share on other sites
its not 0 * 0,4 as in mathematical zero "times" 0,4

it's * operator that defines * as =



void operator *= (float a)
{
x = a;
y = a;
z = a;
}

so what this really do is just setting x,y,z to a,a,a

Share this post


Link to post
Share on other sites
Quote:
Original post by popsoftheyear
??? So why is 0 * 0.4 equal to 0.4 here?

-Scott

Because his custom *= operator doesn't perform multiplication, just assignment.

Share this post


Link to post
Share on other sites

#include < iostream >
#include <vector>
#include <map>
#include <boost/assign/std/vector.hpp>
#include <boost/assert.hpp>

using namespace std;
using namespace boost::assign;

int main() {
vector<int> v;
v += 1,2,3,4,5,6,7,8,9;

map<string,int> m;
insert( m )
( "Bar", 1 )
( "Foo", 2 )
;

std::cout << "This shit is crazy\t" << v[4] << "\t" << m["Bar"] << "\n";
}





Now that's some crazy code. Yes, "," is an operator. So is "()()"

Share this post


Link to post
Share on other sites
Quote:
Original post by Kelly G
Have you been reading this?


No, what I pasted was straight from one of the Boost examples. Boost is considered the C++ gold standard.

The solution to writing unmaintainable code and ensure a job for life is simply to use C++ ;)

Share this post


Link to post
Share on other sites
Boost likes extending the language to see where it can go. :)

In that case, we have operator() returning an inserter object that accepts operator(), which returns itself (or another inserter object).

The += trick is new -- how did they do that?

Share this post


Link to post
Share on other sites
You probably should return a Vec& from the *= operator to keep it consistent with the base types' counterparts.

Share this post


Link to post
Share on other sites
Quote:
Original post by NotAYakk
The += trick is new -- how did they do that?


boost::assign creates an overload for operator += with a vector on the left hand side that returns an object that has the comma operator overloaded. You can look at the actual header for the details.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement