element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Blog C++ write your own stream class - part 1: output stream initial design (embedded friendly C++)
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 11 Nov 2025 7:09 PM Date Created
  • Views 21 views
  • Likes 2 likes
  • Comments 2 comments
  • Modern C++
  • c++
Related
Recommended

C++ write your own stream class - part 1: output stream initial design (embedded friendly C++)

Jan Cumps
Jan Cumps
11 Nov 2025

In C++ it's common to stream data. You write to a file using the << operator.

example: 

cout << "hello, world! << endl;

In this blog I'm making my own minimal output stream class. Just enough code to show that it works. This could become an object that allows you to write to a UART, SPI, CAN, ... . In this post, I called my class uartostream, although it doesn't do anything UART. Just to give it an embedded sounding name, and to show how this could look like in firmware.

I put embedded friendly in the title because this is a resource-friendly exercise. Can be used on the smallest controllers.

output stream class

You can stream c-type strings to this class, and std::string objects. 

class uartostream {
public:
    uartostream() {}    
    
    uartostream& operator << (const char* msg) {
      // write to UART
      return *this;
    }
    
    uartostream& operator << (const std::string& msg) {
      // write to UART
      return *this;
    }
};

Notice that the class doesn't do anything with them. In this first post I want to check if the code compiles. And I use a debugger to see if the strings arrive in their handlers. If I put a breakpoint in the handler methods, and the msg parameter contains the string we've streamed to it, the mechanism works.

using the class

In the example code, I'll stream a std::string and a classic c-style char string. 

#include <string>

class uartostream{ // see previous section for class definition
};

uartostream u;

int main() {
  u << std::string("5") << "5" ;
}

test

I put a breakpoint in both operator << members. The code should first halt in the one that takes a std::string reference. Then the one that takes the c string.

image

The screen capture above shows the state while streaming the first part. If you want to see what happens when the "5" is streamed: try it in your favourite C++ IDE.

Thank you for reading. To be continued.

  • Sign in to reply
Parents
  • DAB
    DAB 8 hours ago

    Nice post Jan.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • DAB
    DAB 8 hours ago

    Nice post Jan.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube