member

C++ stream as a member variable

久未见 提交于 2019-12-04 02:26:53
I've got a C++ class which I would like to hold a stream used for logging. The stream should be able to be set (and possibly reset) after the construction of the object. It should be possible to set the stream as std::cout , or as a file stream to log to a file, or as a stringstream which does nothing more than ignore the data (a /dev/null of sorts). In any case, it should be an ostream type object, which the creator of the object can reset at any time. The class itself is oblivious to the concrete stream type. I could accomplish this with a pointer to an ostream, but then the syntax becomes a

Scala : Why can't we do super.val?

巧了我就是萌 提交于 2019-12-04 01:24:34
问题 I am practicing this code from JavaTpoint for learning inheritance in Scala. But I cannot access the member Bike from the class Vehicle who's value is initialized to zero. I tried by super type reference but it still shows the overridden value. Why does it not allow to access the super class field and directs to the overridden sub class field (speed) . here is the code and the output. Thanking in advance. class Vehicle { val speed = 0 println("In vehicle constructor " +speed) def run() {

getter and setter for class in class c#

白昼怎懂夜的黑 提交于 2019-12-03 23:36:36
Assuming we have a class InnerClass with attributes and getter/setter. We also have a class OuterClass containing the InnerClass. e.g. class InnerClass { private int m_a; private int m_b; public int M_A { get { return m_a; } set { m_a = value; } } } class OuterClass { private InnerClass innerClass } How would I implement a correct getter and setter for the innerClass member of OuterClass? Thanks in advance! The syntax wouldn't be any different. Just... public InnerClass InnerClass { get { return innerClass; } set { innerClass = value; } } By the way, if you're using C# in .NET 3.5, you can use

How to set a login cookie in django?

守給你的承諾、 提交于 2019-12-03 23:15:29
问题 How do I set_cookie with the username of the member that is logged in to my site? Thanks 回答1: you can implement this by using the session middleware, make sure to enable it in your project. I recommend you to use django.contrib.auth to manage sessions though. It manages sessions in the database which is a lot safer than just saving the username in a cookie 回答2: Here is an example of how to do it using middleware class UserCookieMiddleWare(object): """ Middleware to set user cookie If user is

About sizeof of a class member function pointer [duplicate]

柔情痞子 提交于 2019-12-03 17:15:28
问题 This question already has answers here : Pointers to members representations (2 answers) Closed 6 years ago . Let's say we have a class A class A; and these typedefs typedef void (A::*a_func_ptr)(void); typedef void (*func_ptr)(void); My question is why sizeof(a_func_ptr) returns 16, while sizeof(func_ptr) returns 4 (as for any pointer on x86 system)? For instance int main(int argc, char *argv[]) { int a = sizeof(a_func_ptr); int b = sizeof(func_ptr); } 回答1: My question is why sizeof(a_func

PowerShell how to add something on parsed JSON?

余生长醉 提交于 2019-12-03 15:10:04
问题 I want to add something in my parsed JSON using PowerShell. My code: function ConvertFromJson([string]$file) { [System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions") $jsoncontent = Get-Content $file $jsonobj = New-Object System.Web.Script.Serialization.JavaScriptSerializer $global:json = $jsonobj.DeserializeObject($jsoncontent) } My JSON: { "BlockA": { "BlockB": { "name": "BlockB", "value": "Value_B" }, } I want make BlockC like this: { "BlockA": { "BlockB": { "name":

Constructor for '' must explicitly initialize the reference member ''

北城以北 提交于 2019-12-03 14:44:43
问题 I have this class class CamFeed { public: // constructor CamFeed(ofVideoGrabber &cam); ofVideoGrabber &cam; }; And this constructor: CamFeed::CamFeed(ofVideoGrabber &cam) { this->cam = cam; } I get this error on the constructor: Constructor for '' must explicitly initialize the reference member '' What is a good way to get around this? 回答1: You need to use the constructor initializer list: CamFeed::CamFeed(ofVideoGrabber& cam) : cam(cam) {} This is because references must refer to something

enable class's member depending on template

天大地大妈咪最大 提交于 2019-12-03 12:30:19
I already know that you can enable (or not) a class's method using std::enable_if for exemple: template<size_t D, size_t E> class Field { ... size_t offset(const std::array<float,D>& p) const { ... } template<typename TT = size_t> typename std::enable_if<D!=E, TT>::type offset(const std::array<float,E>& p) const { return offset(_projection(p)); } ... }; This helps not being able to call function that are invalid in a specific case as well as removing overloading errors ... which, to me, is very nice ! I'd like to go further and make some of my class's members being present only if the are

Java: Accessing private fields directly from another instance of the same class

烂漫一生 提交于 2019-12-03 12:29:37
I'm writing a equals(Object obj) function for a class. I see that it is possible to access the private fields of obj from the caller. So instead of using a getter: Odp other = (Odp) obj; if (! other.getCollection().contains(ftw)) { } I can just access the field directly: Odp other = (Odp) obj; if (! other.collection.contains(ftw)) { } Is this bad practice? No, it's not. The reason that private variables and methods are not accessable from other classes is to allow you to change the internals of your class without having to change all the code that uses the class (that and to prevent the user

In C++11, protected means public?

二次信任 提交于 2019-12-03 09:11:28
问题 Continuing something learned in C++ error: base function is protected ... The C++11 pointer-to-member rules effectively strip the protected keyword of any value, because protected members can be accessed in unrelated classes without any evil/unsafe casts. To wit: class Encapsulator { protected: int i; public: Encapsulator(int v) : i(v) {} }; Encapsulator f(int x) { return x + 2; } #include <iostream> int main(void) { Encapsulator e = f(7); // forbidden: std::cout << e.i << std::endl; because