constructor

why copy constructor use private property directly in C++

岁酱吖の 提交于 2019-12-12 06:23:24
问题 Look at following: class node { int freq; public: node(const node &other) { freq = other.freq; } int getFreq() { return freq; } }; It works well. However, when I replace freq = obj.freq with freq = obj.getFreq() , it gives me this error: 'int node::getFreq(void)': cannot convert 'this' pointer from 'const node' to 'node &' Why? freq is a private member, it makes more sense that we should use the interface getFreq to access it. 回答1: It won't compile, because your function is not declared const

What version of javascript introduced arbitrary constructor return?

浪子不回头ぞ 提交于 2019-12-12 05:27:23
问题 Someone in the question Is there any reason to manually return in a constructor function mentioned that he thought that returning arbitrary objects in constructor calls may have been introduced in a recent version of Javascript. I am unable to access alternate browsers right now, and I can't find any information about this, so what version introduced this, or has it existed since forever? If it hasn't been around for long, then what browser versions will support it? 回答1: If you look in ECMA

Why do I get a compilation error when calling println method in the class body? #Java

那年仲夏 提交于 2019-12-12 05:26:20
问题 class Test { int a = 100; System.out.println(a); } class Demo { public static void main(String args[]) { Test t = new Test(); } } I'm new to programming. I found this code when I'm practicing. I don't understand why I'm getting this error. Here is the error I'm getting. Demo.java:3: error: <identifier> expected System.out.println(a); ^ Demo.java:3: error: <identifier> expected System.out.println(a); ^ 2 errors Compilation failed. Can you guys explain why I'm getting this error? 回答1: You can't

c++ is default constructor called in parametrized constructor?

寵の児 提交于 2019-12-12 05:14:02
问题 I have the following template class: template<typename T, int nSize> class Stack{ private: int m_nCurrentPos; Array<T> m_tArray; public: Stack(int nCurrentPos = 0); ... }; I would like the default constructor work as follows: template<typename T, int nSize> Stack<T,nSize>::Stack(int nCurrent){ m_nCurrent = nCurrentPos; m_tArray = Array<T>::Array(nSize); }; Where Array looks like that: template <typename T> class Array{ private: T* m_cData; int m_nSize; static int s_nDefaultSize; public: Array

Set prototype of the module in Javascript

女生的网名这么多〃 提交于 2019-12-12 04:57:08
问题 I've seen in some tests that using prototype methods increases the performance of the code execution and reduces the memory consumption, since methods are created per class, not per object. At the same time I want to use the module pattern for my class, since it looks nicer and allows to use private properties and methods. The layout of the code is like the following: var MyClass = function() { var _classProperty = "value1"; var object = { classProperty : _classProperty }; object.prototype =

C++ Inherit class with sending modified parameters to parent's constructor

Deadly 提交于 2019-12-12 04:55:40
问题 I want to create class inheritance, where my new class creates object of parent class, setting some default parameters (which are pointers to other objects). At the beginning I had: btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration(); btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration); btVector3 worldAabbMin(-1000,-1000,-1000); btVector3 worldAabbMax(1000,1000,1000); btAxisSweep3* broadphase = new btAxisSweep3

Explicit and non-explicit constructors

懵懂的女人 提交于 2019-12-12 04:43:55
问题 class Test { public: Test(int i) { cout<<"constructor called\n";} Test(const Test& t) { cout<<" copy constructor called\n";} }; class Test1 { public: Test1(int i) { cout<<"constructor called\n";} explicit Test1(const Test1& t) { cout<<" copy constructor called\n";} }; int main() { Test t(0); Test u = 0; //Test1 t1(0); Line 1 //Test1 u1 = 0; Line 2 } I observe different outputs. Case 1: When Line 1 and Line 2 are commented the o/p is : constructor called constructor called Case 2: When Line 1

Getters, Setters and Constructors in Java - Making a car and stocking it

南笙酒味 提交于 2019-12-12 04:34:36
问题 I'm trying to create a class in Java using BlueJ. My class is named Automobile. My goal is to be able to use my Constructor method to create cars with the variables: year, color, brand, number of doors, number of kilometers, if it's automatic (boolean), if it's sold (boolean), a description, and an identification number. All the variables have a set default value, a minimum and a maximum accepted value. I have to use getVariablename and setVariablename for my methods. My color and brand

Beginner Java required no arguments error on constructor

拈花ヽ惹草 提交于 2019-12-12 04:25:10
问题 So as an assignment for a class i'm trying to make a simple circle class and then use it as an constructor. Here is my class code: public class Circle { private double radius; private double pi; public void setRadius(double rad) { radius = rad; } public double getRadius() { return radius; } public double getArea() { return pi * radius * radius; } public double getDiameter() { return radius * 2; } public double getCircumference() { return 2 * pi * radius; } } Here's the program that uses the

Understanding of “inline” in C++ class constructor? [duplicate]

情到浓时终转凉″ 提交于 2019-12-12 04:14:30
问题 This question already has answers here : In C++ can constructor and destructor be inline functions? (5 answers) Closed 2 years ago . I read a source code of class address_v4 in boost library and there are several constructors declared with BOOST_ASIO_DECL (defined as inline) /// Construct an address from raw bytes. BOOST_ASIO_DECL explicit address_v4(const bytes_type& bytes); /// Construct an address from a unsigned long in host byte order. BOOST_ASIO_DECL explicit address_v4(unsigned long