Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8397657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:50:56+00:00 2026-06-09T20:50:56+00:00

I have a kind of buffer class that takes an std::vector as a constructor

  • 0

I have a kind of buffer class that takes an std::vector as a constructor parameter and uses this vector to storage bytes. This class has methods for reading from and for writing to the buffer. However I would want to give a const std::vector to this buffer class and still be able to use the read functions while write functions should fail at compile time or at least throw an exception.

Only solution that I come up was something like this:

class Buffer
{
    public:
    Buffer(std::vector<uint8>& vec)
    { 
        this->writeVec = vec
        this->readVec = vec;
    }
    Buffer(std::vector<uint8> const& vec)
    {
        this->writeVec = null
        this->readVec = vec;
    }
    void write(uint8 i)
    {
        this->throwIfWriteVecNull();
        // do write to writeVec
    }
    uint8 read()
    {
        // do read from readVec
    }

    private:
    std::vector<uint8>& writeVec;
    std::vector<uint8> const& readVec;
}

Is there any way to achive this without separate writer and reader classes (It would separate similar logic to two different classes which is not nice) and with compile time check for write access? I don’t want to use const_casts to any other unsafe hacks. Feel also free to suggest alternative pattern/architecture as a solution.

EDIT:

Thanks all for your answers. From the three answers user315052’s facade pattern was closest to something what I wanted, since IMO const_cast or multiple pointers are worse than having few more classes. I also did bit more research and stumbled upon this SO q/a where templates are used to select between const and non-const type. Now I have something like the following and it works perfect, I get “no method error” on compile time if I try to call write on const version. The code got bit ugly beacause of the all templates and stuff but getting errors on compile time is much much better than exceptions.

template <typename BlockType, bool isMutable>
class BaseBuf : boost::noncopyable
{
public:
    typedef typename boost::mpl::if_c<isMutable, std::vector<BlockType>, std::vector<BlockType> const>::type VectorType;
    BaseBuf(VectorType& blocks) : blocks(blocks)
    {
    }
    void seekReadCursor(size_t pos)
    {
         // seek to pos
    }
    bool readBool() const
    {
         // do read from pos
    }
    //void read...
    //...
protected:

    VectorType& blocks;
};

template <typename BlockType>
class ImmuBuf : public BaseBuf<BlockType, false>
{
public:
    typedef BaseBuf<BlockType, false> Parent;
    typedef typename Parent::VectorType VectorType;
    ImmuBuf(VectorType& blocks) : Parent(blocks)
    {
    }
private:

};

template <typename BlockType>
class MutaBuf : public BaseBuf<BlockType, true>
{
public:
    typedef BaseBuf<BlockType, true> Parent;
    typedef typename Parent::VectorType VectorType;
    MutaBuf(VectorType& blocks) : Parent(blocks)
    {
    }
    // void resize()...
    void writeBool(bool b)
    {
        // do write
    }
    //void write...
    //...
private:

};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:50:58+00:00Added an answer on June 9, 2026 at 8:50 pm

    It really seems like you want a Buffer to be a facade for a read/write version and a read-only version.

    class BufferInterface {
        friend class Buffer;
        friend class std::default_delete<BufferInterface>;
    protected:
        virtual ~BufferInterface () {}
        virtual void write (uint8_t) = 0;
        virtual uint8_t read () = 0;
        //...
    };
    
    class Buffer {
        std::unique_ptr<BufferInterface> impl_;
    public:
        Buffer (std::vector<uint8_t> &v) : impl_(new BufferReadWrite(v)) {}
        Buffer (const std::vector<uint8_t> &v) : impl_(new BufferReadOnly(v)) {}
        void write(uint8_t i) { impl_->write(i); }
        uint8_t read () { return impl_->read(); }
        //...
    };
    

    BufferInterface can implement any common logic to be reused by both the read-only version and the read/write version.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UUID class that has an inbuilt 16 byte buffer for the
I currently have this kind of loop while(1) { generate_string(&buffer); for(int i = 0;
I'm creating a DirectX 11 helper class that looks kind of like this: #import
I have kind of library which uses a bunch of its own perf counters.
I have this kind of question. In my form, i got this as a
I have this kind of node in my xml document: <parent ...> <a .../>
I have a kind of strange thing that I really nead for my text
I have these kind of list : <ul id=navmenu-v> <li class=level1><a id=56 class=s1>Accessories</a> <ul
I have a class that manages the creation of RTF documents and a method
I have a raw table used as a kind of buffer where periodically new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.