possible to overload a classname?

Started by
13 comments, last by amemorex 22 years, 4 months ago
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?
Advertisement
don''t quote me, but i believe you should overload the "<<" operator...

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
that wouldnt solve nearly any of my problems, except when cout''ing it..i want to be able to use it anywhere, in printf''s, sprintf''s, adding/subtracting/etc...just like STL classes
i asked the same question to bjarne stroustrup, he said to return an ostream&, i''m assuming that works with all of the C++ STL but not the C standard library
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.
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....
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..

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

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

This topic is closed to new replies.

Advertisement