private-members

How to do unit testing on private members (and methods) of C++ classes [duplicate]

青春壹個敷衍的年華 提交于 2019-11-28 22:43:14
This question already has an answer here: How do I test a private function or a class that has private methods, fields or inner classes? 50 answers I am very new to unit testing and I am a little confused. I am trying to do unit testing (using the Boost unit testing framework) on a C++ class called VariableImpl . Here are the details. class Variable { public: void UpdateStatistics (void) { // compute mean based on m_val and update m_mean; OtherClass::SendData (m_mean); m_val.clear (); } virtual void RecordData (double) = 0; protected: std::vector<double> m_val; private: double m_mean; }; class

How to write a simple class in C++?

萝らか妹 提交于 2019-11-28 18:34:22
I have been reading a lot of tutorials on C++ class but they miss something that other tutorials include. Can someone please show me how to write and use a very simple C++ class that uses visibility, methods and a simple constructor and destructor? TStamper Well documented example taken and explained better from Constructors and Destructors in C++ : #include <iostream> // for cout and cin class Cat // begin declaration of the class { public: // begin public section Cat(int initialAge); // constructor Cat(const Cat& copy_from); //copy constructor Cat& operator=(const Cat& copy_from); //copy

Private members in CoffeeScript?

二次信任 提交于 2019-11-28 15:33:40
Does somebody know how to make private, non-static members in CoffeeScript? Currently I'm doing this, which just uses a public variable starting with an underscore to clarify that it shouldn't be used outside of the class: class Thing extends EventEmitter constructor: (@_name) -> getName: -> @_name Putting the variable in the class makes it a static member, but how can I make it non-static? Is it even possible without getting "fancy"? Is it even possible without getting "fancy"? Sad to say, you'd have to be fancy . class Thing extends EventEmitter constructor: (name) -> @getName = -> name

c++ compiler error cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

落花浮王杯 提交于 2019-11-28 12:54:46
问题 Hey Im getting an error I think has to do with copying ofstream variable from reading other posts and Ive tried to change std::ofstream outfil; to std::ofstream & outfil; but I get error reference value "outfil" requires an initializer the code is: #include <algorithm> #include <fstream> #include <iostream> #include <iterator> #include <sstream> #include <string> #include <vector> #include <cstdlib> std::vector<double> GetValues_n(const std::vector<std::string>& src, int start, int end) { std

How to create an object with private members using Object.create() instead of new

血红的双手。 提交于 2019-11-28 11:23:10
EDIT: I figured it out from Bergi's answer in the end. Thanks Bergi. pubPrivExample = (function () { return { init : function () { var private; this.setPrivate = function (p) { private = p; }; this.getPrivate = function () { return private; }; }, public : "This is public\n" }; }()); var a; a = Object.create(pubPrivExample); a.init(); a.setPrivate("This is private"); document.write(a.getPrivate()); EDIT: It seems the answers to my question are off at a tangent. I'm really not interested in a factory and actually would rather not use if. My question is about private state. From Bergi's answers

How can a derived class use a protected member of the base class?

三世轮回 提交于 2019-11-28 08:59:26
问题 Say that a base class A defines a protected member. A derived class B uses this member. class A { public: A(int v) : value(v) { } protected: int value; }; class B : public A { public: B(int v) : A(v) { } void print() const; void compare_and_print(const A& other) const; }; The function B::print just takes the value of the current member and prints it: void B::print() const { std::cout << "Value: " << value << "\n"; } The other member function, B::compare_and_print , takes an instance of A ,

Why is it allowed to access Java private fields via reflection?

℡╲_俬逩灬. 提交于 2019-11-28 04:51:36
Consider this example : import java.lang.reflect.Field; public class Test { public static void main(String[] args) { C c = new C(); try { Field f = C.class.getDeclaredField("a"); f.setAccessible(true); Integer i = (Integer)f.get(c); System.out.println(i); } catch (Exception e) {} } } class C { private Integer a =6; } It seems illogical that you are allowed to access private fields of classes with reflection. Why is such a functionality available? Isn't it "dangerous" to allow such access? Private is intended to prevent accidental misuse, not as a security mechanism. If you choose to bypass it

Java private field access possible when having a reference?

让人想犯罪 __ 提交于 2019-11-28 03:18:45
问题 I came across the following "strange" feature today - if you have a reference to an object from the class A in the body of the class A you can access the private fields of this object - i.e: public class Foo{ private int bar; private Foo foo; public void f() { if(foo.bar == bar) // foo.bar is visible here?! { // } } } Anyone has a good explanation about this? 回答1: Access modifiers work at the class level, not at the instance level: all code in the same class can access private members of all

Private method in groovy is not private

痴心易碎 提交于 2019-11-28 02:37:37
问题 class A { private def sayHello() { println "Anish" } } def a_obj = new A() a_obj.sayHello() output : Anish Is there any way to protect sayHello() in groovy or am I missing something? 回答1: There is defect on that in Groovy issue tracking system and that defect is still open. 回答2: Searching for [groovy] private reveals: groovy call private method in Java super class What does 'private' mean in Groovy? How to define private getter method in Groovy Bean? It's not clear if it is a bug or by design

Java - access private instance variables

人走茶凉 提交于 2019-11-28 02:23:27
问题 I need to access the private variables from the following class listing (Species.java) in order to use them in the KlingonOx.java class. The purpose of the KlingonOx.java class is to determine after how many years the population of the Elephant species will be larger than the population of the Klingon Ox species. Here is the Species.java class: import java.util.Scanner; public class Species { private String name; private int population; private double growthRate; public void readInput() {