constructor

C++, dealing with multiple constructor overloads and redundant code

喜欢而已 提交于 2019-12-24 18:53:02
问题 I've developed a recent interest in (re-)learning programming, so I have taken up C++ as it is a commonly used language. However, I've ran into a roadblock, and I have doubts whether my solution is the best way to go around it. I have a relatively complex class (for me anyways), with around 20 variables, which have been divided into 4 groups for simplification. It also has a parent class which is called during object initialization. However, I do not have a need to set these to values other

Use javassist to modify fields that use getters and setters in a class constructor

我的未来我决定 提交于 2019-12-24 18:47:14
问题 I am trying to modify the following fields in a class constructor using javassist : Label Label1 = new Label(new StringBuilder().append(user.name)); Label Label2 = new Label(new StringBuilder().append(user.time.toString()); I want to prepend text to the 2 labels. The text can be accessed and set using getText() and setText(). How could I achieve this? 回答1: The simplest approach is to use the ability to modify the constructor body with java code and let javassist create the bytecode. So you

access method without object (like static)

允我心安 提交于 2019-12-24 18:39:22
问题 I wanted to call a class method without creating an object or calling the constructor on the document.ready event. I tried following different options but nothing worked. var objReportsInterface; class ReportsInterface extends ReportBase { constructor() { super(); objReportsInterface = this; } subCategories() {} } $(document).ready(function() { $("dropdown").on('change', function() objReportsInterface.subCategories(); }) }) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1

Using parent class constructor initialiser-list in C++

泄露秘密 提交于 2019-12-24 18:30:09
问题 ORIGINAL POST When compiled, the following code produces error C2436: '__ctor' : member function or nested class in constructor initializer list in Child.h #include Parent.h class Child : public Parent { public: Child (List* pList) : Parent::Parent(pList) { } }; Here the parent class: in Parent.h class __declspec(dllimport) Parent : public GrandParent { public: Parent (List* pList = NULL); } in Parent.cpp Parent::Parent (List* pList) : m_a(1) ,m_b(1) ,GrandParent(pList) { } Isn't it right the

For a class constructor, what is the difference between the assignment via parentheses or the equal sign? [duplicate]

扶醉桌前 提交于 2019-12-24 17:43:38
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Initializing fields in constructor - initializer list vs constructor body In a lecture I attended, the lecturer talked briefly in C++ about non-default class constructors. He stated specifically that one version was preferable to the other. He showed these two examples: Point::Point(double x, double y, double z) : x_(x), y_(y), z_(z) {} Point::Point(double x, double y, double z) { x_= x; y_= y; z_= z; } He

For a class constructor, what is the difference between the assignment via parentheses or the equal sign? [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-24 17:42:20
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Initializing fields in constructor - initializer list vs constructor body In a lecture I attended, the lecturer talked briefly in C++ about non-default class constructors. He stated specifically that one version was preferable to the other. He showed these two examples: Point::Point(double x, double y, double z) : x_(x), y_(y), z_(z) {} Point::Point(double x, double y, double z) { x_= x; y_= y; z_= z; } He

C++ Cannot call constructor ' ' directly

自闭症网瘾萝莉.ら 提交于 2019-12-24 16:41:56
问题 I'm working on some OpenCV code and developed it in VS 2008 on windows. I'm trying to run the code on Linux with g++ but I get the error "Cannot call constructor 'ImageProcessor::ImageProcessor' directly" for ImageProcessor and all of the other classes I have created. I've attempted to find a way to indirectly call the constructor, but to no avail. Any suggestion would be great. The code compiles and runs fine on Windows. if (x == 1){ cout <<"MODE SELECTED: IMAGE TESTING \n"; ImageProcessor*

How to implement super mechanism in javascript through prototype?

霸气de小男生 提交于 2019-12-24 16:25:07
问题 I am struggling to understand the constructor invocation pattern in Javascript. I have a base object Mammal ( would it be incorrect to use the term class ? ) and an inherited object Cat . In the following code the object Cat correctly inherits from the Mammal object. /* Mammal base Object */ var Mammal = function(name) { this.name = name; } Mammal.prototype.get_name = function() { return this.name; } Mammal.prototype.says = function () { return this.saying || ''; } /* Cat object */ var Cat =

Multiple inheritance in django. Problem with constructors

此生再无相见时 提交于 2019-12-24 16:14:10
问题 I have a model like this: class Person(models.Model,Subject): name = .. The class Subject is not supposed to be in the Database so, it doesn't extends from models.Model: class Subject: def __init__(self,**kargs): _observers = [] my problem is that the constructor of Subject is never called , so i've tried adding this to the class Person: def __init__(self): super(Person,self).__init__() but now i have an error saying that init takes 1 arguments but 7 are given, and the only thing i'm doing is

Setting std::string to 0 during definition vs setting std::string to 0

帅比萌擦擦* 提交于 2019-12-24 15:54:45
问题 I was working on one my projects, and while I was making the constructor for a class, I was setting some of my variables to a default value. I went to set an std::string to NULL , and it gave me an error. But when I define an std::string and set it to NULL on the same line, it works without any errors. I was wondering why First Example std::string text = NULL; worked, and Second Example std::string text; text = NULL; didn't work. Now I know you shouldn't set a string to NULL, or 0, but I