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
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • 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
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog Add external SPI Flash Memory to Raspberry Pico - 5: pass STL containers as read-only parameters
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
GPIO Pinout
Raspberry Pi Wishlist
Comparison Chart
Quiz
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 9 Apr 2023 11:21 AM Date Created
  • Views 1462 views
  • Likes 8 likes
  • Comments 7 comments
Related
Recommended
  • pico
  • pico_m25
  • pico_eurocard
  • c++

Add external SPI Flash Memory to Raspberry Pico - 5: pass STL containers as read-only parameters

Jan Cumps
Jan Cumps
9 Apr 2023
Add external SPI Flash Memory to Raspberry Pico - 5: pass STL containers as read-only parameters

A projects to learn the SPI API of the RP2040 C SDK, and to use it to control an external Flash IC. In this post, I refine the m25 c++ class again. take care that an STL container is read-only when passing it to a member.

image

Pass an Array as read-only input parameter

C++ can be used as a strict language. One of the strict options is to flag that an object, passed as reference to a method, will / can not be modified inside the method. The object will have the same state after the execution as before the execution.

class m25 {
public:
  // ...
  void write(uint32_t address, const std::vector<uint8_t>& data);

In the write() method, the first parameter is passed by value. That is non-mutable by design.
The second parameter is a reference to a vector object. I pass it by reference, to avoid that a copy has to be made of this object. This is a common c++ construct. But that gives the write() the powers to alter the vector or its content.

When you add const in front of the parameter, it's a sign for the compiler that you can only perform calls on that object that don't alter it. When you try to call a modifier (add or change date, empty the container, ...), the compiler will throw an error.

That's it - a small construct that can help avoid that an object is altered. There's more to const. See from page 66 on in Industrial Strength c++.

image

link to all posts in this series

  • Sign in to reply
  • Jan Cumps
    Jan Cumps over 2 years ago in reply to Jan Cumps

    link to the quote above: https://cplusplus.com/reference/vector/vector/operator[]/

    (I couldn't edit the comment)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 2 years ago

    side note:
    In my write() method, I access the raw data buffer of the vector, via the [] operator:

    std::vector<uint8_t> m25::rdid() {
        // ....
        spi_read_blocking(spi, 0, (&v[0]), v.size()); // Manufacturer Identification, Device Identification (Memory Type || Memory Capacity)
        // ...
    }

    You could think that this may give my method the powers to modify the raw buffer. But there is protection:

    If the vector object is const-qualified, the function returns a const_reference. Otherwise, it returns a reference.

    In my code, I pass the vector as a const reference. So it's const-qualified and the raw buffer can only be used in code that doesn't alter it.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
<
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 © 2026 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