Jump to content
  • Advertisement

Archived

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

amemorex

possible to overload a classname?

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

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

Share this post


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

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

  • Advertisement
×

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!