constructor

“var” variables, “this” variables and “global” variables - inside a JavaScript Constructor

六眼飞鱼酱① 提交于 2019-12-19 03:23:51
问题 After my last question, this one is more accurate for me: example: function Foo() { this.bla = 1; var blabla = 10; blablabla = 100; this.getblabla = function () { return blabla; // exposes blabla outside } } foo = new Foo(); what I understand now: this.bla = 1; // will become an attribute of every instance of FOO. var blabla = 10; // will become a local variable of Foo(will **not** become an attribute of every instance of FOO), which could be accessed by any instance of FOO - only if there's

How to define properties in __init__

非 Y 不嫁゛ 提交于 2019-12-19 03:15:39
问题 I whish to define properties in a class from a member function. Below is some test code showing how I would like this to work. However I don't get the expected behaviour. class Basket(object): def __init__(self): # add all the properties for p in self.PropNames(): setattr(self, p, property(lambda : p) ) def PropNames(self): # The names of all the properties return ['Apple', 'Pear'] # normal property Air = property(lambda s : "Air") if __name__ == "__main__": b = Basket() print b.Air # outputs

Passing input as a function argument using cin

元气小坏坏 提交于 2019-12-19 03:11:09
问题 My program: class test { int k; public: void changeval(int i){k=i;} }; int main() { test obj; int i; cin>>i; obj.changeval(i); return 0; } Is there any way, by which i can directly pass input from the user as an argument to the function changeval(int), without even initializing value to i?? I mean, i don't want to declare a variable just to pass value to a function. Is there any way i can avoid it? If yes, can I use it for constructors also? Thanks. 回答1: Nope. Now, you could put this into a

C++ virtual inheritance initializer list

故事扮演 提交于 2019-12-19 01:37:25
问题 in the following code: class A { public: int x; A(int x):x(x){} }; class B: public virtual A { public: B(int x):A(x){} }; class C: public virtual A { public: C(int x):A(x){} }; class D: public B, public C { public: D(int x):B(x++), C(x++), A(x++){} }; two questions: Why do I need to add A(...) in D's initializer list? D(int x):B(x++), C(x++), A(x++){} and D(int x):A(x++), B(x++), C(x++){} both give the same result with cout<<D(10).x , why? 回答1: Why do I need to add A(...) in D's initializer

Adding a default constructor to a base class changes sizeof() a derived type [duplicate]

此生再无相见时 提交于 2019-12-19 01:10:07
问题 This question already has answers here : When extending a padded struct, why can't extra fields be placed in the tail padding? (4 answers) Closed last year . I tend to think I have a pretty good grasp of C++ internals and memory layouts, but this one has me baffled. I have the following test code: #include <stdio.h> struct Foo { //Foo() {} int x; char y; }; struct Bar : public Foo { char z[3]; }; int main() { printf( "Foo: %u Bar: %u\n", (unsigned)sizeof( Foo ), (unsigned)sizeof( Bar ) ); }

When do constructors of static members of template classes get called in C++?

依然范特西╮ 提交于 2019-12-18 21:49:14
问题 There is plenty of information on when constructors of static members of ordinary classes are called. However, I am seeing some strange behavior with regard to template classes. What should the output of the following program be? (Note I use printf to avoid any static initialization order fiasco complications with std::cout.) #include <iostream> class B { public: B(const std::string &s) { printf("Hello I am B from %s\n", s.c_str()); } }; template<typename T> class Atempl { public: static B b_

const reference must be initialized in constructor base/member initializer list

两盒软妹~` 提交于 2019-12-18 21:19:32
问题 I am trying to block access to the default constructor of a class I am writing. The constructor I want others to use requires a const reference to another object. I have made the default constructor private to prevent others from using it. I am getting a compiler error for the default constructor because the const reference member variable is not initialized properly. What can I do to make this compile? class CFoo { public: CFoo(); ~CFoo(); }; class CBar { public: CBar(const CFoo& foo) :

const reference must be initialized in constructor base/member initializer list

瘦欲@ 提交于 2019-12-18 21:19:11
问题 I am trying to block access to the default constructor of a class I am writing. The constructor I want others to use requires a const reference to another object. I have made the default constructor private to prevent others from using it. I am getting a compiler error for the default constructor because the const reference member variable is not initialized properly. What can I do to make this compile? class CFoo { public: CFoo(); ~CFoo(); }; class CBar { public: CBar(const CFoo& foo) :

C++ Constructors have no return type. Just exactly why?

狂风中的少年 提交于 2019-12-18 20:53:22
问题 I've Googled this and read many posts, but there are so many different answers that all make logical sense that I was wondering if an expert on the topic could demystify this question. Some say that there is no return because there is no way to return - the syntax prohibits it - yes, this makes sense, but I believe that all functions have to return something, no? Others say that the constructor sort of returns the newly created object itself, which seems to make sense since the assignment

Xstream no-args constructor error

我的梦境 提交于 2019-12-18 19:17:07
问题 I'm getting the following error when it tries to create an instance of 'Transacao' `Error: Cannot construct br.com.cbmp.ecommerce.resposta.Transacao as it does not have a no-args constructor : Cannot construct br.com.cbmp.ecommerce.resposta.Transacao as it does not have a no-args constructor ` ---- Debugging information ---- message : Cannot construct br.com.cbmp.ecommerce.resposta.Transacao as it does not have a no-args constructor cause-exception : com.thoughtworks.xstream.converters