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 7165485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:15:27+00:00 2026-05-28T14:15:27+00:00

I have a class that represents a data stream, it basically reads or writes

  • 0

I have a class that represents a data stream, it basically
reads or writes into a file, but first the data are being encrypted/decrypted and there is also an underlying codec object that handles the media being accessed.

I’m trying to write this class in a RAII way and I’d like a clean, nice, usable design.

What bothers me is that right now there is a lot of work being done in the constructor.
Before the object’s I/O routines can be safely used, first of all the codec needs to initialized (this isn’t very demanding), but then a key is taken into account and crypto and other things are intialized – these require some analysis of the media which takes quite a lot of computation.

Right now I’m doing all this in the constructor, which makes it take a long time. I’m thinking of moving the crypto init stuff (most work) out of the ctor into a separate method (say, Stream::auth(key)), but then again, this would move some responsibility to the user of the class, as they’d be required to run auth() before they call any I/O ops. This also means I’d have to place a check in the I/O calls to verify that auth() had been called.

What do you think is a good design?

P.S. I did read similar question but I wasn’t really able to apply the answers on this case. They’re mostly like “It depens”… :-/

Thanks

  • 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-05-28T14:15:28+00:00Added an answer on May 28, 2026 at 2:15 pm

    The only truly golden unbreakable rule is that the class must be in a valid, consistent, state after the constructor has executed.

    You can choose to design the class so that it is in some kind of “empty”/”inactive” state after the constructor has run, or you can put it directly into the “active” state that it is intended to be in.

    Generally, it should be preferred to have the constructor construct your class. Usually, you wouldn’t consider a class fully “constructed”, until it’s actually ready to be used, but exceptions do exist.
    However, keep in mind that in RAII, one of the key ideas is that the class shouldn’t exist unless it is ready, initalized and usable. That’s why its destructor does the cleanup, and that’s why its constructor should do the setup.

    Again, exceptions do exist (for example, some RAII objects allow you to release the resource and perform cleanup early, and then have the destructor do nothing.)
    So at the end of the day, it depends, and you’ll have to use your own judgment.

    Think of it in terms of invariants. What can I rely on if I’m given an instance of your class? The more I can safely assume about it, the easier it is to use. If it might be ready to use, and might be in some “constructed, but not initialized” state, and might be in a “cleaned up but not destroyed” state, then using it quickly becomes painful.

    On the other hand, if it guarantees that “if the object exists, it can be used as-is”, then I’ll know that I can use it without worrying about what was done to it before.

    It sounds like your problem is that you’re doing too much in the constructor.

    What if you split the work up into multiple smaller classes? Have the codec be initialized separately, then I can simply pass the already-initialized codec to your constructor. And all the authentication and cryptography stuff and whatnot could possibly be moved out into separate objects as well, and then simply passed to “this” constructor once they’re ready.

    Then the remaining constructor doesn’t have to do everything from scratch, but can start from a handful of helper objects which are already initialized and ready to be used, so it just has to connect the dots.

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

Sidebar

Related Questions

I have a class that represents some file data. The file contains some headers.
I have a data structure that represents C# code like this: class Namespace: string
I have class that represents users. Users are divided into two groups with different
I have a domain class in grails 1.5 that represents data from a table
I have a class that started life being mutable, but I've since made it
I have a base class that represents a database test in TestNG, and I
Here is my custom class that I have that represents a triangle. I'm trying
Say I have a class which represents a person, a variable within that class
I have a string variable that represents the name of a custom class. Example:
Let's say I have a table that represents a super class, students . And

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.