access-specifier

Can I access a base classes protected members from a static function in a derived class?

时光总嘲笑我的痴心妄想 提交于 2019-12-05 10:07:19
问题 I have a program where I need to make a base class which is shared between a dll and some application code. Then I have two different derived classes, one in the dll one in the main application. Each of these have some static member functions which operate on the data in the nase class. (They need to be static as are used as function pointers elsewhere). In its simplest form my issue is shown below. class Base { protected: int var ; }; class Derived : public Base { static bool Process( Base

C++ Is private really private?

别来无恙 提交于 2019-12-04 22:40:02
I was trying out the validity of private access specifier in C++. Here goes: Interface: // class_A.h class A { public: void printX(); private: void actualPrintX(); int x; }; Implementation: // class_A.cpp void A::printX() { actualPrintX(); } void A::actualPrintX() { std::cout << x: } I built this in to a static library (.a/.lib). We now have a class_A.h and classA.a (or classA.lib) pair. I edited class_A.h and removed the private: from it. Now in another classTester.cpp: #include "class_A.h" // the newly edited header int main() { A a; a.x = 12; // both G++ and VC++ allowed this! a.printX(); /

Why can I access private/protected methods using Object#send in Ruby?

泄露秘密 提交于 2019-12-04 07:19:46
The class class A private def foo puts :foo end public def bar puts :bar end private def zim puts :zim end protected def dib puts :dib end end instance of A a = A.new test a.foo rescue puts :fail a.bar rescue puts :fail a.zim rescue puts :fail a.dib rescue puts :fail a.gaz rescue puts :fail test output fail bar fail fail fail .send test [:foo, :bar, :zim, :dib, :gaz].each { |m| a.send(m) rescue puts :fail } .send output foo bar zim dib fail The question The section labeled "Test Output" is the expected result. So why can I access private/protected method by simply Object#send ? Perhaps more

What is the difference between access specifier protected and internal protected in C#

怎甘沉沦 提交于 2019-12-04 05:53:07
What is the difference between access specifier protected and internal protected in C# ? Internal can be seen within the assembly. Protected can be seen by classes inheriting from the class where it is defined. Protected internal can be seen within the assembly OR types derived from the class where it is defined (including types from other assemblies). See: http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx Copied from the page: public Access is not restricted. protected Access is limited to the containing class or types derived from the containing class. internal Access is limited to the

What actually occurs when stating “private”/“protected” in Ruby?

半城伤御伤魂 提交于 2019-12-04 05:16:30
What is actually occurring when private / protected is stated within a Ruby class definition? They are not keywords , so that implies they must be method calls, but I cannot find where they are defined. They do not appear to be documented. Are the two different ways of declaring private / protected methods (shown below) implemented differently? (The second way is obviously a method call, but this is not so apparent in the first way.) class Foo private def i_am_private; end def so_am_i; end end class Foo def i_am_private; end def so_am_i; end private :i_am_private, :so_am_i end Zabba Both are

When should we consider using private or protected?

ぃ、小莉子 提交于 2019-12-04 01:56:25
Just wondering, when should we actually must use private or protected for some methods in the model? Sometimes I can't not be bothered to group my methods in private nor protected . I just leave it as it is. But I know it must be a bad practice, otherwise these 2 groupings won't be created in programming. Thanks. If you plan to call a method externally, record.method() , then "public" If it will be used only internally, self.method() , then "private" If you plan to use it internally, but also in descendants, self.method() # in subclass , then "protected" iain I'll give my opinion , and maybe I

Can I access a base classes protected members from a static function in a derived class?

我的梦境 提交于 2019-12-03 22:34:05
I have a program where I need to make a base class which is shared between a dll and some application code. Then I have two different derived classes, one in the dll one in the main application. Each of these have some static member functions which operate on the data in the nase class. (They need to be static as are used as function pointers elsewhere). In its simplest form my issue is shown below. class Base { protected: int var ; }; class Derived : public Base { static bool Process( Base *pBase ) { pBase->var = 2; return true; } }; My compiler complains that I cannot access protected

Ruby Class#new - Why is `new` a private method?

非 Y 不嫁゛ 提交于 2019-12-03 20:29:04
I made a Matrix class and I want to use it in various parts of my code. class Matrix def initialize(x, y, v=0) @matrix = Array.new (0..y).each do |j| @matrix[j] = Array.new (0..x).each do |i| @matrix[j][i] = v end end end end When this code is included in the same class as the code that uses it, everything runs fine. When I move this code to lib/matrix.rb and require it, I get the following error: ./phylograph:30:in `block in run': private method `new' called for Matrix:Class (NoMethodError) DigitalRoss As I recall, Matrix is a purely functional class; its objects are immutable, and simply

How to make the class constructor private in Ruby?

风流意气都作罢 提交于 2019-12-03 18:38:26
问题 class A private def initialize puts "wtf?" end end A.new #still works and calls initialize and class A private def self.new super.new end end doesn't work altogether So what's the correct way? I want to make new private and call it via a factory method. 回答1: Try this: class A private_class_method :new end More on APIDock 回答2: The second chunk of code you tried is almost right. The problem is private is operating in the context of instance methods instead of class methods. To get private or

Why is it legal to inappropriately access privates in an explicit instantiation?

白昼怎懂夜的黑 提交于 2019-12-03 12:06:42
问题 Why on earth would this be allowed: ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// template<typename T> struct invisible { static typename T::type value; }; template<typename T> typename T::type invisible<T>::value; ////////////////////////////////////////////////////////////////////////// template<typename T, typename T::type P> class construct_invisible { construct_invisible(){ invisible<T