private

Properties vs Public member variables [duplicate]

↘锁芯ラ 提交于 2019-12-04 02:35:52
Possible Duplicate: What is the difference between a field and a property in C# I'm a beginning programmer and I've read all about class properties. Books state that properties allow you to indirectly access member variables. Ok, so what makes it any different than just making the field public and accessing it directly? Here's a quote from Learning C# 3.0 by Jesse Liberty: For example, you might want external classes to be able to read a value, but not change it; or you might want to write some code so that the internal field can accept only values in a certain range. If you grant external

Can a friend class object access base class private members on a derived class object?

守給你的承諾、 提交于 2019-12-04 00:31:34
I'm surprised that the code below compiles. It seems that a class befriended to the (publicly inherited) base class can access a member of the base class provided an instance of the derived class. If the inheritance is changed to private then compilation fails. In short, how is d.b_var valid within F::func(D& d) ? #include <iostream> #include <string> using namespace std; class B{ int b_var; friend class F; }; class D: public B{ int d_var; }; class F{ public: void func(D &d){ d.b_var = 5; } }; int main() { cout<<"fine"; } sameerkn Object of class D is composed of 2 separate parts : part

Package private modifier in Scala 2.8

点点圈 提交于 2019-12-03 23:19:25
问题 If I try private[com.company.foo] def bar(xml: XmlPath) = { I get [error] ... ']' expected but '.' found. [error] private[com. [error] ^ What's with that? I can only make it package-private to com.*, or...? 回答1: You can only define the enclosing package, within which the code is defined: package com.company.foo class Bar{ private[foo] def bar(xml: XmlPath) } and if you want to set it to company: private[company] def bar(xml: XmlPath) 来源: https://stackoverflow.com/questions/9032631/package

Private 'set' in C# - having trouble wrapping my brain around it

孤者浪人 提交于 2019-12-03 22:31:34
I've seen a lot of example code written using something like (please forgive how horribly canned this is): public class Test { public object Thingy { get; private set; } } Unfortunately, these kinds of examples never really explain why 'set' is set as private. So, I'm just wondering if there's a good, common example that will illustrate to me why something like this would be used. I sort of see it - the property can be run to process some extra logic in addition to setting that field. I'm just confused on how it would be invoked, and why this approach would be used rather than a generic setter

Inaccessible type due to private inheritance

╄→гoц情女王★ 提交于 2019-12-03 19:51:54
问题 g++ is denying me access to a type, just because it happens to be a private grand-father. Does this make sense? struct A {}; struct B : private A {}; struct C : B { void foo(A const& a) {} }; Compiling this yields: 1:10: error: ‘struct A A::A’ is inaccessible 6:12: error: within this context My point is: I never wanted to access A as an ancestor. In fact, if A is a private ancestor of B , shouldn't this be completely invisible to anybody but B (i.e. C )? Of course, I could use protected

C++: Private virtual functions vs. pure virtual functions [duplicate]

妖精的绣舞 提交于 2019-12-03 14:45:27
Possible Duplicate: Private virtual method in C++ If I understood correctly from this post ( Private virtual method in C++ ), making a virtual function in a base class makes the derived classes able to override it. But it seems things stop there. But if the base class virtual function is pure, that forces the derived classes to implement the function. Hence, a pure (public) virtual function is merely an interface. I can see a benefit here. On the other hand, by making a base class virtual function private, only gives the derived class the ability to override the function, but I see no benefit

JUnit Testing private variables? [duplicate]

房东的猫 提交于 2019-12-03 14:17:36
问题 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 5 years ago . I have been assigned the task of unit testing a class that I never worked directly on with JUnit, and am strictly forbidden to change the code in the package. This is usually no issue, since most of our unit testing is just for functionality and input/output consistency, which can be done simply by running routines and

Erroneous private base class inaccessible?

无人久伴 提交于 2019-12-03 13:18:05
Compiling this code using g++ 4.2.1: struct S { }; template<typename T> struct ST { }; template<typename BaseType> class ref_count : private BaseType { }; template<typename RefCountType> class rep_base : public RefCountType { }; class wrap_rep : public rep_base<ref_count<S> > { typedef rep_base<ref_count<S> > base_type; // line 11 }; I get: bug.cpp:1: error: ‘struct S’ is inaccessible bug.cpp:11: error: within this context However, if I change the wrap_rep class to use ST : class wrap_rep : public rep_base<ref_count< ST<int> > > { typedef rep_base<ref_count< ST<int> > > base_type; }; it

Grant access to private constructor without friends?

拜拜、爱过 提交于 2019-12-03 12:52:05
I am working on some code, where I encountered a situation similar to this one: struct Bar; struct Foo{ friend struct Bar; private: Foo(){} void f(){} void g(){} }; struct Bar { Foo* f; Bar() { f = new Foo();} ~Bar() { delete f;} }; int main(){ Bar b; } I would prefer to have Bar not as friend of Foo , because besides Foo s constructor Bar does not need access to any of Foo s private methods (and thus should not have access). Is there a way to allow only Bar to create Foo s without making them friends? PS : realized that the question might not be 100% clear. I don't mind if it is via friends

“Private” struct members in C with const

不打扰是莪最后的温柔 提交于 2019-12-03 12:32:30
In order to have a clean code, using some OO concept can be usefull, even in C. I often write modules made of a pair of .h and .c files. The problem is that the user of the module have to be careful, since private members don't exist in C. The use of the pimpl idiom or abstract data types is ok, but it adds some code and/or files, and requires a heavier code. I hate using accessor when I don't need one. Here is a idea wich provides a way to make the compiler complain about invalid access to "private" members, with only a few extra code. The idea is to define twice the same structure, but with