Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Pure OOP Sepples

Name: Anonymous 2011-12-25 21:23

Basically, trying to define a set of types to replace the standard int, char, float, etc... to make C++ more like Ruby. This is what I have so far... not sure what to name a namespace other than perhaps POOP (Pure Object Oriented Programming... obviously very unprofessional), so I haven't tossed them in a namespace yet.

#include <stdint.h>
#include <string>
#include <sstream>

class Int_Obj {
        int32_t Data;
        Int_Obj(int32_t argument) { Data = argument; }
public:

        Int_Obj() {}
   
        Int_Obj operator + (int32_t arg) { return Int_Obj(arg + Data); }
        Int_Obj operator + (Int_Obj arg) { return Int_Obj(arg.Data + Data); }
        Int_Obj operator - (int32_t arg) { return Int_Obj(Data - arg); }
        Int_Obj operator - (Int_Obj arg) { return Int_Obj(Data - arg.Data); }
   
        void operator =  (int32_t arg) { Data = arg; }
        void operator =  (Int_Obj arg) { Data = arg.Data; }
        void operator += (int32_t arg) { Data += arg; }
        void operator += (Int_Obj arg) { Data += arg.Data; }
        void operator -= (int32_t arg) { Data -= arg; }
        void operator -= (Int_Obj arg) { Data -= arg.Data; }
        void operator ++ () { Data++; }
        void operator -- () { Data--; }

        std::string to_s() {
                std::ostringstream out;
                out << Data;
                return out.str();
        }
};


Anyways, any suggestions on what else I might need to add, or how to implement things like Ruby's arrays (can store any number of any types of objects, and are safe from memory leaks)... obviously void pointers aren't exactly an option.

Name: Anonymous 2011-12-25 21:25

You did something wrong:
class

Name: Anonymous 2011-12-25 21:27

Wait... I should probably make that class a template so I don't have to rewrite a lot of shit.

Name: Anonymous 2011-12-25 21:33

>>2

Interesting fact: It takes less characters to type out class and public than it does to type out struct and private. Thus, as long as I am using at least one private or protected member, I am better off writing something as a class, so I can shorten the number of characters I have to type writing it up. Otherwise, for an all public data type, struct will be more appropriate.

Name: Anonymous 2011-12-25 22:24

>Pure OOP
POOP

Name: Anonymous 2011-12-25 23:02

>>5

You got a problem with C++ POOP?

Name: Anonymous 2011-12-25 23:32

to make C++ more like Ruby
whywouldyoudothat.jpg

Name: Anonymous 2011-12-25 23:56

>>5
People order our patties

Name: Anonymous 2011-12-26 0:26

Well, this is what I've got so far...

#include <stdint.h>
#include <sstream>
#include <string>

// Pure Object Types
namespace P_OBJ {

    // For standard integer data types.
    template <class T> class p_obj_type {
                T Data;
    public:
                p_obj_type() {}
                p_obj_type(T argument) { Data = argument; }

                p_obj_type operator + (T arg) { return p_obj_type(arg + Data); }
                p_obj_type operator + (p_obj_type arg) { return p_obj_type(arg.Data + Data); }
                p_obj_type operator - (T arg) { return p_obj_type(Data - arg); }
                p_obj_type operator - (p_obj_type arg) { return p_obj_type(Data - arg.Data); }
       
                bool operator <  (T arg) { return Data < arg; }
                bool operator <  (p_obj_type arg) { return Data < arg.Data; }
                bool operator >  (T arg) { return Data > arg; }
                bool operator >  (p_obj_type arg) { return Data > arg.Data; }
                bool operator == (T arg) { return Data == arg; }
                bool operator == (p_obj_type arg) { return Data == arg.Data; }

                void operator =  (T arg) { Data = arg; }
                void operator =  (p_obj_type arg) { Data = arg.Data; }
                void operator += (T arg) { Data += arg; }
                void operator += (p_obj_type arg) { Data += arg.Data; }
                void operator -= (T arg) { Data -= arg; }
                void operator -= (p_obj_type arg) { Data -= arg.Data; }
                void operator ++ ()    { Data++; }
                void operator ++ (int) { Data++; }
                void operator -- ()    { Data--; }
                void operator -- (int) { Data--; }   

                std::string to_s() {
                        std::ostringstream out;
                        out << Data;
                        return out.str();
                }
        };

}; // P_OBJ
   
typedef P_OBJ::p_obj_type<uint64_t> o_uint64;
typedef P_OBJ::p_obj_type<uint32_t> o_uint;
typedef P_OBJ::p_obj_type<uint16_t> o_ushort;
typedef P_OBJ::p_obj_type<int64_t > o_int64;   
typedef P_OBJ::p_obj_type<int32_t > o_int;
typedef P_OBJ::p_obj_type<int16_t > o_short;
typedef P_OBJ::p_obj_type< double > o_double;
typedef P_OBJ::p_obj_type< float  > o_float;

namespace P_OBJ {
        template <class C> class p_char_obj  {
                char Data;
        public:
                p_char_obj() {}
                p_char_obj(C argument) { Data = argument; }

                p_char_obj operator + (C arg) { return p_char_obj(arg + Data); }
                p_char_obj operator + (p_char_obj arg) { return p_char_obj(arg.Data + Data); }
                p_char_obj operator - (C arg) { return p_char_obj(Data - arg); }
                p_char_obj operator - (p_char_obj arg) { return p_char_obj(Data - arg.Data); }
       
                bool operator <  (C arg) { return Data < arg; }
                bool operator <  (p_char_obj arg) { return Data < arg.Data; }
                bool operator >  (C arg) { return Data > arg; }
                bool operator >  (p_char_obj arg) { return Data > arg.Data; }
                bool operator == (C arg) { return Data == arg; }
                bool operator == (p_char_obj arg) { return Data == arg.Data; }

                void operator =  (C arg) { Data = arg; }
                void operator =  (p_char_obj arg) { Data = arg.Data; }
                void operator += (C arg) { Data += arg; }
                void operator += (p_char_obj arg) { Data += arg.Data; }
                void operator -= (C arg) { Data -= arg; }
                void operator -= (p_char_obj arg) { Data -= arg.Data; }
                void operator ++ ()    { Data++; }
                void operator ++ (int) { Data++; }
                void operator -- ()    { Data--; }
                void operator -- (int) { Data--; }   

                o_int to_i() { o_int result = static_cast<int>(Data); return result; }
                std::string to_s() {
                        std::ostringstream out;
                        out << Data;
                        return out.str();
                }
        };
};

