possible to overload a classname?

Started by
13 comments, last by amemorex 22 years, 4 months ago
Good call on overriding the cast operator, that looked like what the original poster was looking for..

quote:Original post by amemorex
by overloading the int operator, i will have to re-set whatever variable i want to equal the class''s value, each time it changes, which is a pain in the ass..meaning

You''d have to do that for a standard type too. If your intention is making a user type that''s just like an int, this is consistent behavior:
int a = 4;
int b = a; // b = 4;
a = 2; // b still = 4

quote:
i''d have to do that every time nr changes, which would be a major pain in the butt, when i would like to just use "nr" by itself whenever needed, y''know?

Then what you''re talking about is a reference. I have no idea how to overload for references, it''s something that maybe you should try. I''m sure this would work:
MyType blah;
MyType &ref = blah; // from now on, ref acts just like blah & vice versa

But I''m pretty sure this wouldn''t:
int &ref = blah; // where blah is still a MyType

quote:
but im under the impression that this isnt possible, heh
i really wish i knew how the STL programmers went about this (stl string variables, for instance)

Lucky for you STL has almost no object code; everything''s inlined in the headers. Just open the header files and start digging. Sure, the variable names might be scary, but that shouldn''t scare bad-ass coders like ourselves.

quote:
or then again, is it possible in any way to maybe "overload" a normal variable with a class''s functions? im seriously doubting it, but..

No idea what you''re saying here. By variable do you mean "built-in type"?, as in class MyClass : public int? If you do, the answer is no.
Advertisement
I have an idea: just figure out what you want and put that information in your post. The others and I have been doing out best to figure out your cryptic messages and give you solutions but you''re not making much of an effort to clarify your question. I think this quote says it all:

"yea but you guys dont really understand, i want the average user to be able to use this class whenenver and with whatever they feel like.."

If that really is your question then the answer is to put it in a namespace, but that isn''t your question.
anon. ok. i make a class. it has a variable inside it.

whenever i use an instance of that class, i want it to simply return the value of that variable. in any situation.

CLASSNAME num;
num = 5;
cout << num;
cin >> num;
printf("%d", num);
sprintf(...);
MyOwnFunction(..type in num here, but have it really pass ''5'');

the list goes on. i simply want the instance of the class to always return its member variable value, whenever that class is used (except of course when using the class''s functions, like num.length() ).

that clarify it up for you?
quote:Original post by amemorex
by overloading the int operator, i will have to re-set whatever variable i want to equal the class''s value, each time it changes

What you''re saying doesn''t really make sense to me... the int operator won''t change your instance of your number class, it just allows functions to get an int value from it. So you could use it by itself.

quote:i really wish i knew how the STL programmers went about this (stl string variables, for instance)

I don''t think you understand, because they don''t use strings like that. For instance, you can''t use an std::string in sprintf, you have to use its c_str() function to convert it to a char* first. And that''s what I would do for your number class, probably: add a ToInt() function that returns an integer. I would prefer this over adding an int operator as it makes for more explicit code. The only time I would prefer it with the operator is where I had to transparently convert existing code from using integers to my Number class.
yes that does clarify it. You''ll also notice that I''ve already answered your question long ago, mine is the fourth response. Then several other people gave the same response.

This topic is closed to new replies.

Advertisement