Sign in to follow this  
suliman

copy classes? default parameters?

Recommended Posts

suliman    1652
Hi Two questions: 1. I have a simple int2 class to hold x and y and wanna be able to compere two of them to see if they are equal. Any built-in class stuff (which might be fatser) i can use intead of:
bool int2::same(int2 & compare){
if(x==compare.x && y==compare.y)
	return true;
else
	return false;
}

2. Can i leave a earlier (more to the left) param as default.
void myFunc(int one=100, int two=200);

//at call
myfunc(useDefault,45); //i wanna do something like this, possible?

3. any shortcut on this forum for [/source] and such? thanks Erik

Share this post


Link to post
Share on other sites
Aardvajk    13207
1) There isn't a built-in equality for user classes but you can define an operator== function that lets you use the same syntax as for built in types:


class int2
{
public:
int2(int a=0,int b=0) : x(a),y(b) { }

bool operator==(const int2 &i) const { return(x==i.x && y==i.y); }

int x,y;
};


Note the use of const both for the parameter and for the method itself. These allow you to use this in more flexible situations.


void f()
{
int2 a(1,2),b(3,4);

if(a==b) blah;
if(a==int2(10,20)) blah;
}


2) No. Only the rightmost parameters can be defaulted in C++. You can, however, create muliple overloads of a function that will do what you want, provided the paramters are of different types:


void f(int a=10,const std::string &s="default")
{
std::cout << a << " " << s << std::endl;
}

void f(const std::string &s)
{
f(10,s);
}

void x()
{
f(); // prints "10 default"
f(18); // prints "18 default"
f("hello"); prints "10 hello"
f(26,"arglebargle"); // prints "26 arglebargle"
}


Not a perfect solution to your problem, but gets around a lot of situations.

[Edited by - EasilyConfused on December 13, 2006 10:59:29 AM]

Share this post


Link to post
Share on other sites
rip-off    10976
1) You can overloading operators.

You can give your class "int2" an equality operator like so.

Google "c++ operator overloading" for more info.

bool operator==( const int2 &one, const int2 &two )
{
return ( one.x == two.x) && (one.y == two.y);
}




2) No, the default parameters cannot come before a non-default one.

3) Not as far as I'm aware.

Share this post


Link to post
Share on other sites
Morrandir    304
Hi!

1. I would say comparing like that is fine. I would overload the '==' operator for this purpose, as it would probably result in more comprehensive code when used.

2. You can only use default values for the rightmost parameters, as there would be no way (in C++) to tell the compiler which values are omitted otherwise.

3. I don't find typing down [_source] too tiring, so I didn't search for shortcuts, so I can't help you with that.

Cheers

Share this post


Link to post
Share on other sites
Aardvajk    13207
Quote:
Original post by Morrandir
@EasilyConfused: you have a typo there, careful, it's 'operator==' you want not 'operator='


Oops. Thanks Morrandir. Edited. Goddamn substandard work keyboards [smile].

Share this post


Link to post
Share on other sites
rip-off    10976
Quote:
Original post by EasilyConfused
Quote:
Original post by Morrandir
@EasilyConfused: you have a typo there, careful, it's 'operator==' you want not 'operator='


Oops. Thanks Morrandir. Edited. Goddamn substandard work keyboards [smile].


It should probably also return a bool, but thats just being pedantic [grin]

Share this post


Link to post
Share on other sites
Aardvajk    13207
Quote:
Original post by rip-off
Quote:
Original post by EasilyConfused
Quote:
Original post by Morrandir
@EasilyConfused: you have a typo there, careful, it's 'operator==' you want not 'operator='


Oops. Thanks Morrandir. Edited. Goddamn substandard work keyboards [smile].


It should probably also return a bool, but thats just being pedantic [grin]


Only if you define pedantic as turning nonsense code that will not compile into actual code that will compile [grin]. I guess I'm not going to convince you all that that one was a typo.

Edited. Thanks rip-off. Man, I'm on fire today. I think I need to sit quietly in the corner and rock for a while.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this