Jump to content

  • Log In with Google      Sign In   
  • Create Account

Can you overload the assignment operator to take multiple values? [FIXED]

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Akashi   Members   -  Reputation: 268


Posted 14 June 2013 - 05:42 PM

This is something I've been curious about. Say with certain structs/classes, you can define them while declaring them by assigning them to multiple values contained within curly braces. For instance, with the COORD struct:


COORD position = {0, 0, 0};


How do you do this in code? Or is this some strange special case? I can't find anything on the subject.


EDIT: I really should code before I speak. I'm pretty sure you can just define all the variables once you declare a struct, and it'll go down the list of the struct's variables, so if I were to have a similar struct, let's say Vector, with X, Y, and Z values, you would be able to declare and define a variable of its type with Vector = {0,0,0}; This doesn't work for assignment though, for some reason. Is there a way to do that?

Edited by StoneMask, 14 June 2013 - 05:58 PM.


#2 Cornstalks   Crossbones+   -  Reputation: 6966


Posted 14 June 2013 - 05:49 PM

If you're got access to C++11's initializer lists, there's no magic required. This functionality is now built into the language.


Also, that code doesn't call the assignment operator. Initialization upon creation is done not with the assignment operator, but with the constructor (and subsequently the copy constructor, though to extra copy is typically elided).


Long story short, that code you wrote will get handled as if it were written:


COORD position(COORD({0, 0, 0})); // operator= is never used

// Note the above copy often is elided, and thus the code becomes "COORD position({0, 0, 0});" (but the copy construct is still required to be available, or else you'll get a compile time error)


And unless you've got C++11's initializer lists, you can't do that.


As for your question, operator= is a binary operator, and cannot take any more (or less) than 2 arguments.

Edited by Cornstalks, 14 June 2013 - 05:53 PM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#3 Akashi   Members   -  Reputation: 268


Posted 14 June 2013 - 05:58 PM

Alright, that makes sense, thanks! Thank you for your time.

#4 Servant of the Lord   Crossbones+   -  Reputation: 16693


Posted 15 June 2013 - 03:28 PM

Semi-relevantly, here's a function I use in my code base for formatting strings.

	Creates a string from 'str', with every occurance of "%n" replaced with the nth argument.
	Example: String::Format("The time is now %1 on the %3 of %2", {time, month, day})
std::string Format(std::string text, const StringList &arguments)
	StringList symbols = {"%1", "%2", "%3", "%4", "%5", "%6", "%7", "%8", "%9", "%10", "%11", "%12", "%13", "%14", "%15"};
	unsigned int i = 0;
	for(const auto &arg : arguments)
		if(i <= symbols.size())
			text = String::ReplaceAll(text, symbols[i], arg);
	return text;

It takes advantage of C++11's curly-bracket "uniform initialization syntax" to initialize a std::vector<std::string> (typedef'd to StringList since I use them so frequently).

Just a quick and sloppy implementation that works fairly well. It could be optimized further (it's very Schlemiel at the moment), but I haven't needed to yet.


But yes, uniform initialization syntax works for other types:

#include <iostream>
struct Point
     int x = 0; //default value of 0
     int y = 0;
int main()
     Point point = {640, 480};
     std::cout << point.x << ", " << point.y << std::endl;
     return 0;


Try the code


It works, for example, in C++11's new tuple types. C++11 must be enabled on your compiler for you to have access to the new features. How you enable it varies from compiler to compiler, but for GCC/MinGW you pass in -std=c++11 (or -std=c++0x for older versions of GCC).

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.