Jump to content
• Advertisement

Public Group

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

This topic is 1864 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

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

#### Share this post

##### Share on other sites
Advertisement

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

#### Share this post

##### Share on other sites

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

#### Share this post

##### Share on other sites

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);
}

i++;
}

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).

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5
• Advertisement

• 10
• 14
• 30
• 13
• 11
• ### Forum Statistics

• Total Topics
631782
• Total Posts
3002338
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!