Archived

This topic is now archived and is closed to further replies.

amemorex

possible to overload a classname?

Recommended Posts

i have a class with an integer in it, setup something like..
  
class Num {
public:
    void Num::operator = (int num);
    int a;
};

void Num::operator = (int num) {
    a = num;
} 

void main() {
    Num number;
    number = 5;
}
  
that works fine and dandy..but now i''d like the instances of my class to be used like this:
  
    int x = number + 5;
    sprintf(buffer, "%d", number);
    cout << number << endl;
  
kind of like that..basically i want the name of the class to return the value of "a" whenever its used like the above.. so how would i do this?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Your post is pretty vague. What are you trying to do, or what is the purpose of this class? From what you have I think you are looking for a conversion operator. Num::operator int() {return a;} note: conversion operators do not specify a return type, but they don''t need to because it is obvious what they return (this one returns an int). Conversion operators are kind of the inverse of implicit constructors.

Share this post


Link to post
Share on other sites
You wont be able to do so...

If you want to do math and printing, you will have to overlaod the operators you want. For printf you will have to create a get method which retuns a pointer to char(not the best way) or pass a pointer to the get method and then print the variable... It is usually not good idea to mix c and c++ together, the only reason why you can do so is because the compilers lets you, but some times you can get weird results....

Share this post


Link to post
Share on other sites
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 i make an STL string for instance, that string is a class (which i assume because it has functions/operators?), and you can use it anywhere..in a cout..a printf..an sprintf..i highly doubt the coders put an overload for every single absolute possible thing you can use the variable in..

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

Add two lines to your class declaration:
operator int() const { return a; }
int operator()() const { return a; }


The first will handle most cases. Now you can do this:

int x = number + 5;
cout << number << endl;

However, with printf and sprintf, etc, because they are
variable argument list functions, your compiler might not perform the conversion properly (MSVC gives a warning about
it), in which can you can do this:

sprintf( buffer, "%d", number() );

Share this post


Link to post
Share on other sites
I came across the same problem one day. My solution involved overloading the int() operator as in the following example:

  
#include "stdafx.h"
#include "iostream.h"

struct Number {
int value;
void operator=(const int valparam) {
this->value = valparam;
printf("Overloaded operator = used.\n");
}
operator unsigned int () const {
printf("Overloaded operator int() used.\n");
return value;
}
};

void PrintNumber(int number) {
printf("PrintNumber:\t\t%i\n", number);
}

int main(int argc, char* argv[]) {
Number nr;
nr = 31;
printf("nr address:\t\t%X\n", &nr);
printf("nr.value addres:\t%X\n", &nr);

int temp = 3;
temp = nr; // overloaded op int()


printf("temp:\t\t\t%i\n", temp);
printf("nr:\t\t\t%i\n", nr);
PrintNumber(nr); // overloaded op int()

cout << "cout:\t\t\t" << nr << endl << endl; // overloaded op int()


return 0;
}


As you can see the overloaded operator int() gets called most of the time and the results are correct. The only exception is printf but that''s probably because the argument list is pass-by-reference (is that what you call it?). But as the addresses of nr and nr.value are the same this doesn''t cause any problems. Except when you want to execute some code in the overloaded operator int(). Hope this helps. Good luck,

Jasper

BTW What are you using this for? (Just curious)

Share this post


Link to post
Share on other sites
thanks guys, but a question/concern..

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

    
int temp = 3;
temp = nr;


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?

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)

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

Edited by - amemorex on December 18, 2001 6:57:30 PM

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites