Alright guys I have a question. I am trying to write a custom data type. Let me explain a little further. I'll start out at the base.

te_char (you don't really have to worry about this.

>char c_char
>rgb color

te_word (weird part)

>int type (based on an enumeration
>vector <te_char> whatever
>what ever else

>inline void conv_type_to_word

te_body : public ostream (I want it to work this way atleast)

>vector <te_word> whatver

I want to be able to write to the te_body's vector like you would a cout.

Like

te_body awesomesauce;
awesomesauce << "SMILEY EXTREME =" << 7;

OR

I'd like to be able to dynamically write to a te_word with any data type. At that I can handle converting to it. I just want to litteraly make a functuion with a dynamic amount of arguments or make an operator that is like the "cout <<" that writes to the vector of te_words. I can determine the type of input within that function or operator.

I tried this to no avail:

#include <cstdlib>
#include <iostream>

using namespace std;
class lol: public ostream
{    
};
int main(int argc, char *argv[])
{
    lol b;
    b<< "HI" << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

It had no errors but crashed upon running.

Recommended Answers

All 4 Replies

If all you want is the syntax of ostream inserters, it is easy to duplicate without dealing with the iostreams framework. Here is an example taking strings.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class VectorStream {
public:
    string& operator[](int i) { return v[i]; }

    friend VectorStream& operator<<(VectorStream& strm, string const& s)
    {
        strm.v.push_back(s);
        return strm;
    }
private:
    vector<string> v;
};

int main()
{
    VectorStream strm;

    strm << "a" << "test" << "baby";

    for (int i = 0; i < 3; ++i)
        cout << strm[i] << '\n';
}

Okay I just looked at that, I don't have a compiler right now but could I go like:

strm << "hi" << 27373;

? Or would it be like the Iostreams own ostream function where they declare it for each data type??

EDIT:

Okay I see how it works, the v vector would be my te_word vector, and I would have to make a operator friend function for every different type such as int, double, etc.

I FIGURED IT OUT. Here is an example of it working. :)

#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>	// for itoa() call
#include <stdio.h>	// for printf() call

char buf[10000];

using namespace std;



class te_body {
public:
    string& operator[](int i) { return v[i]; }

    friend te_body& operator<<(te_body& strm, string const& s)
    {
        strm.v.push_back(s);
        return strm;
    }
    
    friend te_body& operator<< (te_body& strm, int const& i)
    {
        strm.v.push_back(itoa(i, buf, 10));
        return strm;
    }
    
    unsigned int size()
    {
         return v.size();
    }
private:
    vector<string> v;
};

int main()
{
    int b;
    b = 348347834;
    te_body strm;

    strm << "a" << "test" << "baby" << b;

    for (int i = 0; i < strm.size() ; ++i)
        cout << strm[i] << '\n';
        
        system("PAUSE");
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.