private-members

Why `private static` field is not allowed in Java 8 interface?

大城市里の小女人 提交于 2019-12-01 15:23:29
问题 When I'm trying to compile the following code public interface SomeInterface{ private static Logger logger = Logger.getLogger(); public default void someMethod(){ logger.info("someMethod: default implementation"); } } I get an error Illegal modifier for the interface field SomeInterface.logger; only public, static & final are permitted When I delete private modifier, code compiles, but I don't want other classes from the package to see this field. Why Java doesn't allow me to do such thing

What does the “private” modifier do?

杀马特。学长 韩版系。学妹 提交于 2019-12-01 13:44:22
问题 Considering "private" is the default access modifier for class Members, why is the keyword even needed? 回答1: It's for you (and future maintainers), not the compiler. 回答2: There's a certain amount of misinformation here: "The default access modifier is not private but internal" Well, that depends on what you're talking about. For members of a type, it's private. For top-level types themselves, it's internal. "Private is only the default for methods on a type" No, it's the default for all

Is private member hacking defined behaviour?

£可爱£侵袭症+ 提交于 2019-12-01 13:44:04
问题 I have the following class: class BritneySpears { public: int getValue() { return m_value; }; private: int m_value; }; Which is an external library (that I can't change). I obviously can't change the value of m_value , only read it. Even deriving from BritneySpears won't work. What if I define the following class: class AshtonKutcher { public: int getValue() { return m_value; }; public: int m_value; }; And then do: BritneySpears b; // Here comes the ugly hack AshtonKutcher* a = reinterpret

Can't declare ifstream class member in header file

时光怂恿深爱的人放手 提交于 2019-12-01 10:57:43
I am trying to declare an ifstream object in a header file as is shown but I get an error saying that it cannot be accessed. I have tried various things such as making it into a pointer instead, initialising in the .c file etc. but my code can't seem to get part the declaration of it. ReadFile.h: #ifndef READFILE_H #define READFILE_H #include "cv.h" #include "highgui.h" #include <iostream> #include <fstream> class ReadFile{ private: std::ifstream stream; public: std::string read(); ReadFile(); // Default constructor ~ReadFile(); // Destructor }; #endif ReadFile.c: #include "ReadFile.h"

Declaring strings as std:string in C++

落花浮王杯 提交于 2019-12-01 10:57:19
This is based on GCC/G++ and usually on Ubuntu. Here's my sample program I've done: #include <iostream> using namespace std; int main() { std::string c = "Test"; cout << c; return 0; } The above code works fine. But I have two issues that I don't quite get... Writing the string declaration as std:string (with one : ) also works fine. What's the difference? If I use std:string (with one : ) within a class to declare a private variable, I get an error error: ‘std’ does not name a type . Example of this declaration: class KType { private: std:string N; }; Can someone please explain these issues?

Declaring strings as std:string in C++

瘦欲@ 提交于 2019-12-01 07:34:26
问题 This is based on GCC/G++ and usually on Ubuntu. Here's my sample program I've done: #include <iostream> using namespace std; int main() { std::string c = "Test"; cout << c; return 0; } The above code works fine. But I have two issues that I don't quite get... Writing the string declaration as std:string (with one : ) also works fine. What's the difference? If I use std:string (with one : ) within a class to declare a private variable, I get an error error: ‘std’ does not name a type . Example

Java - making a static reference to the non-static field list

∥☆過路亽.° 提交于 2019-12-01 05:40:36
I've just been experimenting and found that when I run the rolling code, it does not compile and I can't figure out why. My IDE says 'Cannot make a static reference to the non-static field list', but I don't really understand what or why this is. Also what else does it apply to, i.e.: is it just private variables and or methods too and why?: public class MyList { private List list; public static void main (String[] args) { list = new LinkedList(); list.add("One"); list.add("Two"); System.out.println(list); } } However, when I change it to the following, it DOES work: public class MyList {

Python “private” name mangling and instance vs class attributes

最后都变了- 提交于 2019-12-01 04:24:01
I was writing a decorator that needs to access private variables and found this discrepancy. Can anyone explain this? (Python 2.5) Naming mangling works as expected for attributes defined in the class: >>> class Tester(object): ... __foo = "hi" >>> t = Tester() >>> t._Tester__foo 'hi' Instance attributes do not work (and this is the way we are supposed to do it right?) >>> class Tester(object): ... def __init__(self): ... self.__foo = "hi" >>> t = Tester() >>> t._Tester__foo AttributeError: 'Tester' object has no attribute '_Tester__foo' P.S. Is "class attribute" the right word for these? They

How to hide private members of a Class?

倖福魔咒の 提交于 2019-12-01 03:59:14
I've been using visual studio for some time and it annoys me everytime when I work with Classes. The problem is, when I create an object of a Class I tend to see the private members belongs to that class and I don't want to, because what if I create a class with 10+ private variable, then it will be a nightmare, there must be a way to hide private members, If there is a way could you please share it with me? Thank you :) EDIT: Here is a picture that will help you understand what I'm talking about, for example here I have 2 private variables of LinkedList class (curSize and head) I won't be

Java - making a static reference to the non-static field list

耗尽温柔 提交于 2019-12-01 03:34:07
问题 I've just been experimenting and found that when I run the rolling code, it does not compile and I can't figure out why. My IDE says 'Cannot make a static reference to the non-static field list', but I don't really understand what or why this is. Also what else does it apply to, i.e.: is it just private variables and or methods too and why?: public class MyList { private List list; public static void main (String[] args) { list = new LinkedList(); list.add("One"); list.add("Two"); System.out