3

I am learning C++, and learned that int-types are just premade classes. So I thought maybe i should try to create one.

What I want to do basically is a normal class of int

int x;
x=7;
cout << x;

// Output is 7 on screen.

so similarly...

abc x;
x=7;
cout << x;

What would I put in

class abc{
\\ HERE!!!!!! 
};

so I could do this

class SomeClass {
public:
    int x;

    SomeClass(int x) {
        this->x = x;
    }
};

int main(int argc, char *argv[]) {
    SomeClass s = 5;

    cout << s.x << "\n"; // 5

    s = 17;

    cout << s.x << "\n"; // 17

    return 0;
}

But as you can see I have to use s.x to print the value - I just want to use 's'. I am doing it as an experiment, I don't want to hear about how this method is good or bad, pointless or revolutionary, or can 't be done. I remember once I did it. But only by copying and pasting code that I didn't fully understand, and have even forgotten about.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Muhammad Umer
  • 17,263
  • 19
  • 97
  • 168
  • 18
    "and learned that int, types, are just premade classes" -- they are not. – Xeo Sep 26 '12 at 20:23
  • 1
    A bit of word salad, though. `int` is a premade type; classes are types which are _not_ premade types. ("premade" as in: you don't need to include a header because the compiler already knows them). – MSalters Sep 27 '12 at 07:26

5 Answers5

6

and learned that int, types, are just premade classes

This is completely false. Still, you have complete control on how your class will behave in expressions, since you can overload (almost) any operator. What you are missing here is the usual operator<< overload that is invoked when you do:

cout<<s;

You can create it like this:

std::ostream & operator<<(std::ostream & os, const SomeClass & Right)
{
    Os<<Right.x;
    return Os;
}

For more information, see the FAQ about operator overloading.

Community
  • 1
  • 1
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
2

the << and >> are basically function names. you need to define them for your class. same with the +, -, * and all the other operators. here is how:

http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html

Ionut Hulub
  • 6,180
  • 5
  • 26
  • 55
2

You need to overload operator<< for your class, like so:

class abc
{
public:
    abc(int x) : m_X(x) {}

private:
    int m_X;

    friend std::ostream& operator<<(std::ostream& stream, const abc& obj);  
};

std::ostream& operator<<(std::ostream& os, const abc& obj)
{
    return os << obj.m_X;
}

You don't have to friend your operator<< overload unless you want to access protected/private members.

David
  • 27,652
  • 18
  • 89
  • 138
2

You must define in your class abc cast operator to int and assignment operator from int, like in this template class:

template <class T>
class TypeWrapper {
public:
  TypeWrapper(const T& value) : value(value) {}
  TypeWrapper() {}
  operator T() const { return value; }
  TypeWrapper& operator (const T& value) { this->value = value; return *this; }
private:
  T value;
};

int main() {
  TypeWrapper<int> x;
  x = 7;
  cout << x << endl; 
}
PiotrNycz
  • 23,099
  • 7
  • 66
  • 112
0

You want to overload the output operator:

std::ostream& operator<< (std::ostream& out, SomeClass const& value) {
    // format value appropriately
    return out;
}
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380