private-members

Access private elements of object of same class

孤人 提交于 2019-12-30 03:20:07
问题 Is this legal? If not, will the following code allow this? class Foo { friend class Foo; } 回答1: That's redundant. Foo already has access to all Foo members. Two Foo objects can access each other's members. class Foo { public: int touchOtherParts(const Foo &foo) {return foo.privateparts;} private: int privateparts; }; Foo a,b; b.touchOtherParts(a); The above code will work just fine. B will access a's private data member. 回答2: Yes it is legal for an object of class Foo to access the private

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

怎甘沉沦 提交于 2019-12-29 04:29:18
问题 This question already has answers here : How do I test a private function or a class that has private methods, fields or inner classes? (51 answers) Closed 2 years ago . 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

With a private modifier, why can the member in other objects be accessed directly?

三世轮回 提交于 2019-12-28 01:55:51
问题 I have the following code: class A { private: int x; public: A() { x = 90; } A(A a1, A a2) { a1.x = 10; a2.x = 20; } int getX() { return this->x; } }; I know that code might be weird but I don't understand why a1 and a2 can access private data member x ? 回答1: Good question. The point is that protection in C++ is class level , not object level. So a method being invoked on one object can access private members of any other instance of the same class. This makes sense if you see the role of

Why do objects of the same class have access to each other's private data?

删除回忆录丶 提交于 2019-12-27 10:02:21
问题 Why do objects of the same class have access to each other's private data? class TrivialClass { public: TrivialClass(const std::string& data) : mData(data) {}; const std::string& getData(const TrivialClass& rhs) const { return rhs.mData; }; private: std::string mData; }; int main() { TrivialClass a("fish"); TrivialClass b("heads"); std::cout << "b via a = " << a.getData(b) << std::endl; return 0; } This codes works. It is perfectly possible for object a to access private data from object b

How to call constructor and variables from another class?

好久不见. 提交于 2019-12-25 18:29:29
问题 I have a main class: class Sportist{ private: string ime; int godina_na_ragjanje; int godisna_zarabotuvacka_EUR; public: Sportist(string i, int g_n_r, int g_z_EUR){ ime = i; godina_na_ragjanje = g_n_r; godisna_zarabotuvacka_EUR = g_z_EUR; } }; And now I have a new class like this: class Fudbaler:public Sportist{ private: int broj_na_odigrani_natprevari; int danocna_stapka; public: Fudbaler(string ime, int godina, int zarabotuvacka, int b, int d){ :Sportist(ime, godina, zarabotuvacka) broj_na

Accessibility of data member in member function before declaration of data member

柔情痞子 提交于 2019-12-25 05:14:14
问题 Consider this code: class Test { public: Test() { i = 0; } private: int i; }; Data member 'i' is used even before it is declared/defined. Should this not be a compilation error ? (It compiled fine!!!) 回答1: The rule is that member functions defined in the class definition are compiled as if they were defined immediately after the class definition. 回答2: No it shouldn't, within the context of the class definition, all members, data members or functions have complete visibility. 回答3: where is the

Accessibility of data member in member function before declaration of data member

試著忘記壹切 提交于 2019-12-25 05:14:06
问题 Consider this code: class Test { public: Test() { i = 0; } private: int i; }; Data member 'i' is used even before it is declared/defined. Should this not be a compilation error ? (It compiled fine!!!) 回答1: The rule is that member functions defined in the class definition are compiled as if they were defined immediately after the class definition. 回答2: No it shouldn't, within the context of the class definition, all members, data members or functions have complete visibility. 回答3: where is the

unit test private methods in F#

眉间皱痕 提交于 2019-12-23 12:44:28
问题 Let's say we have a class type ThisClassIsComplicated () = let calculateSomething a b = a + b In this case calculateSomething is trivial, but if it would be more complicated it may make sense to verify that the calculations done there are correct. It might make sense to use a unit testing framework to test that private methods. My question: how to unit test private methods in F#? Some random thoughts: The selected answer here, suggests to use the InternalsVisibleTo attribute which anyway is

Pylint W0212 protected-access

蓝咒 提交于 2019-12-23 06:49:23
问题 In Python, prefixing with one underscore indicates that a member should not be accessed outside of its class. This seems to be on a per-class basis like Java and C++. However, pylint seems to enforce this convention on a per-object basis. Is there a way to allow per-class access without resorting to #pylint: disable=protected-access ? class A: def __init__(self): self._b = 5 def __eq__(self, other): return self._b == other._b Result: pylint a.py a.py:6: W0212(protected-access) Access to a

Javascript private methods — what is the memory impact?

爷,独闯天下 提交于 2019-12-22 10:09:18
问题 I'm working on a bit of code where I'm attempting to hide some private variables inside closures. The thing is the environment is fairly constrained in terms of memory, so I'm also concerned with keeping the overall footprint of the classes low. What is the impact of using closures to hide private instance variables and methods when compared to just making all methods and variables on an object public? Would an instance of the one using closures take up more memory than an instance that did