typedef P_OBJ::p_char_obj<char> o_char;
typedef P_OBJ::p_char_obj<unsigned char> o_uchar;


Tried doing template specialization for the p_char_obj to make it just a p_obj_type, didn't work out too well. But I guess I like the idea of a char more like an Ada Character than just an 8 bit number

Name: Anonymous 2011-12-26 1:35

>>7

Why the fuck not?

Name: Anonymous 2011-12-26 7:38

Overloaded operator= should return a non-const reference to self.

Name: Anonymous 2011-12-26 7:56

>>10
Let's pile on even more useless bullshit, killing off one of the few good points about the language.

Name: Anonymous 2011-12-26 9:52

>>9
For the love of programming! Read the wiki article on operator overloading!

http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

Assignment operators that don't return a reference to themselves WILL cause problems. You have much to learn, young padawan!

Name: Anonymous 2011-12-26 9:56

>>4
I meant ``Class-based OOP considered harmful''.

Name: Anonymous 2011-12-26 10:56

スマルタルコ

Name: Anonymous 2011-12-26 14:06

>>6
Only that it's shitty.

Name: Anonymous 2011-12-26 16:15

>>1

you should probably create a base class object for all of your objects, and support virtual methods for things like tostring, equality and the like. That way, everything is an object (literally, heheh).


class Object {
public:
  virtual std::string to_s() {
    std::ostringstream out;
    out << "Object:" << (void*)this;
    return out.str();
  }

  virtual bool operator == (const Object& other) const {
    return ((void*)this) == (void*(&other));
  }

  // more virtual methods with reasonable default behavior.
};

Name: Anonymous 2011-12-26 16:46

Just use another language.
C++ (with all it's flaws) is not meant to work with god objects

Name: Anonymous 2011-12-26 17:21

>>18

C++ is meant to work however you want to make it work. That's the entire idea behind making it multiparadigm. Anyways, found a way that sort of works.... should probably use virtuals for a few things though, like >>17 suggested.

>>11
>>13

So... make = return the type of the class using it? Gotcha.

>>14

Any type of OOP you consider NOT harmful?

Name: Anonymous 2011-12-26 17:57

>>19
Trying to do prototype OO (likely the alternative >>14 has in mind) in C++ is not going to work out. C++ isn't a dynamic language and you would have to subvert the object system almost entirely to make it work. It would be a lot like doing OO in C.

Name: Anonymous 2011-12-26 17:58

>>8
lol'd hard

Name: Anonymous 2011-12-26 18:11

>>20

I'm not doing prototype OO though. I'm wrapping all the basic data types in classes and giving them an ass load of methods to make them the C++ equivalent of Ruby Fixnum objects (or, alternatively, do to int, float and char what std::string did to char*). The only thing I can't do is shit like 5.to_s() or Fixnum.each { block_of_code }, because that's beyond the power of C++. Although I wish I could make an equivalent of the proc class, and have it store some function pointer (or anonymous function ala C++11), let it be aware of it's own arity, and have some call (bleh, ...) method.

Name: Anonymous 2011-12-26 18:23

>>22

Yeah, there would be limits to what you could type like "hello".print(); But you could still do String("hello").upperCase().concat(" faggot").concat(" how are you doing today?").print();

Name: Anonymous 2011-12-26 18:33

>>22
I'm not doing prototype OO though. [...] The only thing I can't do
The only thing you can't do is what everyone does with Ruby: treat it like a proto-OO system.

Since Ruby is a dynamic language it isn't hard to support monkey patching classes (at the cost of performance.) This is how eg. rails works. You can't provide an object system in C++ that comes anywhere near approximating the way people use Ruby, where every framework piles its crap into the base classes. It's why Ruby is so slow (look at benchmarks that don't use rails vs. ones that do.)

Why don't you just stop using C++? There are a lot of other languages out there that have different type/object systems. In fact in some languages what you're trying to do is considered quite acceptable instead of downright terrible.

Name: Anonymous 2011-12-26 18:42

>>23

Naturally. This is C++ after all.

Name: Anonymous 2011-12-26 18:49

>>24

I'm not talking about how everyone uses Ruby, I'm talking about how Ruby was built. With the philosophy that everything is an object and should be able to be manipulated as such. Also, given that this is not for any web development, why are we bringing up Rails? Rails is not Ruby, it's just a framework people tend to use with Ruby

Name: Anonymous 2011-12-26 20:57

>>26
I brought up rails because it's the most common Ruby framework. More Ruby code is written with it than without, or was the last time I checked. You don't need to target the web just to use a framework.

Anyway you seem bent on fagging up whatever your working with so you might as well stick with C++. When you finally make integers, floats and pointers into objects you'll be somewhere between the speed of rails and plain Ruby.

Name: Anonymous 2011-12-26 23:02

>>27

Actually, they'll be about as fast as std::string objects, which aren't exactly that slow

Don't change these.
Name: Email:
Entire Thread Thread List