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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:47:20+00:00 2026-06-14T02:47:20+00:00

Is it possible to have private member fields inside of an R reference class.

  • 0

Is it possible to have private member fields inside of an R reference class. Playing with some of the online examples I have:

> Account <- setRefClass(    "ref_Account"
>      , fields = list(
>       number = "character"
>       , balance ="numeric")
>      , methods = list( 
>     deposit <- function(amount) {
>       if(amount < 0)   {
>         stop("deposits must be positive")
>       }
>       balance <<- balance + amount
>     }
>     , withdraw <- function(amount) {
>       if(amount < 0)   {
>         stop("withdrawls must be positive")
>       }
>       balance <<- balance - amount
>     }       
>   ) )
> 
> 
> tb <- Account$new(balance=50.75, number="baml-029873") tb$balance
> tb$balance <- 12 
> tb$balance

I hate the fact I can update the balance directly. Perhaps that the old pure OO in me, I really would like to be able make the balance private, at least non-settable from outside the class.

Thoughts

  • 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-14T02:47:21+00:00Added an answer on June 14, 2026 at 2:47 am

    This answer doesn’t work with R > 3.00, so don’t use it!

    As has been mentioned, you can’t have private member fields. However, if you use the initialize method, then the balance isn’t displayed as a field. For example,

    Account = setRefClass("ref_Account", 
                           fields = list(number = "character"),
                           methods = list(
                               initialize = function(balance, number) {
                                   .self$number = number
                                   .self$balance = balance
                               })
    

    As before, we’ll create an instance:

    tb <- Account$new(balance=50.75, number="baml-0029873")
    ##No balance
    tb
    
    Reference class object of class "ref_Account"
    Field "number":
    [1] "baml-0029873"
    

    As I mentioned, it isn’t truly private, since you can still do:

    R> tb$balance
    [1] 50.75
    R> tb$balance = 12 
    R> tb$balance
    [1] 12
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: how do i access static member of a class? I have this
I have a template class. The class has a private member variable which I
I have this member defined in class X { private: int (TMyClass::*pt2Member)(float, char, char);
I want to have a class with a private static data member: class C
Suppose you have a class Foo with private member of type Bar. You don't
When I have a public exposed class, I usually make its member variables private,
Is it possible to have a static collection as a member of a hibernate
Let's say you have a c++0x std::array member of a template class and you
Possible Duplicate: How do you use the non-default constructor for a member? I have
i have the code as below : class Singleton { private: int i; static

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.